473,385 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Process Starter

Hi All,

I know using Process class you can get the information of the current
processes
running on the current computer. Some processes are started by the user. And
some processes are started by other processes. My question is how do I know
the starter of a process? If process A is started by process B, how do I
know the information of the process B by querying process A?

Thanks,
Lenny
Nov 18 '05 #1
3 1236
You don't.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"L. L." <ll**@cyence.com.invalid> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
Hi All,

I know using Process class you can get the information of the current
processes
running on the current computer. Some processes are started by the user. And some processes are started by other processes. My question is how do I know the starter of a process? If process A is started by process B, how do I
know the information of the process B by querying process A?

Thanks,
Lenny

Nov 18 '05 #2

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
You don't.

--


Well you can. It's just not easy.

Here's how to do it in VB.
Declare Function CreateToolhelp32Snapshot _
Lib "KERNEL32.DLL" (ByVal dwFlags As Integer, _
ByVal th32ProcessID As Integer) As Integer
Declare Function Process32First Lib "KERNEL32.DLL" _
(ByVal hSnapshot As Integer, ByVal PE As Byte()) As Integer
Declare Function Process32Next Lib "KERNEL32.DLL" _
(ByVal hSnapshot As Integer, ByVal PE As Byte()) As Integer
Declare Function CloseHandle Lib "KERNEL32.DLL" _
(ByVal hObject As Integer) As Integer

Const TH32CS_SNAPPROCESS As Integer = 2
Const SIZEOF_PROCESSENTRY32 As Integer = 564
Const SIZE_OFFSET = 0
Const PROCESS_OFFSET = 8
Const PARENT_OFFSET = 24
'// typedef struct tagPROCESSENTRY32
'// {
'// DWORD dwSize;
'// DWORD cntUsage;
'// DWORD th32ProcessID;
'// DWORD th32DefaultHeapID;
'// DWORD th32ModuleID;
'// DWORD cntThreads;
'// DWORD th32ParentProcessID;
'// LONG pcPriClassBase;
'// DWORD dwFlags;
'// TCHAR szExeFile[MAX_PATH];
'// DWORD th32MemoryBase;
'// DWORD th32AccessKey;
'// } PROCESSENTRY32;

Function GetParentProcessID(ByVal id As Integer) As Integer
Dim b(564 - 1) As Byte
'write the size into the structure
BitConverter.GetBytes(SIZEOF_PROCESSENTRY32).CopyT o(b, SIZE_OFFSET)
Dim h As Integer = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
Try
Dim rv As Integer = Process32First(h, b)
If rv <> 1 Then
Throw New Exception("Could not enumerte processes.")
End If
While rv = 1
Dim pid As Integer = BitConverter.ToInt32(b, PROCESS_OFFSET)
Dim parent As Integer = BitConverter.ToInt32(b, PARENT_OFFSET)
If pid = id Then
Return parent
End If
rv = Process32Next(h, b)
End While
Finally
CloseHandle(h)
End Try

Return -1

End Function

Then to use it

'get the target process id (here the current process)
Dim pid As Integer =
GetParentProcessID(System.Diagnostics.Process.GetC urrentProcess.Id)

'get the parent process
Dim parent As System.Diagnostics.Process =
System.Diagnostics.Process.GetProcessById(pid)
This information is also available from the performance counters under
Process>[Instance]>Creating Process ID,
but you must enumerate the instances and identify the target process by
Process>[Instance]>ID Process
which is slow.

David
Nov 18 '05 #3
I stand corrected! Should have known better.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uD**************@TK2MSFTNGP12.phx.gbl...

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
You don't.

--


Well you can. It's just not easy.

Here's how to do it in VB.
Declare Function CreateToolhelp32Snapshot _
Lib "KERNEL32.DLL" (ByVal dwFlags As Integer, _
ByVal th32ProcessID As Integer) As Integer
Declare Function Process32First Lib "KERNEL32.DLL" _
(ByVal hSnapshot As Integer, ByVal PE As Byte()) As Integer
Declare Function Process32Next Lib "KERNEL32.DLL" _
(ByVal hSnapshot As Integer, ByVal PE As Byte()) As Integer
Declare Function CloseHandle Lib "KERNEL32.DLL" _
(ByVal hObject As Integer) As Integer

