尝试使用 CreateProcess 时发生转换错误 Windows API

Conversion error while trying to use CreateProcess Windows API

我正在尝试 运行 创建进程的 C++ 程序:

int main()
{
    HANDLE hProcess;
    HANDLE hThread;
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    DWORD dwProcessId = 0;
    DWORD dwThreadId = 0;
    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&pi, sizeof(pi));
    BOOL bCreateProcess = NULL;
    
    bCreateProcess = CreateProcessW(L"C:\Windows\System32\notepad.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
                            
    if (bCreateProcess == FALSE) {
        cout << "Create Process Failed & Error No. " << GetLastError() << endl;
    }
    
    cout << "Process Creation Successful!" << endl;
    
    cout << "Process ID: " << pi.dwProcessId << endl;
    cout << "Thread ID: " << pi.dwThreadId << endl;
    cout << "GetProcessID: " << GetProcessId(pi.hProcess) << endl;
    cout << "GetThreadID: " << GetThreadId(pi.hThread) << endl;
    
    WaitForSingleObject(pi.hProcess, INFINITE);
    
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);

    return 0;
}

但是,我returns出现如下错误:

main.cpp(19): error C2664: 'BOOL CreateProcessW(LPCWSTR,LPWSTR,LPSECURITY_ATTRIBUTES,LPSECURITY_ATTRIBUTES,BOOL,DWORD,LPVOID,LPCWSTR,LPSTARTUPINFOW,LPPROCESS_INFORMATION)': cannot convert argument 9 from 'LPSTARTUPINFOW *' to 'LPSTARTUPINFOW'
main.cpp(27): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files (x86)\Windows Kits\include.0.20348.0\um\processthreadsapi.h(372): note: see declaration of 'CreateProcessW'

我正在 Windows 10 上使用以下命令编译它:

cl.exe /EHsc main.cpp

此外,如果可以的话,请推荐一些Windows系统编程的教程(课程、书籍、youtube 频道、博客等)。

您似乎是用 Ansi 字符串而不是宽字符串进行编译,所以 STARTUPINFO 变成了 STARTUPINFOA,这与 CreateProcessW() 不兼容。

当我编译您的代码时,出现错误 cannot convert argument 9 from 'STARTUPINFO *' to 'LPSTARTUPINFOW',这比您发布的错误更有意义。

要么更改编译器选项,要么在代码中写入 STARTUPINFOW si;