从另一个进程中的 SysHeader32 控件获取列名
Getting column names from SysHeader32 control in another process
我正在制作一个程序(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 窗口,则很简单:
-
发送消息
HDM_GETITEMCOUNT
,它返回项目数。 -
发送消息
HDM_GETITEM
,wParam
设置为要检索的项目的索引,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);
- C++ 获取"控件可能会到达约翰逊-特罗特代码上的非空函数的末尾
- 从资源 ID 获取 CButton 控件名称?
- 如何获取资源文件中对话框的按钮控件的大小和位置
- 使用 GetWindowTextW 从编辑控件获取希伯来语文本
- 使用 LDAP - 本机 API 中的页面控件获取活动目录组中的所有用户 ID
- C++获取对话框控件的位置和大小
- 从丰富的编辑控件获取文本
- 如何从丰富的编辑控件中获取文本作为 CString
- 从另一个窗口上的控件获取文本时出现问题
- 无法从winapi中的编辑控件获取文本
- 如何获取避免滚动条的树视图控件的最小大小
- Win32 c++:如何获取树视图控件中已检查项的数目
- 如何在 MFC 功能包中获取功能区控件以处理ID_WINDOW_TILE_VERT
- 无法获取 Visual C++ 6.0 对话框编辑控件以接受 Unicode /无法获取 EDITTEXT 资源以接受 Unicode
- 有没有办法获取对 MFC 对话框中所有子窗口或控件的引用,因为我有一个引用该控件的 CWnd 对象
- 如何从编辑控件获取数值
- 从编辑控件获取文本(纯Win32 API)
- 如何从Java应用程序(AWT)中的外部控件获取文本(或自动化)
- 从无窗口控件获取文本
- 从另一个进程中的 SysHeader32 控件获取列名