简体   繁体   中英

Multiple CMD commands Managed c++ (or c#)

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM