-
August 16th, 2005, 03:52 AM
#1
How to Get windows Handle using Process Id
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.
-
August 16th, 2005, 11:56 AM
#2
Re: How to Get windows Handle using Process Id
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 .
-
August 16th, 2005, 02:03 PM
#3
Re: How to Get windows Handle using Process Id
here the a sample code for doing it:
Code:
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);
}
Cheers
-
August 16th, 2005, 11:27 PM
#4
Re: How to Get windows Handle using Process Id
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.
-
August 17th, 2005, 08:25 AM
#5
Re: How to Get windows Handle using Process Id
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.
Please don't forget to rate users who helped you!
-
August 17th, 2005, 10:44 AM
#6
Re: How to Get windows Handle using Process Id
Originally Posted by philkr
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.
-
July 19th, 2010, 03:17 AM
#7
Re: How to Get windows Handle using Process Id
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.
-
July 21st, 2010, 05:06 AM
#8
Re: How to Get windows Handle using Process Id
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
Best regards,
Igor
-
July 21st, 2010, 09:02 AM
#9
Re: How to Get windows Handle using Process Id
Guys, you know this thread is 5 years old ...
-
July 21st, 2010, 09:22 AM
#10
Re: How to Get windows Handle using Process Id
...yep, and in spite to this the last guy is still unaware of the handles details.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|