Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Process Identifier from Process Handle

2 views
Skip to first unread message

Grinder

unread,
Nov 23, 2001, 8:17:02 PM11/23/01
to
Does anyone know how to get the process identifier if the
process handle is known?


Pi

unread,
Nov 23, 2001, 10:07:51 PM11/23/01
to

Grinder ha escrito:

> Does anyone know how to get the process identifier if the
> process handle is known?

AFAIK you can do the reverse, i.e. get a process handle if you know the
process id, but I haven't seen anything in the SDK to do what you want.
Maybe it's a Windows limitation? However you can get it by other ways:
for example, if you know the window handle of a window property of that
process, you can get the process name, and then you can get the process
id using that process name... But you will need the free JCL library
from www.delphi-jedi.org.

regards, Pi

Leonid Troyanovsky

unread,
Nov 24, 2001, 7:17:39 AM11/24/01
to
Grinder wrote:

> Does anyone know how to get the process identifier if the
> process handle is known?

{NT only}

var
hProcess, hThread: THandle;
pGetCurrentProcessID: Pointer;
rtid, pid: DWord;
..
pGetCurrentProcessID := GetProcAddress( GetModuleHandle('kernel32.dll'),
'GetCurrentProcessId');
Win32Check(pGetCurrentProcessID <> nil);
hThread:= CreateRemoteThread ( hProcess,
nil,
0,
pGetCurrentProcessID,
nil,
0,
rtid);
Win32Check(hThread <> 0);
WaitForSingleObject(hThread, INFINITE);
GetExitCodeThread(hThread, pid);
CloseHandle(hThread);

Caption := IntToStr(pid); // result

--
Regards, LVT.

Grinder

unread,
Nov 24, 2001, 5:43:26 AM11/24/01
to
What I'm after is a trim way to use ShellExecuteEx and
EnumWindows--I want to be able to tell if the window belongs to
the process I hold the handle to.

What do you mean by "process name"?

"Pi" <parn...@gmx.net> wrote in message
news:3BFF0F06...@gmx.net...

Grinder

unread,
Nov 24, 2001, 6:50:16 PM11/24/01
to
Wow, thanks--I would not have thought to do this.


Leonid Troyanovsky

unread,
Nov 25, 2001, 6:56:12 AM11/25/01
to
Grinder wrote:

> What I'm after is a trim way to use ShellExecuteEx and
> EnumWindows--I want to be able to tell if the window belongs to
> the process I hold the handle to.

FindExecutable {uses ShellApi}
CreateProcess
WaitForInputIdle
EnumWindows & GetWindowThreadProcessId

--
Regards, LVT.

Grinder

unread,
Nov 25, 2001, 7:17:14 PM11/25/01
to
This does not work for me--I want to use the extended
capabilities of ShellExecuteEx. Pi has offered a solution, but
I can't help but feel I'm missing something simple.

"Leonid Troyanovsky" <lv...@eco-pro.ru> wrote in message
news:3C00DC5C...@eco-pro.ru...

0 new messages