views:

40

answers:

2

Hello !

I'm having a minor problem with EM_GETLINE. I have a textbox I want to extract the text from. The box keeps updating all the time (it's a log file thet keeps updating, last message at the bottom). All I want is that very last line.

My code:

        HWND hwnd = (HWND)0x00020A72;
 TCHAR param[1000];
 char display[1000];
 LONG lResult;
 lResult = SendMessage( hwnd, WM_GETTEXT, 500, (LPARAM)param);
 //lResult = SendMessage( hwnd, EM_STREAMOUT, SF_RTF, (LPARAM)param);
 //lResult = SendMessage( hwnd, EM_GETLINE, 1, (LPARAM)param); 
 wcstombs(display, param, 1000);

 printf( " %s\n", display );

As you can see I've tried WM_GETTEXT (that works). When using GETLINE it compiles nice (VS2010express) but returns rubbish.

Would be really gratful for help. Thanks for listening.

A: 

You should ask for the last not the first line and add the NULL for the termination, try the following:

int last_line = SendMessage(hwnd, EM_GETLINECOUNT,0 ,0) - 1;
int size = SendMessage(hwnd, EM_GETLINE, (WPARAM)last_line, (LPARAM)param);
param[size] = 0;//EM_GETLINE does not add the NULL
Tassos
Well I tried your sugestion but I recieve 0 as the size. The line counting on the other hand is works and is correct... ;-(Any idea why the size is 0?Thanks
Rocky
@Rocky Documentation says that the return value is zero when the line is wrong. I guess it's probably that the hwnd belong to an other process.
Tassos
+1  A: 

This window belongs to another process, right? I can see you hard-coded the window handle. Not so sure that message is automatically marshaled across process boundaries, only the system message are (WM_Xxx < 0x400).

Marshaling it yourself requires OpenProcess, VirtualAllocEx to allocate the buffer, WriteProcessMemory to intialize it, SendMessage, ReadProcessMemory to read the buffer. Plus cleanup.

Hans Passant