none
How can a console application find out if it has been launched from the command prompt, or from another application? RRS feed

  • Question

  • Hello,
    I would like to ask, how a console application like myfirstapp.exe could find out, if it has been launched from the command prompt or if it has been called from another application like Word or Excel.

    Saturday, November 12, 2016 3:02 PM

All replies

  • There are Win32 APIs that will tell you the parent process ID. Search for "parent process C#" .

    David


    My Blog

    Saturday, November 12, 2016 3:09 PM
  • There are many ways.

    For example, with WMI

    Saturday, November 12, 2016 3:23 PM
  • Another way is to use GetConsoleProcessList

    It will tell you all of the processes that are sharing the console with your .exe. 

    Typically, if you are running from a command prompt there will be 2 processes in the list -- your .exe and cmd.exe. 

    If there is only 1 process (your .exe) using the console it is likely that program has not been started from a command prompt.

    Wednesday, November 16, 2016 4:39 PM
  • It is good to know that it could be done via WMI or an API.

    Up to now, no .NET class around, correct?

    Wednesday, November 16, 2016 10:03 PM
  • Hi Matjung,

    Thank you for posting here.

    >>Up to now, no .NET class around, correct?

    For .NET, there is no .NET class. If you want to use it, we could invoke in C#.

    For the invoke, you could refer to the link provide by Castorix31.

    using System;
    using System.Management;  // <=== Add Reference required!!
    using System.Diagnostics;
    
    class Program {
        public static void Main() {
            var myId = Process.GetCurrentProcess().Id;
            var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId);
            var search = new ManagementObjectSearcher("root\\CIMV2", query);
            var results = search.Get().GetEnumerator();
            results.MoveNext();
            var queryObj = results.Current;
            var parentId = (uint)queryObj["ParentProcessId"];
            var parent = Process.GetProcessById((int)parentId);
            Console.WriteLine("I was started by {0}", parent.ProcessName);
            Console.ReadLine();
        }
    }

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 21, 2016 10:15 AM
    Moderator
  • There is also the very simple approach of just handing the Application a commandline Argument to tell it "you have been started by another programm (that knew to give this argument)".

    If this is mostly about rights, a Manifest would propably help more then looking at "who started this"?
    Making Your Application UAC Aware | The Code King
    Note however that no mater how you plan to elevate, it can fail on the Windows side of things:
    "Run as Administrator" not Elevating

    You question if Word or Excel started your programm is extremly odd. Office programms should by default never start other programms. I am doubtfull that even plugins could do that. Even if it could, it would be very poor behavior of a plugin to do so.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Monday, November 21, 2016 4:11 PM