0

我正在尝试创建一个向 cmd.exe 发送命令并接收错误 2 的进程,为什么?有可能吗?如何?

    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    String pathexe = "C:\Windows\system32\cmd.exe";
    String command= "notepad.exe";

    if(!CreateProcess(
            pathexe.c_str(),  // lpApplicationName
            command.c_str(),  // lpCommandLine
            NULL,   // lpProcessAttributes
            NULL,   // lpThreadAttributes
            FALSE,  // bInheritHandles
            0,      // dwCreationFlags
            NULL,   // lpEnvironment
            NULL,   // lpCurrentDirectory
            &si,    // lpStartupInfo
            &pi     // lpProcessInformation
            ))
    {
        AnsiString error = GetLastError();
        ShowMessage("Error: " + error);
    }
    WaitForSingleObject( pi.hProcess, INFINITE );
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

PD:1)假设您可以为此目的使用CreateProcess(),我不应该使用ShellExecute()或system()来做。2)我在论坛上看过,找不到解决这个错误的办法,类似问题有很多答案但没有解决错误,建议其他功能,或者与路由命令混合使用。3)我不认为该问题允许,因为我在清单时构建。4) 我目前使用 C++ Builder,在 win7 中,32 位但不重要。5)我猜这个问题将被投票为否定和重复(像往常一样),但建议的测试示例也会收到错误。谢谢大家

第一个结论:

错误2:系统找不到指定的文件。

链接功能:https ://msdn.microsoft.com/es-es/library/windows/desktop/ms679360(v=vs.85).aspx 链接错误:https ://msdn.microsoft.com/es-es/库/windows/desktop/ms681382(v=vs.85).aspx

错误 2:检查语法、文件路径和存在。

作品:

STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    String command = "notepad.exe";

    if(!CreateProcess(
            NULL,   // lpApplicationName
            commmand.c_str(), // lpCommandLine
            NULL,   // lpProcessAttributes
            NULL,   // lpThreadAttributes
            FALSE,  // bInheritHandles
            0,      // dwCreationFlags
            NULL,   // lpEnvironment
            NULL,   // lpCurrentDirectory
            &si,    // lpStartupInfo
            &pi     // lpProcessInformation
            ))
    {
        AnsiString error = GetLastError();
        ShowMessage("Error: " + error);
    }
    WaitForSingleObject( pi.hProcess, INFINITE );
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

此示例也适用于 exe

String command = "cd C:\\sample\\calc.exe";

但是没有 cmd 的一般命令,必须有一种方法可以将命令发送到 cmd,如下所示:

notepad.exe && cd C:\sample\ && sample1.txt

谢谢大家

4

1 回答 1

1

You're trying to run this command:

cmd notepad

(You aren't doing that quite right, either; the lpCommandLine argument should include the entire string, not just notepad, and you haven't quoted the backslashes properly.)

But even once you fix those problems, it won't work, because you've got the syntax wrong. You'll find it won't work if typed on the command line either!

Instead, try:

String pathexe = "C:\\Windows\\system32\\cmd.exe";
String command= "cmd /c notepad.exe";

The /c option means "run this command". You can use /k instead if you want the command window to stay open after the command has finished, though it's unusual for a program to do so.

One final note: I'm assuming here than notepad is just a stand-in for a more complicated command. If you actually want to run notepad, or any other executable, you shouldn't be invoking cmd.exe at all:

String command= "notepad";

if(!CreateProcess(
        NULL,  // lpApplicationName
        command.c_str(),  // lpCommandLine
        ...

You only need to call on cmd.exe if you need to run a built-in command, or a composite command line.

(Actually, calling cmd.exe is considered poor practice even in those cases; in Windows, you are generally expected do that sort of thing for yourself via the API rather than farming out the job to the command interpreter. But there are edge cases, and your mileage may vary.)

于 2015-10-03T22:23:39.630 回答