从另一个应用程序读取状态栏

时间:2016-02-21 22:09:37

标签: c++ winapi

我正在尝试从包含多个面板的其他应用程序的状态栏中读取文本。我该怎么做呢?我试图在其他应用程序中分配内存并基于此读取该缓冲区:

http://www.codeproject.com/Articles/5570/Stealing-Program-s-Memory

我目前的代码是:

GetWindowThreadProcessId(grxstatusbar, &statusbarpid);
statusbarprocess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, statusbarpid);

char* _buffer = nullptr;
char buffer[20];

_buffer = (char*)VirtualAllocEx(statusbarprocess, NULL, 20, MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(statusbarprocess, _buffer, &buffer, 20, NULL);

SendMessage(grxstatusbar, SB_GETTEXT, (WPARAM)0, (LPARAM)_buffer);
ReadProcessMemory(statusbarprocess, _buffer, buffer, 20, NULL);
VirtualFreeEx(statusbarprocess, _buffer, 0, MEM_RELEASE);

然而,缓冲总是只是胡言乱语。什么是解决这个问题的最佳方法。

1 个答案:

答案 0 :(得分:0)

编辑*****

如果目标窗口是UNICODE,则无效。您可以使用SB_GETTEXTLENGTH查找文本长度。使用UNICODE版本:

int char_count;
char_count = SendMessageW(hwndTarget, SB_GETTEXTLENGTH, 1, 0);
if (char_count <= 0)
    return 0;
char_count = LOWORD(char_count) + 1;

int datalen = LOWORD(char_count);
datalen *= sizeof(wchar_t);

void* data = VirtualAllocEx(hprocess, NULL, datalen, 
    MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

SendMessageW(hwndTarget, SB_GETTEXT, 1, (LPARAM)data);
wchar_t *buf = new wchar_t[char_count];
if (ReadProcessMemory(hprocess, data, buf, datalen, 0))
    std::wcout << "buf: " << buf << "\n";
delete[]buf;

VirtualFreeEx(hprocess, data, 0, MEM_RELEASE);
CloseHandle(hprocess);