C++ 枚举窗口时出现问题
我在尝试运行以下代码时遇到问题:C++ 枚举窗口时出现问题,c++,windows,winapi,C++,Windows,Winapi,我在尝试运行以下代码时遇到问题: #include "header.h" int main() { id = GetCurrentProcessId(); EnumWindows(hEnumWindows, NULL); Sleep(5000); //MoveWindow(hThis, 450, 450, 100, 100, TRUE); system("pause"); return 0; } //header.h #include &
#include "header.h"
int main()
{
id = GetCurrentProcessId();
EnumWindows(hEnumWindows, NULL);
Sleep(5000);
//MoveWindow(hThis, 450, 450, 100, 100, TRUE);
system("pause");
return 0;
}
//header.h
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <Windows.h>
using namespace std;
DWORD id = 0;
HWND hThis = NULL;
BOOL CALLBACK hEnumWindows(HWND hwnd, LPARAM lParam)
{
DWORD pid = 0;
pid = GetWindowThreadProcessId(hwnd, NULL);
if (pid == id)
{
hThis = GetWindow(hwnd, GW_OWNER);
if (!hThis)
{
cout << "Error getting window!" << endl;
}
else
{
char *buffer = nullptr;
int size = GetWindowTextLength(hThis);
buffer = (char*)malloc(size+1);
if (buffer != nullptr)
{
GetWindowText(hThis, buffer, size);
cout << pid << ":" << buffer << endl;
free(buffer);
}
}
}
return TRUE;
}
#包括“header.h”
int main()
{
id=GetCurrentProcessId();
枚举窗口(hEnumWindows,NULL);
睡眠(5000);
//移动窗口(hThis,450450100100,TRUE);
系统(“暂停”);
返回0;
}
//标题.h
#包括
#包括
#包括
#包括
使用名称空间std;
dwordid=0;
HWND-hThis=NULL;
BOOL回调hEnumWindows(HWND-HWND,LPARAM-LPARAM)
{
DWORD pid=0;
pid=GetWindowThreadProcessId(hwnd,NULL);
如果(pid==id)
{
hThis=GetWindow(hwnd,GW_所有者);
如果(!hThis)
{
cout根据API
检索调用进程的进程标识符
另一方面,
检索创建指定窗口的线程的标识符,以及创建窗口的进程的标识符(可选)
返回值是创建窗口的线程的标识符
看看你的电话:
pid = GetWindowThreadProcessId(hwnd, NULL);
实际上,您返回的是一个线程ID,而不是进程ID。因此,当您将pid
与ID
进行比较时,您正在比较进程ID和线程ID,这是行不通的。请尝试以下方法:
GetWindowThreadProcessId(hwnd, &pid);
(注意:我无法实际测试这是否有效,因为EnumWindows
需要一个顶级窗口来枚举,我将此作为控制台应用程序运行。如果此答案不适用于您,请告诉我,我将删除它。)
(第二个注意事项是,您不再需要使用NULL
,即使是像HWND
nullptr
这样的WinAPI工具也可以很好地工作。)我假设您正在尝试从ProcessID中查找“主”窗口。在这种情况下,这可能会有帮助:
#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <Windows.h>
struct WindowHandleStructure
{
unsigned long PID;
HWND WindowHandle;
};
BOOL CALLBACK EnumWindowsProc(HWND WindowHandle, LPARAM lParam)
{
unsigned long PID = 0;
WindowHandleStructure* data = reinterpret_cast<WindowHandleStructure*>(lParam);
GetWindowThreadProcessId(WindowHandle, &PID);
if (data->PID != PID || (GetWindow(WindowHandle, GW_OWNER) && !IsWindowVisible(WindowHandle)))
{
return TRUE;
}
data->WindowHandle = WindowHandle;
return FALSE;
}
HWND FindMainWindow(unsigned long PID)
{
WindowHandleStructure data = { PID, nullptr };
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&data));
return data.WindowHandle;
}
int main()
{
HWND Window = FindMainWindow(GetCurrentProcessId());
std::wstring Buffer(GetWindowTextLength(Window) + 1, L'\0');
GetWindowText(Window, &Buffer[0], Buffer.size());
std::wcout << Buffer.c_str() << L"\n";
system("pause");
return 0;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
结构窗把手结构
{
无符号长PID;
窗柄;
};
BOOL回调EnumWindowsProc(HWND WindowHandle,LPARAM LPARAM)
{
无符号长PID=0;
WindowHandleStructure*数据=重新解释强制转换(LPRAM);
GetWindowThreadProcessId(WindowHandle和PID);
如果(数据->PID!=PID | |(GetWindow(WindowHandle,GW_所有者)和&!IsWindowVisible(WindowHandle)))
{
返回TRUE;
}
数据->窗口句柄=窗口句柄;
返回FALSE;
}
HWND FindMainWindow(无符号长PID)
{
WindowHandleStructure数据={PID,nullptr};
EnumWindows(EnumWindowsProc、重新解释强制转换(&data));
返回data.WindowHandle;
}
int main()
{
HWND Window=FindMainWindow(GetCurrentProcessId());
std::wstring缓冲区(GetWindowTextLength(窗口)+1,L'\0');
GetWindowText(窗口和缓冲区[0],Buffer.size());
Std::WcOut[RoBtthARVY ]我看到了C风格的演员阵容,所以希望不是C++,)Cys:我看到的东西是“代码”> CUT,这是在VS2013上用C++编译器编译的,我喜欢MALLC到新的,这就是为什么已经使用了。你可以在控制台应用程序中调用<代码>枚举窗口()/<代码>。控制台应用程序可以调用WiN2 API函数,和<代码>枚举窗口()
枚举所有顶级窗口,无论调用它的应用程序的类型如何。调用EnumWindows()不需要自己的顶级窗口
@RemyLebeau没错,但看看op的其他代码在做什么-它正在检查顶级窗口的pid是否与该程序的pid匹配。因为我刚刚使用了一个快速脏的控制台应用程序进行测试,我的应用程序中没有可用的顶级窗口,所以我无法正确执行pid检查。