3

Hello i have to write program which have to open a few system properties like:

Process sound = new Process();
sound.StartInfo.FileName = "mmsys.cpl";
sound.Start();

// Place 1

Process device = new Process();
device.StartInfo.FileName = "hdwwiz.cpl";
device.Start();

// Place 2

// Other Code doing sth

And that works well, but my problem is that i must on first run mmsys.cpl, wait for user check what he must to do, and after closing window run hdwwiz.cpl.

So in // Place 1 i wrote:

sound.WaitForExit();

But that dont works, because mmsys.cpl is only shortcut and run as process "explorer.exe", and hdwwiz.cpl runs as "mmc.exe", so that comand doesnt wait till closing that windows, and run both at once.

Is any way to make sth like i want ?

Maciek
  • 523
  • 1
  • 4
  • 8

2 Answers2

1

What you are running are called control panel files. Legacy ones, like mmsys.cpl are hosted in a processed with the name rundll32.exe You could get poll the list of these processes using:

Process[] processes = Process.GetProcessessByName("rundll32.exe");

and call WaitForExit on these Process objects. You can do the same for newer mmc.exe based .CPL's.

Obviously, this is not a great solution if there are multiple control panel applets running at the same time. You could perhaps put up a warning to the user close the other ones in this case.

Other more complicated solutions might involve using pinvoke to call the Win32 function EnumWindows to get a list of top level windows and hook them to watch for the WM_CLOSE message.

jlyonsmith
  • 873
  • 11
  • 15
  • works almost perfect for `mmc`, but when i start process via `devmgmt.msc` system run one instance of mmc and very fast restart to another instance with other pid. I found solution that run process via `hdwwiz.cpl` and with help in your answer wait till system create process with `mmc`in name :) and that work correctly. _Thanks_ – Maciek Jul 06 '13 at 22:36
0

Ok for properties like mmsys.cpl solution is:

Process sound = new Process();
sound.StartInfo.FileName = "rundll32.exe";
sound.StartInfo.Arguments = "shell32.dll,Control_RunDLL mmsys.cpl";
sound.Start();

sound.WaitForExit();

because that windows is runs via rundll32 command not explorer.exe, so i can check that process.

But still dont know how to open device manager. i found 2 commands:

  • devmgmt.msc
  • hdwwiz.cpl

but both run and create second process which i cannot track. Any solution for that ?

Maciek
  • 523
  • 1
  • 4
  • 8