Beginner C++ CreateProcess () Error 2

Beginner C++ CreateProcess () Error 2

本文关键字:Error C++ CreateProcess Beginner      更新时间:2023-10-16

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

    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));
    String pathexe = "C:Windowssystem32cmd.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)我在论坛上读到过,找不到解决这个错误的方法,类似的问题有很多答案,但没有解决这个错误,提出了其他功能,或者与route命令混合。3)我认为这个问题不允许,因为我在舱单上建造。4)我目前使用c++ Builder,在win7, 32位,但不重要。5)我猜这个问题将被投票为负面和重复(像往常一样),但提议的测试示例也会收到错误。Thanks to all

第一个结论:

错误2:The system cannot find The file specified.

链接功能:https://msdn.microsoft.com/es-es/library/windows/desktop/ms679360(v=vs.85).aspx链接错误:https://msdn.microsoft.com/es-es/library/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

THANKS TO ALL

您正在尝试运行这个命令:

cmd notepad

(你也做得不对;lpCommandLine参数应该包括整个字符串,而不仅仅是notepad,并且您没有正确地引用反斜杠。

但是即使你解决了这些问题,它也不会工作,因为你的语法是错误的。你会发现如果在命令行上输入它也不会起作用!

相反,尝试:

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

/c选项表示"执行此命令"。如果您希望命令窗口在命令完成后保持打开状态,则可以使用/k,尽管程序很少这样做。

最后一个注意事项:我在这里假设notepad只是一个更复杂的命令的替代品。如果你真的想运行记事本,或任何其他可执行文件,你不应该调用cmd.exe:

String command= "notepad";
if(!CreateProcess(
        NULL,  // lpApplicationName
        command.c_str(),  // lpCommandLine
        ...

只有在需要运行内置命令或复合命令行时才需要调用cmd.exe

(实际上,即使在这些情况下,调用cmd.exe也被认为是不好的做法;在Windows中,通常期望您通过API为自己做这类事情,而不是将工作外包给命令解释器。但也有一些极端情况,您的情况可能会有所不同。

相关文章: