Dear All,
I am having the process id of all the process. Now I want to get the Windows Handle for that Process. Is it possible ?
Thanks in Advance.
Printable View
Dear All,
I am having the process id of all the process. Now I want to get the Windows Handle for that Process. Is it possible ?
Thanks in Advance.
No.
A process can have multiple windows open.
You could however iterate over all the windows and retrieve the process ID from the HWND and match this process ID with the ID you are interested in. Check out GetWindowThreadProcessId .
here the a sample code for doing it:
CheersCode:HWND h = ::GetTopWindow(0 );
while ( h )
{
DWORD pid;
DWORD dwTheardId = ::GetWindowThreadProcessId( h,&pid);
if ( pid == /*your process id*/ )
{
// here h is the handle to the window
break;
}
h = ::GetNextWindow( h , GW_HWNDNEXT);
}
Thanks Gurus
Sample Code is working Fine. I can able to get the Handles of all open windows. Still I have a doubt what is handle (HANDLE) and what is handle to window ( HWND ) is there any difference in that.
Please clarify my doubt, if possible give a good direction to know more about it.
Thanks in Advance.
A HWND is a special type of a HANDLE: a handle to a window. Likewise HFILE is a handle to a file. Since some functions work with more than one type of handle there is the generic type HANDLE. For example with CloseHandle() you can close a file handle or a thread handle, etc. A handle is only a kind of pointer to the real thing, which is managed internly by windows.
A HWND is not HANDLE but it is a handle indeed. A HWND cannot be used to replace when a HANDLE is needed. And HFILE shall not be used anymore, it got generally replaced via HANDLE and it's only served through backward compatibility with 16bit windows and the "old" I/O routines.Quote:
Originally Posted by philkr
according to: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
HWND is defined as such:
typedef HANDLE HWND;
typedef PVOID HANDLE;
typedef void *PVOID;
Also in Visual Studio 6, HWND is defined in the include files as such:
typedef /* [wire_marshal] */ void __RPC_FAR *HWND; (C:\Program Files\Microsoft Visual Studio\VC98\Include\WTYPES.H)
# define __RPC_FAR (C:\Program Files\Microsoft Visual Studio\VC98\Include\RPC.H)
typedef void *HANDLE; (C:\Program Files\Microsoft Visual Studio\VC98\Include\WINNT.H)
typedef void *PVOID; (C:\Program Files\Microsoft Visual Studio\VC98\Include\WINNT.H)
So, as far as I can tell, HWND and HANDLE are exactly equivalent, at least as far as VC6 is concerned. I have yet to confirm this in VS2010.
According to this experiment below, HWND is not equivalent to HANDLE: :)
Code:// E:\Temp\558\558.cpp
#include <windows.h>
int main()
{
HANDLE h = NULL;
HWND hwnd = NULL;
hwnd = h;
return 0;
}
Code:E:\Temp\558>cl 558.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
558.cpp
558.cpp(8) : error C2440: '=' : cannot convert from 'HANDLE' to 'HWND'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Guys, you know this thread is 5 years old ...
...yep, and in spite to this the last guy is still unaware of the handles details. ;)