【问题标题】:Why can't I call the child process when I put it C:/Windows/System32/?为什么我把它放在C:/Windows/System32/时不能调用子进程?
【发布时间】:2018-11-26 11:39:59
【问题描述】:

我编写了一个显示 Hello World 的 Project.exe。编写主进程,创建Project.exe的子进程。但是我把Projetc.exe放到C:/Windows/System32/上时,无法正确创建Project.exe的子进程,但是放到其他目录下可以正常创建。

主程序如下:

#include <Windows.h>
#include <string.h>
#include <stdio.h>

int main() {
    STARTUPINFO si;
    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);

    PROCESS_INFORMATION pi;
    wchar_t *p = (wchar_t*)TEXT("C:/Windows/System32/Project1.exe");
    CreateProcess(p, 0, 0, 0, 0, 0, 0, 0, &si, &pi);

    DWORD CurId = GetCurrentProcessId();    //Get the ID of the current process
    DWORD Pid = pi.dwProcessId;             //ID of the created process
    DWORD Tid = pi.dwThreadId;              //The main thread ID of the created child process
    printf("the ID of the current process:  %d\nID of the created process:  %d\nThe main thread ID of the created child process  %d\n", CurId, Pid, Tid);

    WaitForSingleObject(pi.hProcess, -1);
}

运算结果为:

如果我把Project.exe放在E:/下,结果是

这是什么原因,需要怎么改才能在/System32/下成功运行?

【问题讨论】:

    标签: process operating-system


    【解决方案1】:

    一般的答案是,如果您想知道为什么调用没有按照您的预期进行,您应该首先查看返回状态以了解调用是否有效。

    CreateProcess 的文档告诉您它返回非零表示成功,返回零表示失败。您没有查看返回值,因此您的代码不知道成功或失败。

    此外,在失败时,GetLastError() 将返回失败的原因。这可能会告诉你发生了什么。

    简而言之,您需要“如果返回值为零,则打印 GetLastError() 的值并退出”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2011-07-19
      • 2021-02-01
      • 1970-01-01
      相关资源
      最近更新 更多