[英]error by creating process
您好我想通過WIN32編程獲得startet,因此我編寫了一個創建進程的程序,但在代碼行中,我創建了一個程序,程序得到一個錯誤,不能正常工作(abend)。 我不知道程序1中的代碼是錯誤的還是第二個程序中的代碼應該由第一個程序創建。 (我不知道“createprocess”之后的第一個程序中的代碼是否正確,因為我沒有進一步調試,因為在這行中我得到了錯誤。(我測試了它沒有cout,waitforobject和關閉句柄但我也沒有工作))。
第一個程序:
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
void main()
{
bool ret;
bool retwait;
STARTUPINFO startupinfo;
GetStartupInfo (&startupinfo);
PROCESS_INFORMATION pro2info;
ret = CreateProcess(NULL, L"D:\\betriebssystemePRA1PRO2.exe", NULL, NULL, false, CREATE_NEW_CONSOLE, NULL,
NULL, &startupinfo, &pro2info);
cout<<"hProcess: "<<pro2info.hProcess<<endl;
cout<<"dwProcessId: "<<pro2info.dwProcessId <<endl;
retwait= WaitForSingleObject (pro2info.hProcess, 100);
retwait= WaitForSingleObject (pro2info.hProcess, 100);
CloseHandle (pro2info.hProcess);//prozesshandle schließen
retwait= WaitForSingleObject (pro2info.hProcess, 100);
ExitProcess(0);
}
Seconde計划:
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
void main()
{
int b;
b=GetCurrentProcessId();
cout<<b<<endl;
cout<<"Druecken Sie Enter zum Beenden"<<endl;
cin.get();
//warten bis Benutzer bestätigt
Sleep (700);
ExitProcess(0);
cout<<"test";
}
提前致謝
注意CreateProcess
的lpCommandLine
參數的類型 - 它是LPTSTR
,而不是LPCTSTR
,即它不是 const
。
這意味着CreateProcess
保留實際修改 lpCommandLine
內容的lpCommandLine
。 但是,您提供了一個指向字符串文字的指針作為參數,字符串文字是不可變的(它們來自程序的只讀數據段,嘗試更改它們通常會導致訪問沖突錯誤。)
要解決此問題,只需更改代碼, 不要使用不可變的字符串文字 :
wchar_t wcsCommandLine[] = L"D:\\betriebssystemePRA1PRO2.exe";
ret = CreateProcess(NULL, wcsCommandLine, NULL, NULL, ...
有趣的是, CreateProcessW
(UNICODE)嘗試寫入lpCommandLine
而CreateProcessA
(ANSI)沒有,並且令人驚訝 - 您的第一個程序被構建為UNICODE(您是否將其構建為ANSI,它可以開箱即用,至少在Windows XP。)
我可以確認,通過上述修改,您的代碼可以正常工作。
另請注意:
D:\\\\betriebssystemePRA1PRO2.exe
的窗口標題,位置等,您根本不需要提供STARTUPINFO
結構 ,您只需將lpStartupInfo
傳遞為NULL
並使用默認值 WaitForSingleObject
您必須設置startupinfo
結構的大小:
startupinfo.cb = sizeof(startupinfo);
也許這就是CreateProcess
失敗的原因。
順便說一下 - 你為什么要調用GetStartupInfo
? 您應該將startupinfo
的內存清零(除了如上所述設置大小)。
在這里查看示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.