Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Parent process ID

belsim
Principiante
3.656 Vistas
Is there a way to get the parent process ID in Visual Fortran ?
Or should I have to mix language ?
0 kudos
12 Respuestas
james1
Principiante
3.656 Vistas
Sure, you'd do it the same way as in C. What OS?

James
Jugoslav_Dujic
Colaborador Valioso II
3.656 Vistas
Um, parent process ID? Windows does not even have concept of parent and child processes -- it's a Unixian thingo. One can spawn a "child" process via CreateProcess, but the relationship between "child" and "parent" is very weak and AFAIK there's no way to reach the "parent" process (unless the "parent" passes that information explicitly, e.g. using command line). What exactly you're trying to do?

Jugoslav
james1
Principiante
3.656 Vistas
Jugoslav, the relationship may be "weak" but it is still there, and such information accessible.

James
Jugoslav_Dujic
Colaborador Valioso II
3.656 Vistas
James, I'm affraid you'll have to support that statement with a code sample :-).
james1
Principiante
3.656 Vistas
Ok, attached.
Jugoslav_Dujic
Colaborador Valioso II
3.656 Vistas
OK, you win -- I didn't count on undocumented APIs :-).
james1
Principiante
3.656 Vistas
Actually it is the concept that is important! I illustrated one way to do it, there is another using documented APIs, it just requires more code.

James
gfthomas8
Novato
3.656 Vistas
GetCurrentProcessID in kernel32 returns this.

HTH,
Gerry T.
james1
Principiante
3.656 Vistas
No, GetCurrentProcessID returns the current process id as the name says, not that of the parent.

James
gfthomas8
Novato
3.656 Vistas
That's correct. However, if you snapshot the system via CreateToolHelp32Snapshot, then the snapshot and GetCurrentProcessID allow you to get the process entry data for the current app and hence the parent process id. This is then passed with the snapshot to Process32First(Next) to reveal the name of the parent process as the last member of the PROCESSENTRY32 struct. This approach is preferable to use of internal functions of ntdll as advised in MSDN. Also, snapshots can reveal details on app modules (including loaded ocx's, dll's, and their versioning info) and the threads spawned by the app, wittingly or otherwise.

Ciao,
Gerry T.
james1
Principiante
3.656 Vistas
Gerry, yes, that approach is fine in practice although it would be quite a bit more verbose than my example, which was intended to demonstrate a concept. Note that I also mentioned there were more official interfaces available, but didn't have time to whip up an example based on that approach just to show that it was possible.

Cheers,

James
gfthomas8
Novato
3.656 Vistas
Hi James

I found your demo to be highly instructive and I'm sorry if I came across as unappreciative. Snapshotting is more bother and besides it hasn't made into CVF's kernel32 yet.

Have you noticed that if you run it from the ide, the parent is the ide; if you double click the .exe, the parent is Explorer; and if you shell it, the parent is the sheller. Ditto for the snapshot approach. I hope my pc hasn't been invaded by Raelians, :-).

Ciao,
Gerry T.
Responder