불행히도, 시스템이 포인터의 마샬링에 필요한 창 메시지를 처리하지 않기 때문에 다른 프로그램에서 만든 창에 액세스 할 때 쉽게 불가능합니다. 공유 DLL 파일 (다른 프로세스에로드하기 위해 일부 시스템 전체 Windows 훅 만들기) 또는 프로세스 간 메모리 액세스와 같은 다른 해킹을 사용하여이 작업을 수행해야합니다.
같은 프로그램에서 SysHeader32 창에 액세스하는 경우
는,이 간단 할 것 :
메시지 보내기 HDM_GETITEMCOUNT
, 그것은 항목 수를 반환합니다. 포인터에 검색 할 항목의 인덱스에 wParam
세트 lParam
세트
메시지 보내기 HDM_GETITEM
적절 HDITEM
구조를 설정합니다. 특히 mask
을 HDI_TEXT
으로 설정하고 pszText
에 대한 버퍼를 준비하고 그 길이를 cchTextMax
으로 설정하십시오.
예 :
int count = SendMessage(hWnd, HDM_GETITEMCOUNT, 0, 0);
std::cout << "There are " << count << " items.\n";
for (int i = 0; i < count; i++) {
TCHAR name[260];
HDITEM hdi;
hdi.mask = HDI_TEXT;
hdi.pszText = name;
hdi.cchTextMax = 260;
SendMessage(hWnd, HDM_GETITEM, i, reinterpret_cast<LPARAM>(&hdi));
std::cout << " " << i << ") " << hdi.pszText << "\n";
}
우리가 다른 프로그램의 공간에서 입력 및 출력 메모리를 저장해야하기 때문에이 같은 일이 필요하다 (취향에 따라 등 오류 검사를 추가하십시오) :
struct InterProcessData {
HDITEM hdi;
TCHAR buffer[260];
};
// Open the owning process and allocate a buffer big enough for
// our inter-process communication
DWORD dwProcessId;
GetWindowThreadProcessId(hWnd, &dwProcessId);
HANDLE hProcess = OpenProcess(
PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
FALSE, dwProcessId);
InterProcessData* pRemoteData = reinterpret_cast<InterProcessData*>(
VirtualAllocEx(hProcess, NULL, sizeof(InterProcessData), MEM_COMMIT, PAGE_READWRITE));
int count = SendMessage(hWnd, HDM_GETITEMCOUNT, 0, 0);
std::cout << "There are " << count << " items.\n";
for (int i = 0; i < count; i++) {
InterProcessData data;
data.hdi.mask = HDI_TEXT;
data.hdi.pszText = pRemoteData->buffer;
data.hdi.cchTextMax = 260;
// Write the HDITEM structure to the space in the remote process
// (without the buffer, its contents are undefined anyway)
WriteProcessMemory(hProcess, pRemoteData, &data, sizeof(data.hdi), NULL);
// Send the message itself, passing the remote address in lParam
SendMessage(hWnd, HDM_GETITEM, i, reinterpret_cast<LPARAM>(&pRemoteData->hdi));
// Read the data back, HDITEM and the buffer
ReadProcessMemory(hProcess, pRemoteData, &data, sizeof(data), NULL);
// The documentation says that the pszText can point elsewhere -
// copy it to our buffer in that case
if (data.hdi.pszText != pRemoteData->buffer)
ReadProcessMemory(hProcess, data.hdi.pszText, data.buffer, data.hdi.cchTextMax * sizeof(TCHAR), NULL);
std::cout << " " << i << ") " << data.buffer << "\n";
}
// Cleanup
VirtualFreeEx(hProcess, pRemoteData, 0, MEM_RELEASE);
CloseHandle(hProcess);
코드를 사용할 때 완벽하게 컴파일되지만 프로그램을 실행하고 program2에서 항목을 검색하면 항목 수 (15),하지만 항목 텍스트를 가져 오기 위해 SendMessage()를 시작하면 program2가 꺼지고 15 개 항목 모두에 대해 (pisymbole) TTw %와 같은 출력이 시작됩니다. 그 이유는 무엇인지 ? – computerWizard
모든 정보 동생 들께 감사드립니다.나는 프로그램이 코드가 정확히 존재할 때 디버깅을했다 : SendMessage (hWnd, HDM_GETITEM, i, reinterpret_cast (&hdi)); 사용). 어떤 이유로 program2에 어떤 종류의 오류를 일으키는 program2에 msg가 보내지는지 추측하고있다. 아이디어? –
computerWizard
우리 [채팅 에서이 토론을 계속하자] (http://chat.stackoverflow.com/rooms/51761/discussion-between-yirkha-and-computerwizard) – Yirkha