Getting HWND of current Process

2020-02-10 12:35发布

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.

7条回答
冷血范
2楼-- · 2020-02-10 13:12

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

See MSDN entry here.

查看更多
家丑人穷心不美
3楼-- · 2020-02-10 13:15

If you're talking about getting a process handle, 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 GetCurrentProcess as the others explained.

On the other hand, if you want to obtain an HWND (a window handle) to the main window of your application, then you have to walk the existing windows with EnumWindows and 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.

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.

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

查看更多
时光不老,我们不散
4楼-- · 2020-02-10 13:15

Here is another answer:

this->GetSafeHwnd();

查看更多
We Are One
5楼-- · 2020-02-10 13:20

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.

查看更多
我命由我不由天
6楼-- · 2020-02-10 13:25

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."

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

Get other windows

GetActiveWindow() might NOT be 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

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

查看更多
再贱就再见
7楼-- · 2020-02-10 13:28

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.

查看更多
登录 后发表回答