简体   繁体   中英

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

您需要将STARTUPINFO归零。

ZeroMemory(&si, sizeof(si));

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. Eg 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");

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