views:

250

answers:

3

We've got an interesting case where we are trying to determine how different instances of our app were launched. Is there any way for .NET to be able to query another running instance and get the command line parameters passed to that instance? I've not been able to find any way to do it in .NET so far, so I thought I'd check here to see if anyone had done anything like this before.

+2  A: 

Generally those variables are stored in the program's memory space, which you should (theoretically) should not be able to access.

You'll need to find out how to initiate interprocess communication with the other instances and trade data. Named pipes are one good option, but you might want to start a new stackoverflow question to get good options on this.

Adam Davis
+4  A: 

You can retrieve this information through WMI.

See the Win32_Process class, in particular its command line property. This Code Project article provides pointers on how to do this,

Rob Walker
A: 

For future reference, here is a code snippet from how I got it to work. This was just for a test to see how it all worked. The actual implemented code parses the command line parameters for what we need.

            try
            {
                ManagementScope connectScope = new ManagementScope();
                connectScope.Path = new ManagementPath(@"\\" + Environment.MachineName + @"\root\CIMV2");

                SelectQuery msQuery = new SelectQuery("SELECT * FROM Win32_Process Where Name = '" + "PROGRAMNAMEHERE.exe" + "'");
                ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(connectScope, msQuery);

                foreach (ManagementObject item in searchProcedure.Get())
                {
                     try 
                     {
                         MessageBox.Show(item["CommandLine"].ToString()); 
                     }
                     catch (SystemException) 
                     {

                     }
                }
            }
Noah