我正在嘗試運行一個創建行程的 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;
}
但是,我回傳以下錯誤:
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\10\include\10.0.20348.0\um\processthreadsapi.h(372): note: see declaration of 'CreateProcessW'
我正在使用以下命令在 Windows 10 上編譯它:
cl.exe /EHsc main.cpp
此外,如果您可以推薦一些 Windows 系統編程的教程(課程、書籍、YouTube 頻道、博客等)。
uj5u.com熱心網友回復:
似乎您正在使用 Ansi 字串而不是 Wide 字串進行編譯,因此STARTUPINFO變得與 .STARTUPINFOA不兼容CreateProcessW()。
當我編譯你的代碼時,我得到 error cannot convert argument 9 from 'STARTUPINFO *' to 'LPSTARTUPINFOW',這比你發布的錯誤更有意義。
更改編譯器選項或撰寫STARTUPINFOW si;代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/432445.html
