Eduard Luca
Eduard Luca

Reputation: 6602

CreateProcess not working

I am having problems with getting the following code to work in C++ (VC++ console app). It simply doesn't create the process, but it prints out the error text.

static void main(){
    char *hotkeyexe = "cmd";
    PROCESS_INFORMATION pi;
    STARTUPINFO si;

    if(!CreateProcess(hotkeyexe, "", 0, 0, 0, 0, 0, 0, &si, &pi))
        printf("error");
        scanf("%d");
    }
}

Upvotes: 0

Views: 4214

Answers (2)

mkaes
mkaes

Reputation: 14119

I quote from the MSDN:

The function will not use the search path. This parameter must include the file name extension; no default extension is assumed.

So you cannot just use cmd. It will depend on your working dir if it will work. If you use a full path it will work. E.g this is a working example on my machine.

char *hotkeyexe = "c:\\Windows\\notepad.exe";
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));

if(!CreateProcess(hotkeyexe, "", 0, 0, 0, 0, 0, 0, &si, &pi))
    printf("error");
scanf("%d");

Upvotes: 1

Joshua
Joshua

Reputation: 43188

You need to zero out STARTUPINFO.

ZeroMemory(&si, sizeof(si));

Upvotes: 3

Related Questions