【问题标题】:Error events when CreateProcess uses a path: E0167 argument of type "char *" is incompatible with parameter of type "LPWSTR"CreateProcess 使用路径时的错误事件:“char *”类型的 E0167 参数与“LPWSTR”类型的参数不兼容
【发布时间】:2019-04-23 16:47:45
【问题描述】:

当我在 C++ VisualStudio 2017 中使用 CreateProcess 命令时,我给出了关于 LPWSTR 的错误: E0167 “char *”类型的参数与“LPWSTR”类型的参数不兼容。

我该如何解决? 下面的代码是我关于该问题的代码的一部分。 感谢您的任何建议。

int main()
{
...
    ConnectToEngine("stockfish.exe");
...
}


void ConnectToEngine(char* path)
{
    pipin_w = pipin_r = pipout_w = pipout_r = NULL;
    sats.nLength = sizeof(sats);
    sats.bInheritHandle = TRUE;
    sats.lpSecurityDescriptor = NULL;

    CreatePipe(&pipout_r, &pipout_w, &sats, 0);
    CreatePipe(&pipin_r, &pipin_w, &sats, 0);

    sti.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    sti.wShowWindow = SW_HIDE;
    sti.hStdInput = pipin_r;
    sti.hStdOutput = pipout_w;
    sti.hStdError = pipout_w;

    CreateProcess(NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &sti, &pi);
}

【问题讨论】:

  • 你需要一个LPWSTR的参数而不是char*,错误信息有什么不清楚的地方?
  • 如您在此处看到的docs.microsoft.com/en-us/windows/desktop/Intl/… LPWSTRtypedefunsigned wchar_t*,而不是char*
  • 我做到了:char* exename[] = "stockfish.exe"; ConnectToEngine("exename");但它不起作用。错误:E0520 初始化,聚合对象需要“{...}”
  • @nanosi 看看LPWSTR的定义,不是char*,对应的原始字符串字面量是L前缀。
  • 这与您在项目设置中启用UNICODE 有关。话虽如此,您很可能想要进行@πάνταῥεῖ 告诉您的修复。也使用wchar_t* 而不是char*

标签: c++ winapi createprocess


【解决方案1】:

上面的朋友的一些笔记解决了这个问题。 工作代码如下:

    int main()
   {
    ...
    wchar_t a[] = L"stockfish.exe";
    ConnectToEngine(a);
    ...
   }


void ConnectToEngine(WCHAR* path)
{
    pipin_w = pipin_r = pipout_w = pipout_r = NULL;
    sats.nLength = sizeof(sats);
    sats.bInheritHandle = TRUE;
    sats.lpSecurityDescriptor = NULL;

    CreatePipe(&pipout_r, &pipout_w, &sats, 0);
    CreatePipe(&pipin_r, &pipin_w, &sats, 0);

    sti.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    sti.wShowWindow = SW_HIDE;
    sti.hStdInput = pipin_r;
    sti.hStdOutput = pipout_w;
    sti.hStdError = pipout_w;

    CreateProcess(NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &sti, &pi);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2021-03-23
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多