0

In my hello world console app, Process.GetCurrentProcess().Id property returns a different value from the Id for the Console window created to display the app's stdout etc.

How do I get the process id for the console window specifically?

I cycle through the processes in Process.GetProcesses() and check for the console window based on window title. When it finds it, I print it's process id out and it's different to what is returned from the GetCurrentProcess() call. So I'm concluding the console app process and the console window are two different processes, maybe the console window is a child process of my console app, or maybe it's a peculiarity associated with running a console app from within Visual Studio.

        Process[] processlist = Process.GetProcesses();
        int origProcessId = Process.GetCurrentProcess().Id;
        foreach ( Process p in processlist)
        {
            // get all window handles of title 'C:\Windows\system32\cmd.exe
            if (!String.IsNullOrEmpty(p.MainWindowTitle) && p.MainWindowTitle.IndexOf("C:\\Windows\\system32\\cmd.exe") == 0 )
            {
                    Console.WriteLine("Gets here ok, once & only once");
                    if(origProcessId == p.Id){
                                Console.WriteLine("Process: {0}", p.Id); // doesn't get here!!!
                    }
            }
        }
user1561108
  • 2,666
  • 9
  • 44
  • 69
  • 1
    Can you add more details? I have a hard time understanding what you mean. – nvoigt Jun 21 '15 at 20:49
  • why on earth should this be closed? – user1561108 Jun 21 '15 at 21:16
  • To me, it's still unclear what you actually want. Did you start a second process? What does your first process look like? Why are you looking for cmd.exe? Can you write a [Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve)? – nvoigt Jun 21 '15 at 21:35
  • Done - I think it's better to ask for more info rather than just close the question. Sometimes I think the gamification here really doesn't help the conversation. – user1561108 Jun 21 '15 at 21:39
  • I am looking for cmd.exe because that is the default title of windows console applications. – user1561108 Jun 21 '15 at 21:40
  • Why do you think you should find a process that has your process id but is named cmd.exe? Did you print out all the process names? – nvoigt Jun 21 '15 at 21:40
  • The text I am searching for is an exact match on the console window title that my app spawns when run from Visual Studio. I feel like there is something important I'm missing here? – user1561108 Jun 21 '15 at 21:42
  • My console applications have different window titles. Maybe you should check you aren't finding one of your other console windows. Anyway, you migh be better off asking how to achieve something. Finding your own process when you could call GetCurrentProcess is probably not your actual question. – nvoigt Jun 21 '15 at 21:48
  • It is just broken code, your console window title is not "cmd.exe". Use `if (p.MainWindowTitle == Console.Title) ` instead. If you want to get the window handle then pinvoke GetConsoleWindow(). – Hans Passant Jun 21 '15 at 21:51
  • I am finding the only command line window open. GetCurrentProcess.Id is not the process Id of the console window. – user1561108 Jun 21 '15 at 21:55
  • http://imgur.com/Y9bwLdh.jpg 'just broken code'... have you tried running it? – user1561108 Jun 21 '15 at 21:58

1 Answers1

3

I think it would be useful for us to know why you need the process id. The problem is that there are multiple ways that your application can be launched and every one of them will look a little differently:

In Visual Studio, Run with debugging:

This will make you application run in a single process. The MainWindowTitle will look similar to the following:

file://C:\...\ConsoleApplication.exe

In Visual Studio, Run without debugging:

This will start cmd.exe and have that start your application. So, your application will be a separate process from cmd.exe and will have no MainWindowTitle (because it has no window). You can see the process running as a child of cmd.exe in Process Explorer:

enter image description here

Without Visual Studio:

When double-clicking the exe of your application, you'll get a single process whose MainWindowTitle will be the path to your exe (so the same thing as the first case but without the file://). You can also get it running like this when debugging with VS if you uncheck "Enable the Visual Studio hosting process" in your project's Debug options.

Without Visual Studio, using command line

This will give you exactly the same result as VS's "Run without debugging" option.

I think the important message here is: don't use MainWindowTitle to find your application. Process.GetCurrentProcess() will always give you the current process id.

If, for some reason, you want to find the parent process, I suggest looking at this question. I think you should clarify: why do you need to find the process id? What do you want to do with it?

Community
  • 1
  • 1
vesan
  • 3,289
  • 22
  • 35