问题
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.
回答1:
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.
回答2:
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
回答3:
"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
来源:https://stackoverflow.com/questions/3634298/em-getline-error-c-get-last-line-from-textbox