0

Hey im just wondering would this work for running multiple CMD commands? I have not tested this yet.

//multiple commands
System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process();
System::Diagnostics::ProcessStartInfo ^startInfo = gcnew System::Diagnostics::ProcessStartInfo();
//startInfo->WindowStyle = System::Diagnostics::ProcessWindowStyle::Hidden;
startInfo->FileName = "cmd.exe";
startInfo->Arguments = "/C powercfg -attributes SUB_PROCESSOR 12a0ab44-fe28-4fa9-b3bd-4b64f44960a6 -ATTRIB_HIDE";
startInfo->Arguments = "/C powercfg -attributes SUB_PROCESSOR 40fbefc7-2e9d-4d25-a185-0cfd8574bac6 -ATTRIB_HIDE";
process->StartInfo = startInfo;
process->Start();

Or does startInfo only work with one argument at a time? If so how would I execute multiple commands without making a .bat file and executing that.

Joey
  • 344,408
  • 85
  • 689
  • 683
cox
  • 1
  • 1
  • 1
    Unrelated, but I always wonder how people can drag typos in variable names through multiple lines of code without even noticing ... – Joey Jul 18 '11 at 09:26
  • @Joey, very easily, when using IntelliSense. It also doesn't help if English isn't your native language. – svick Jul 22 '11 at 00:36
  • svick: Ah, I forgot IntelliSense; typing most of my code in a text editor nowadays. – Joey Jul 22 '11 at 07:52

2 Answers2

0

The code you wrote does what it appears to do. It first sets Arguments to one value and then overwrites that with another value. So the Start() executes only the second command.

I would recommend creating a helper function (or method):

void RunPowerCfg(System::String ^id)
{
    System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process();
    System::Diagnostics::ProcessStartInfo ^startInfo =
        gcnew System::Diagnostics::ProcessStartInfo();
    startInfo->FileName = "cmd.exe";
    startInfo->Arguments = System::String::Format(
        "/C powercfg -attributes SUB_PROCESSOR {0} -ATTRIB_HIDE", id);
    process->StartInfo = startInfo;
    process->Start();
}

void main()
{
    RunPowerCfg("12a0ab44-fe28-4fa9-b3bd-4b64f44960a6");
    RunPowerCfg("40fbefc7-2e9d-4d25-a185-0cfd8574bac6");
}

Depending on what you want to do, you might want to call process->WaitForExit() after you start it.

svick
  • 236,525
  • 50
  • 385
  • 514
0

This won't work. This code:

stratInfo->Arguments = "/C powercfg -attributes SUB_PROCESSOR 12a0ab44-fe28-4fa9-b3bd-4b64f44960a6 -ATTRIB_HIDE";
stratInfo->Arguments = "/C powercfg -attributes SUB_PROCESSOR 40fbefc7-2e9d-4d25-a185-0cfd8574bac6 -ATTRIB_HIDE";

doesn't set two arguments. It sets the argument string, and then overwrites it.

If you want to run this twice, you'll have to do something like:

void RunProc(System::String ^arguments)
{
    System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process();
    System::Diagnostics::ProcessStartInfo ^startInfo = gcnew System::Diagnostics::ProcessStartInfo();
    startInfo->FileName = "cmd.exe";
    startInfo->Arguments = arguments;
    process->StartInfo = startInfo;
    process->Start();

}

RunProc("/C powercfg -attributes SUB_PROCESSOR 12a0ab44-fe28-4fa9-b3bd-4b64f44960a6 -ATTRIB_HIDE");
RunProc("/C powercfg -attributes SUB_PROCESSOR 40fbefc7-2e9d-4d25-a185-0cfd8574bac6 -ATTRIB_HIDE");

Of course, you'll want to add error handling etc to this, especially for the case where the current process doesn't have the right permissions.

Ben
  • 6,023
  • 1
  • 25
  • 40
  • Ah ok this makes more sense, as for error handling in this case do you mean asking for the user to allow these commands to run? (admin rights?) – cox Jul 18 '11 at 01:11
  • For error handling, I mean wrapping the call to `process->Start()` in a try/catch block and, depending on what happens, informing the user to run the application as an administrator. You can learn more about running with admin privileges here: http://stackoverflow.com/questions/6412896/giving-application-elevated-uac – Ben Jul 18 '11 at 01:47
  • If this answers your question, don't forget to accept it! =) – Ben Jul 18 '11 at 01:47