Click to See Complete Forum and Search --> : Convert AnsiString to Long in SendMessage function


alrabaca
July 20th, 2007, 04:32 PM
Hi,

Im trying to convert a string value which is retrived from a listbox as shown here in my code:


HWND exWND = FindWindow(NULL, "Untitled - Notepad");
HWND exEB = FindWindowEx(exWND, NULL, "Edit", NULL);
int count = TForm1::ListBox1->Items->Count;
for(int x = 0; x < count; x++)
{
String combMessage;
combMessage = TForm1::ListBox1->Items->ValueFromIndex[x];
SendMessage(exEB, WM_SETTEXT, 0, (long)combMessage);
}


Basically when I compile it, I get an error:


[C++ Error] myfirstproject.cpp(87): E2031 Cannot cast from 'AnsiString' to 'long'


So my question, is there a way to change 'combMessage' from AnsiString to long so it would be compatable to go into my SendMessage function.

Thanks alot

darwen
July 21st, 2007, 04:42 PM
Presumably you're using C++/CLI, not managed C++.

If you have MFC support you can just do :


String ^combMessage = "Hello there";
CString sMessage(combMessage);
LPCTSTR szMesssage = static_cast<LPCTSTR>(sMessage);
SendMessage(exEB, WM_SETTEXT, 0, reinterpret_cast<long>(szMessage));


If you don't have MFC support included in the project you can do this instead :


String ^message = "Hello world";
array<Byte> ^stringBytes = System::Text::Encoding::ASCII->GetBytes(message);
pin_ptr<Byte> pStringBytes = &stringBytes[0];
const char *szByte = reinterpret_cast<const char *>(pStringBytes);
std::string sMessage(szByte, stringBytes->Length);
::SendMessage(exEB, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(sMessage.c_str())));


Bear in mind this will only work with SendMessage and NOT PostMessage.

Darwen.