从另一个进程中的 SysHeader32 控件获取列名

Getting column names from SysHeader32 control in another process

本文关键字:控件 获取 SysHeader32 另一个 进程      更新时间:2023-10-16

我正在制作一个程序(program1(,它将读取位于另一个程序(program2(中报告样式列表(SysListView32(中的标头(SysHeader32(的列的名称。

所以基本上我希望我的程序进入另一个程序并读取我找到的所有标头 (SysHeader32( 的标题名称。由于程序的每个列表都有许多不同的列表和标题,因此我决定将 EnumChildWindows 函数与 EnumChildProc 回调函数一起使用,以查看子窗口的所有句柄。使用这些句柄,我使用 GetClassName() 来查看类名是什么,当我看到它是一个 SysHeader32 时,我知道我找到了一个可以包含各种标题名称的标题......但我不知道我可以使用什么代码来从这些不同的标题中获取文本,也不知道如何识别每个标题......

这是我到目前为止的代码,可以找到找到的每个 SysHeader32 标头的句柄:

BOOL CALLBACK EnumChildProc (HWND hWnd, LPARAM lParam)
{
  char myBuffer [100];//buffer that will get the class name
  GetClassName(hWnd, myBuffer, 100);
  string myString (myBuffer);//converting myBuffer into a readable string
  if (myString == "SysHeader32")
  {
    ///here is where I am currently lost
    ///I just don't know how to get the text from the different titles/items
    ///in the header found
  }
}

问题1:如何检查标题中有多少个不同的标题/项目?

问题 2::如何获取标题中找到的每个标题/项目的文本?

请提供一些示例代码。

不幸的是,在访问由另一个程序创建的窗口时,这并不容易实现,因为系统不会对指针进行必要的窗口消息封送。您需要从共享 DLL 文件(在其中创建一些系统范围的 Windows 挂钩以将其加载到其他进程中(或使用其他技巧(如进程间内存访问(执行此操作。


如果在同一程序中访问 SysHeader32 窗口,则很简单:

  1. 发送消息HDM_GETITEMCOUNT,它返回项目数。

  2. 发送消息HDM_GETITEMwParam设置为要检索的项目的索引,lParam设置为指针以适当设置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);