Take the 2-minute tour ×

I am developing console application in .Net and I want to change a behavior a little based on information that application was started from cmd.exe or from explorer.exe. Is it possible?

share|improve this question
add comment

3 Answers

up vote 9 down vote accepted
Process this_process = Process.GetCurrentProcess();
int parent_pid = 0;
using (ManagementObject MgmtObj = new ManagementObject("win32_process.handle='" + this_process.Id.ToString() + "'"))
    parent_pid = Convert.ToInt32(MgmtObj["ParentProcessId"]);
string parent_process_name = Process.GetProcessById(parent_pid).ProcessName;
share|improve this answer
Please check Adam Mitz comment bellow. I still found this answer the best because of simplicity. However someone should concern. –  Jakub Šturc Sep 10 '08 at 10:01
add comment

One issue with the ToolHelp/ManagementObject approaches is that the parent process could already have exited.

The GetStartupInfo Win32 function (use PInvoke if there's no .NET equivalent) fills in a structure that includes the window title. For a Win32 console application "app.exe", this title string is "app" when started from cmd and "c:\full\path\to\app.exe" when started from explorer (or the VS debugger).

Of course this is a hack (subject to change in other versions, etc.).

#include <windows.h>
int main()
  MessageBox(NULL, si.lpTitle, NULL, MB_OK);
  return 0;
share|improve this answer
add comment

The CreateToolhelp32Snapshot Function has a Process32First method that will allow you to read a PROCESSENTRY32 Structure. The structure has a property that will get you the information you want:

th32ParentProcessID - The identifier of the process that created this process (its parent process).

This article will help you get started using the ToolHelpSnapshot function:


share|improve this answer
add comment

Your Answer