Const TH32CS_SNAPPROCESS As Integer = 2
Const SIZEOF_PROCESSENTRY32 As Integer = 564
Const SIZE_OFFSET = 0
Const PROCESS_OFFSET = 8
Const PARENT_OFFSET = 24
'// typedef struct tagPROCESSENTRY32
'// {
'// DWORD dwSize;
'// DWORD cntUsage;
'// DWORD th32ProcessID;
'// DWORD th32DefaultHeapID;
'// DWORD th32ModuleID;
'// DWORD cntThreads;
'// DWORD th32ParentProcessID;
'// LONG pcPriClassBase;
'// DWORD dwFlags;
'// TCHAR szExeFile[MAX_PATH];
'// DWORD th32MemoryBase;
'// DWORD th32AccessKey;
'// } PROCESSENTRY32;

Function GetParentProcessID(ByVal id As Integer) As Integer
Dim b(564 - 1) As Byte
'write the size into the structure
BitConverter.GetBytes(SIZEOF_PROCESSENTRY32).CopyT o(b, SIZE_OFFSET)
Dim h As Integer = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
Try
Dim rv As Integer = Process32First(h, b)
If rv <> 1 Then
Throw New Exception("Could not enumerte processes.")
End If
While rv = 1
Dim pid As Integer = BitConverter.ToInt32(b, PROCESS_OFFSET)
Dim parent As Integer = BitConverter.ToInt32(b, PARENT_OFFSET)
If pid = id Then
Return parent
End If
rv = Process32Next(h, b)
End While
Finally
CloseHandle(h)
End Try

Return -1

End Function

Then to use it

'get the target process id (here the current process)
Dim pid As Integer =
GetParentProcessID(System.Diagnostics.Process.GetC urrentProcess.Id)

'get the parent process
Dim parent As System.Diagnostics.Process =
System.Diagnostics.Process.GetProcessById(pid)
This information is also available from the performance counters under
Process>[Instance]>Creating Process ID,
but you must enumerate the instances and identify the target process by
Process>[Instance]>ID Process
which is slow.

David

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wesley | last post by:
Hello, How do I pass object to the current running process? For example my assembly is called program.exe. Once program.exe is running sometime I want to call it again ie: program.exe /m...
1
by: Bob | last post by:
I'm looking at the docs in VS2005 regarding starter kits and I noticed that the starter kits to download from the site are in vsi files (compressed), all except the shareware starter kit. MS docs...
0
by: L. L. | last post by:
Hi All, I know using Process class you get the information of the current processes running on the current computer. Some processes are started by the user. And some processes are started by...
1
by: KiwiNET uk.geocities.com/haroonnet2002/ | last post by:
Hello folks, I don't know what they've done to the ordinary ASP newsgroup, I hope you can answer this question. I am trying to make this piece of code: If Len(Trim(primary))=0 Then...
3
by: Philip Wagenaar | last post by:
How can I get the output from a process.start. Example: Process.Start("c:\windows\system32\rp", PrintQueue & " " & filename, Username, pass, ComputerName) How do I get the output of this...
1
by: oldgent | last post by:
I am having a problem installing the starter kits. I have reinstalled VS 2005, think that might be the problem. I then installed both 'Personal Website" and the "Club Website" starter kits. I...
7
by: Mr. SweatyFinger | last post by:
I downloaded t he time tracker starter kit. Now where the hell is it? honest to god what is the problem. And no it is not under "n ew project/ starter kits" I mean how much of a hassle can...
6
by: =?Utf-8?B?UmljY2FyZG8=?= | last post by:
Hi everybody, I have a c# dll and I want to call the class library from several applications. I need some variables to be static in the memory space, not locally to each process. Thus, if...
1
by: Rick | last post by:
Can anyone tell me how to set the printer using the process class in VB.net, I've tried everything, it always goes to the default printer. My Code: Dim pathToExecutable As String =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.