C++ 获取当前进程的 HWND

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2620409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:07:29  来源:igfitidea点击:

Getting HWND of current Process

c++cwinapiwindows

提问by Siddiqui

I have a process in c++ in which I am using window API. I want to get the HWND of own process. Kindly guide me how can I make it possible.

我在 C++ 中有一个进程,我在其中使用窗口 API。我想获得自己进程的 HWND。请指导我如何使它成为可能。

采纳答案by MSalters

You are (incorrectly) assuming that a process has only a single HWND. This is not generally true, and therefore Windows can't offer an API to get it. A program could create two windows, and have two HWNDs as a result. OTOH, if your program creates only a single window, it can store that HWND in a global variable.

您(错误地)假设一个进程只有一个 HWND。这通常不是真的,因此 Windows 无法提供 API 来获取它。一个程序可以创建两个窗口,结果有两个 HWND。OTOH,如果您的程序只创建一个窗口,它可以将该 HWND 存储在一个全局变量中。

回答by Matteo Italia

If you're talking about getting a processhandle, then it's not an HWND(which is a window handle), but a HANDLE(i.e., a kernel object handle); to retrieve a pseudo-handle relative to the current process, you can use GetCurrentProcessas the others explained.

如果您正在谈论获取进程句柄,那么它不是一个HWND(这是一个wi ndow h句柄),而是一个HANDLE(即内核对象句柄);要检索相对于当前进程的伪句柄,您可以GetCurrentProcess像其他人解释的那样使用。

On the other hand, if you want to obtain an HWND(a windowhandle) to the main window of your application, then you have to walk the existing windows with EnumWindowsand to check their ownership with GetWindowThreadProcessId, comparing the returned process ID with the one returned by GetCurrentProcessId. Still, in this case you'd better to save your main window handle in a variable when you create it instead of doing all this mess.

另一方面,如果您想获得应用程序主窗口的HWND窗口句柄),则必须使用 遍历现有窗口EnumWindows并检查其所有权GetWindowThreadProcessId,将返回的进程 ID 与由返回的进程 ID 进行比较GetCurrentProcessId. 尽管如此,在这种情况下,您最好在创建它时将主窗口句柄保存在一个变量中,而不是做所有这些乱七八糟的事情。

Anyhow, keep always in mind that not all handles are the same: HANDLEs and HWNDs, in particular, are completely different beasts: the first ones are kernel handles (=handles to kernel-managed objects) and are manipulated with generic kernel-handles manipulation functions (DuplicateHandle, CloseHandle, ...), while the second ones are handles relative to the window manager, which is a completely different piece of the OS, and are manipulated with a different set of functions.

无论如何,请始终记住并非所有句柄都相同:特别是HANDLEs 和HWNDs 是完全不同的野兽:第一个是内核句柄(=内核管理对象的句柄)并使用通用内核句柄操作进行操作函数 ( DuplicateHandle, CloseHandle, ...),而第二个是相对于窗口管理器的句柄,它是操作系统的一个完全不同的部分,并使用不同的函数集进行操作。

Actually, in theory an HWNDmay have the same "numeric" value of a HANDLE, but they would refer to completely different objects.

实际上,理论上 anHWND可能具有与 a 相同的“数字”值HANDLE,但它们会指代完全不同的对象。

回答by jave.web

Get your console window

获取您的控制台窗口

GetConsoleWindow();


"The return value is a handle to the window used by the console associated with the calling process or NULL if there is no such associated console."


“返回值是与调用进程关联的控制台使用的窗口的句柄,如果没有这样的关联控制台,则返回值为 NULL。”

https://msdn.microsoft.com/en-us/library/windows/desktop/ms683175(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/ms683175(v=vs.85).aspx

Get other windows

获取其他窗口

GetActiveWindow()might NOTbe the answer, but it could be useful
"The return value is the handle to the active window attached to the calling thread's message queue. Otherwise, the return value is NULL." > msdn GetActiveWindow() docs

GetActiveWindow()可能不是答案,但它可能很有用
“返回值是连接到调用线程的消息队列的活动窗口的句柄。否则,返回值为 NULL。” > msdn GetActiveWindow() 文档

However, the graphical windows are notjust popping up - so you should retrieve the handle from the place you/your app've created the window...e.g. CreateWindow()returns HWNDhandle so all you need is to save&retrieve it...

但是,图形窗口只是弹出 -所以你应该从你/你的应用程序创建窗口的地方检索句柄......例如CreateWindow()返回HWND句柄所以你需要的是保存和检索它......

回答by Greg Hewgill

The GetCurrentProcess()function returns a pseudo-handle which refers to the current process. This handle can be used in most Win32 API functions that take a process handle parameter.

GetCurrentProcess()函数返回一个指向当前进程的伪句柄。此句柄可用于大多数采用进程句柄参数的 Win32 API 函数。

The documentation contains more information about this pseudo-handle, including how to convert it to a real handle if you need to.

该文档包含有关此伪句柄的更多信息,包括如果需要,如何将其转换为真实句柄。

回答by Stu Mackellar

You can use HANDLE WINAPI GetCurrentProcess(void);from Kernel32.dll.

您可以HANDLE WINAPI GetCurrentProcess(void);从 Kernel32.dll使用。

See MSDN entry here.

请参阅此处的MSDN 条目。

回答by Denis Kolesnik

My example is not to deal with process, but maybe you need this:

我的例子不是处理过程,但也许你需要这个:

HWND hwndList = GetDlgItem(hwnd, IDCL_COMBOBOX);

This returns HWND of the control specified by its IDCL_COMBOBOX.

这将返回由其指定的控件的 HWND IDCL_COMBOBOX

回答by SChalice

Here is another answer:

这是另一个答案:

this->GetSafeHwnd();

this->GetSafeHwnd();