通过创建窗口进程的ID来查找窗口句柄
2010-04-30 12:33
627 查看
我们知道通过窗口句柄来查找创建这个窗口进程ID可以通过调用微软提供的API函数:
DWORD GetWindowThreadProcessId(
HWND hWnd, // Handle to the window.
LPDWORD lpdwProcessId // Pointer to a 32-bit value that receives the process identifier
);
即可。但是如果要反过来,通过创建窗口进程的ID来查找窗口句柄要怎么办呢?去MSND找,结果微软没有为我们提供这个API供我们使用。没有办法,现在只有我们自己去实现它哦!我这里用到了两个API :GetWindowThreadProcessId 和EnumWindows。
BOOL EnumWindows(
WNDENUMPROC lpEnumFunc,// Long pointer to an application-defined callback function
LPARAM lParam //Specifies an application-defined value to be passed to the callback function
);
实现如下:
typedef struct EnumFunArg
{
HWND hWnd;
DWORD dwProcessId;
}EnumFunArg;
BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam)
{
EnumFunArg *pArg = reinterpret_cast<EnumFunArg *> (lParam);
DWORD dwProcessId;
GetWindowThreadProcessId(hwnd, &dwProcessId);
if( dwProcessId == pArg->dwProcessId )
{
pArg->hWnd = hwnd;
// 注意:当查找到了,应该返回FALSE中止枚举下去
return FALSE;
}
return TRUE;//继续枚举下去直到所有顶层窗口枚举完为止
}
HWND myGetWindowByProcessId( DWORD dwProcessId )
{
EnumFunArg arg;
arg.dwProcessId = dwProcessId;
arg.hWnd = 0;
EnumWindows(lpEnumFunc,reinterpret_cast<LPARAM>(&arg));
return arg.hWnd;
}
之后我们们可以将创建窗口进程的ID作为参数传入到myGetWindowByProcessId函数里返回来的值就是与之对应的窗口句柄。
即:
HWND hWnd = myGetWindowByProcessId( dwProcessId );
DWORD GetWindowThreadProcessId(
HWND hWnd, // Handle to the window.
LPDWORD lpdwProcessId // Pointer to a 32-bit value that receives the process identifier
);
即可。但是如果要反过来,通过创建窗口进程的ID来查找窗口句柄要怎么办呢?去MSND找,结果微软没有为我们提供这个API供我们使用。没有办法,现在只有我们自己去实现它哦!我这里用到了两个API :GetWindowThreadProcessId 和EnumWindows。
BOOL EnumWindows(
WNDENUMPROC lpEnumFunc,// Long pointer to an application-defined callback function
LPARAM lParam //Specifies an application-defined value to be passed to the callback function
);
实现如下:
typedef struct EnumFunArg
{
HWND hWnd;
DWORD dwProcessId;
}EnumFunArg;
BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam)
{
EnumFunArg *pArg = reinterpret_cast<EnumFunArg *> (lParam);
DWORD dwProcessId;
GetWindowThreadProcessId(hwnd, &dwProcessId);
if( dwProcessId == pArg->dwProcessId )
{
pArg->hWnd = hwnd;
// 注意:当查找到了,应该返回FALSE中止枚举下去
return FALSE;
}
return TRUE;//继续枚举下去直到所有顶层窗口枚举完为止
}
HWND myGetWindowByProcessId( DWORD dwProcessId )
{
EnumFunArg arg;
arg.dwProcessId = dwProcessId;
arg.hWnd = 0;
EnumWindows(lpEnumFunc,reinterpret_cast<LPARAM>(&arg));
return arg.hWnd;
}
之后我们们可以将创建窗口进程的ID作为参数传入到myGetWindowByProcessId函数里返回来的值就是与之对应的窗口句柄。
即:
HWND hWnd = myGetWindowByProcessId( dwProcessId );
相关文章推荐
- 通过进程ID获得该进程主窗口的句柄或者获得特定窗口句柄
- 小实验:用创建进程()打开计算器,然后关闭进程句柄。再用打开进程(进程ID),使用两次,得到两个进程句柄。实验目的:这两个进程句柄都能控制这个进程吗?通过该试验加深对句柄的理解!!
- 通过进程ID获得该进程主窗口的句柄
- 通过进程ID获取进程主窗口句柄
- 通过进程ID获取最顶层窗口句柄
- 通过进程号ID获得窗口的句柄
- 通过进程ID获取窗口句柄(微软)
- 通过进程ID获得该进程主窗口的句柄
- 通过进程id获取窗口句柄
- 通过Windows进程ID获取窗口句柄
- VC怎么通过进程ID得到窗口句柄
- 通过进程ID获得该进程主窗口的句柄
- 通过进程名或进程ID获取进程句柄,窗口句柄
- 通过进程号ID获得窗口的句柄
- 通过进程ID获得主窗口句柄
- 通过进程号ID获得窗口的句柄 .
- 通过进程Id获取窗口句柄
- 通过进程ID获得该进程主窗口的句柄
- 通过进程ID获得主窗口句柄
- dos命令记录以及dos下通过进程id查找工作路径