Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process started by Process.start() returns incorrect process ID?

Tags:

I am starting an executable using this code:

Process proc = new Process(); proc.StartInfo.FileName = executablePath; proc.Start(); proc.WaitForInputIdle(); 

after this calling proc.Id it gives me some integer, which is not real process ID. In the task manager there is another ID for this process and also I am using MS UI Automation to access this application, which also returns the same ID as in task manager. So my question is how can I get the real process ID of started process?

UPDATE

I found out that on Windows 7 it works fine and returns me the right ID, but not on Windows XP. What can be the reason?

SCENARIO

The scenario of the application is the following. I have a running embedded HTTP server, which is implemented not by me, (here is the source). The client connects to the web server and sends a request to run a program. In the request handler of my server I am just using Process.start() to start the requested application. As a web server the program creates threads for every client session connected to it (I assume so, as I didn't wrote it). Can this somehow help to identify the problem as it exists only on Windows XP X86 Service Pack 3?

like image 270
haynar Avatar asked Oct 15 '12 08:10

haynar


People also ask

How do I start a process in VB net?

Start another application using your . NET code As a . NET method, Start has a series of overloads, which are different sets of parameters that determine exactly what the method does. The overloads let you specify just about any set of parameters that you might want to pass to another process when it starts.

How does process start work?

Start(String, String) Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component.

What is process in C#?

C# Process class provides Start method for launching an exe from code. The Process class is in the System. Diagnostics namespace that has methods to run a .exe file to see any document or a webpage. The Process class provides Start methods for launching another application in the C# Programming.


1 Answers

An example of how I did it:

    bool started = false;     var p = new Process();      p.StartInfo.FileName = "notepad.exe";      started = p.Start();      try {       var procId = p.Id;       Console.WriteLine("ID: " + procId);     }     catch(InvalidOperationException)     {         started = false;     }     catch(Exception ex)     {         started = false;     } 

Otherwise, try using handles like this:
Using handlers
Getting handler

hWnd = (int) process.MainWindowHandle; int processId; GetWindowThreadProcessId(hWnd, out processId);  [DllImport("user32")] static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId); 

Side note:
What happens if you get the array of process and iterate over them and compare the PIDs?

Process[] p = Process.GetProcessesByName( "testprogram" ); foreach(var proc in p)     Console.WriteLine("Found: "+proc.Id == myExpectedProcId); 
like image 87
Marcus Avatar answered Oct 01 '22 02:10

Marcus