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
Beginner
910 Views
Is there a way to get the parent process ID in Visual Fortran ?
Or should I have to mix language ?
0 Kudos
12 Replies
james1
Beginner
910 Views
Sure, you'd do it the same way as in C. What OS?

James
0 Kudos
Jugoslav_Dujic
Valued Contributor II
910 Views
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
0 Kudos
james1
Beginner
910 Views
Jugoslav, the relationship may be "weak" but it is still there, and such information accessible.

James
0 Kudos
Jugoslav_Dujic
Valued Contributor II
910 Views
James, I'm affraid you'll have to support that statement with a code sample :-).
0 Kudos
james1
Beginner
910 Views
Ok, attached.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
910 Views
OK, you win -- I didn't count on undocumented APIs :-).
0 Kudos
james1
Beginner
910 Views
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
0 Kudos
gfthomas8
Novice
910 Views
GetCurrentProcessID in kernel32 returns this.

HTH,
Gerry T.
0 Kudos
james1
Beginner
910 Views
No, GetCurrentProcessID returns the current process id as the name says, not that of the parent.

James
0 Kudos
gfthomas8
Novice
910 Views
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.
0 Kudos
james1
Beginner
910 Views
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
0 Kudos
gfthomas8
Novice
910 Views
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.
0 Kudos
Reply