我正在编写一个小 DLL,一旦注入到我的目标进程中,它就会找到一个hwnd
并将窗口的文本写入文件。我有这样的设置:
hWnd = FindWindow(L"tSkMainForm",NULL);
chat = FindWindowEx(hWnd, NULL, L"TConversationForm", NULL);
ofstream myfile("X:\\Handles.txt", ios::out | ios::app);
if (myfile.is_open())
{
int len;
len = SendMessage(chat, WM_GETTEXTLENGTH, 0, 0) + 1; // + 1 is for the null term.
char* buffer = new char[len];
SendMessageW(chat, WM_GETTEXT, (WPARAM)len, (LPARAM)buffer);
myfile.write(buffer,len); /* << buffer <<endl; */
myfile.close();
delete[] buffer;
}
它在看似随机的时间内工作,然后应用程序 (Skype) 崩溃。它只在我分配内存时崩溃。我尝试过使用malloc
:
char* buffer = (char*)malloc(len); //I even tried removing "(char*) before malloc
//Do the rest of the stuff here
free((void*) buffer);
但这也崩溃了。
我的 DLL 调用CreateThread
,通过添加一个额外的菜单项AppendMenu
,并为它处理消息,一切都完美无缺。似乎分配内存并不想工作,而只是在随机时间。我不确定,但我认为 Skype 正在覆盖我的内存,或者我正在覆盖 Skype 的内存(我如何确保两者不会相互覆盖?)
另外,我知道 Skype 有一个 API,但我想这样做。如果我想编写一个严肃的程序,我会使用 Skype API。谢谢。