0

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.

anno
  • 5,970
  • 4
  • 28
  • 37
Rocky
  • 1
  • 1
  • 1
    Read the doc : "Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer." – anno Sep 03 '10 at 10:33
  • I saw that, but I'm quite new to this stuff and I'm not shure what they do mean. Sounds like a riddle to me... – Rocky Sep 03 '10 at 13:26
  • See this thread : http://www.gamedev.net/community/forums/topic.asp?topic_id=147943 – anno Sep 03 '10 at 19:19

3 Answers3

2

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
  • 922,412
  • 146
  • 1,693
  • 2,536
1

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
  • 3,158
  • 24
  • 29
  • 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 Sep 03 '10 at 13:27
  • @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 Sep 05 '10 at 10:05
  • 1
    That's not the problem. The issue here is that you fail to initialize the buffer to hold the buffer size in its first word: `((WORD*)param)[0]=bufferSize;` before sending `EM_GETLINE`. – IInspectable Dec 04 '13 at 20:49
0

"Long pointer to the buffer that receives a copy of the line. The first word of the buffer specifies the maximum number of characters that can be copied to the buffer" http://msdn.microsoft.com/en-us/library/aa921607.aspx

*(WORD*) param = 1000
Chike
  • 306
  • 3
  • 5