首页 > 解决方案 > CreateProcess 使用路径时的错误事件:“char *”类型的 E0167 参数与“LPWSTR”类型的参数不兼容

问题描述

当我在 C++ VisualStudio 2017 中使用 CreateProcess 命令时,出现关于 LPWSTR 的错误:“char *”类型的 E0167 参数与“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);
}

标签: c++winapicreateprocess

解决方案


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

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


推荐阅读