Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Process's "Command Line" and arguments from Process object?

Tags:

c#

winapi

In my Win7 Task Manager, there's a column that can be displayed called "Command Line" and will show exactly how the process was started and all the parameters issued. If I have a Process object for a currently running process that I did not start, how can I get that information? I had hoped that I could do something like p.StartInfo.Arguments but that's always coming back as an empty string. The entire StartInfo property seems empty, probably because I did not start the process I'm querying. I'm guessing that I'm going to have to use a WinAPI call.

like image 746
Corey Ogburn Avatar asked May 22 '13 21:05

Corey Ogburn


People also ask

How do I find command line arguments in Visual Studio?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line.

How do you separate command line arguments?

Each argument on the command line is separated by one or more spaces, and the operating system places each argument directly into its own null-terminated string. The second parameter passed to main() is an array of pointers to the character strings containing each argument (char *argv[]).

Where does command line arguments are get stored?

All the command line arguments are stored in a character pointer array called argv[ ].


1 Answers

Well you could use WMI, there is a class that could be queryied to retrieve the process list and each object contains also a property for the command line that started the process

string query = "SELECT Name, CommandLine, ProcessId, Caption, ExecutablePath " + 
               "FROM Win32_Process";
string wmiScope = @"\\your_computer_name\root\cimv2";
ManagementObjectSearcher searcher = new ManagementObjectSearcher (wmiScope, query);
foreach (ManagementObject mo in searcher.Get ()) 
{
    Console.WriteLine("Caption={0} CommandLine={1}", 
             mo["Caption"], mo["CommandLine"]);
}
like image 95
Steve Avatar answered Oct 27 '22 01:10

Steve