Click to See Complete Forum and Search --> : Getting a process's main thread handle
yaniv_av
July 12th, 2007, 08:42 AM
When creating a process with the "CreateProcess" function, it return _PROCESS_INFORMATION with the main thread HANDLE of the process, but if I create the process with "ShellExecute/ShellExecuteEx", how can I get that handle ?
Or, In the different wording, How can I get a handler to a main thread of some process, via the process-handle ?
10X !
ashukasama
July 12th, 2007, 08:54 AM
When creating a process with the "CreateProcess" function, it return _PROCESS_INFORMATION with the main thread HANDLE of the process, but if I create the process with "ShellExecute/ShellExecuteEx", how can I get that handle ?
ShellExecuteEx( LPSHELLEXECUTEINFO)
LPSHELLEXECUTEINFO this structure contain the handle of process
typedef struct _SHELLEXECUTEINFO {
DWORD cbSize;
ULONG fMask; // set it to SEE_MASK_NOCLOSEPROCESS
...........
HANDLE hProcess;
} SHELLEXECUTEINFO, FAR* LPSHELLEXECUTEINFO;
yaniv_av
July 12th, 2007, 09:00 AM
But I want to get a handler to that process main thread ...
How can I get a handler to a process main thread via that process handler ?
ashukasama
July 12th, 2007, 09:17 AM
How can I get a handler to a process main thread via that process handler ?
please elobarate more :blush:,
yaniv_av
July 12th, 2007, 09:21 AM
please elobarate more :blush:,
When you create a process, it created with a main thread.
The process itself has a handle, and the thread also has a handle.
If you use CreateProcess, one of the returned parameters is LPPROCESS_INFORMATION which contains:
typedef struct _PROCESS_INFORMATION {
HANDLE hProcess;
HANDLE hThread;
DWORD dwProcessId;
DWORD dwThreadId;
} PROCESS_INFORMATION;
As you can see you have 2 handlers, to the process and to it's main thread.
My question is how can you get the thread handler if you have only the hProcess handler... (Like if you create the process with ShellExecuteEx)
ashukasama
July 12th, 2007, 09:37 AM
The process itself has a handle, and the thread also has a handle.
If you use CreateProcess, one of the returned parameters is LPPROCESS_INFORMATION which contains:
typedef struct _PROCESS_INFORMATION {
HANDLE hProcess;
HANDLE hThread;
DWORD dwProcessId;
DWORD dwThreadId;
} PROCESS_INFORMATION;
As you can see you have 2 handlers, to the process and to it's main thread.
My question is how can you get the thread handler if you have only the hProcess handler... (Like if you create the process with ShellExecuteEx)
Basic Difference Between Create Process and ShellExecute or ShellExecuteEx
CreateProcess
The CreateProcess function creates a new process and its primary thread. The new process runs the specified executable file in the security context of the calling process.
but ShellExecute performs an operation on a specified file. The file can be an executable file or a document
To obtain information about the application that is launched as a result of calling ShellExecute, use ShellExecuteEx.
so you will not get the Thread Handle in ShellExecuteEx.
what the problem in using CreateProcess
yaniv_av
July 12th, 2007, 10:35 AM
The reason I don't use CreateProcess is that it is not working in VISTA.
But my question is more simple: Suppose I have a process handler, no matter how, meybe it's a handler of an existing process that wasn't created by me - how can I retrieve the process main thread handler?
There must be some way to do that...
ashukasama
July 12th, 2007, 10:42 AM
The reason I don't use CreateProcess is that it is not working in VISTA.
But my question is more simple: Suppose I have a process handler, no matter how, meybe it's a handler of an existing process that wasn't created by me - how can I retrieve the process main thread handler?
There must be some way to do that...
read this article (http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-for-Developers-_1320_-Part-4-_1320_-User-Account-Control.aspx)
I dont have vista so i can't test but may be it is fruitful for you .
yaniv_av
July 12th, 2007, 10:53 AM
read this article (http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-for-Developers-_1320_-Part-4-_1320_-User-Account-Control.aspx)
I dont have vista so i can't test but may be it is fruitful for you .
I'll read that article :)
But I still don't have an answer to my basic question -
Let's forget about vista, forget about createprocess or shellexecute.
The question is simple - Suppose I have a process handler (let's say I got it by HANDLE hl = GetCurrentProcess() ), how do I get the process-main-thread ???
miteshpandey
July 12th, 2007, 11:15 AM
Didn't find an API to get the main thread of a process by supplying a process Id or Handle.
Btw, Is GetCurrentThread what you are looking for?
yaniv_av
July 12th, 2007, 11:18 AM
Didn't find an API to get the main thread of a process by supplying a process Id or Handle.
Btw, Is GetCurrentThread what you are looking for?
No... I need some way to get the primary thread handler of any process - not the current..
Suppose I want to send a WM_MSG to the main thread of some process - how can I do that? There must be some wat to get that thread handler...
miteshpandey
July 12th, 2007, 11:26 AM
Probably due to security reasons the Windows Architects refrained from supplying such a function.
You have no choice but to get the Window Handle (HWND) maybe by using ::FindWindow and ::EnumWindows and send the message to this window.
yaniv_av
July 12th, 2007, 11:31 AM
Probably due to security reasons the Windows Architects refrained from supplying such a function.
You have no choice but to get the Window Handle (HWND) maybe by using ::FindWindow and ::EnumWindows and send the message to this window.
But with FindWindow I have to know the window title. And I don't know it. I know only the process handler ....
It's a little bit strange that I can use TerminateProcess to kill a process bruttly, but I can't send it WM_CLOSE...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.