Beginner C++ CreateProcess () Error 2

11.3k Views Asked by At

I am trying to create a process that send command to cmd.exe and receive Error 2, Why? It's posible? How?

    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) It is assumed that you can use CreateProcess () for this purpose, I should not do it with ShellExecute () or system(). 2) I have read about it in the forum and can not find a solution to this error, there are many answers to similar questions but do not address the error, other functions are proposed, or mix with the route command. 3) I do not think that issue permits because I built while the manifest. 4) I currently use C ++ Builder, in win7, 32bits but not important. 5) I guess the issue will be voted as negative and duplicate (as usual) but the proposed testing examples also receive errors. Thanks to all

FIRST CONCLUSIONS:

Error 2: The system cannot find the file specified.

Link funtion: https://msdn.microsoft.com/es-es/library/windows/desktop/ms679360(v=vs.85).aspx Link error: https://msdn.microsoft.com/es-es/library/windows/desktop/ms681382(v=vs.85).aspx

With error 2: check syntax, file path and existence.

works:

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 );

This example works also for exe

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

But no with cmd´s general commands, there must be a way to send commands to cmd as:

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

THANKS TO ALL

1

There are 1 best solutions below

0
Harry Johnston On

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.)