hello i want to get startet with programming with WIN32, therefore i wrote a programm that creates a process but in the line of code where i create the process the programm gets an error an dosn’t work (abend). i don’t know if the code in programm 1 is wrong or the code in the second programm that should be created by the first. ( I don’t know if the code in the first programm after “createprocess” is right because i didn’t get further with debugging, because in this line i get the error.(i tested it without the cout,waitforobject and close handle but i didn’t work either )).
First Programm:
#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 Programm:
#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";
}
Thanks in advance
Notice the type of the
lpCommandLineparameter toCreateProcess— it isLPTSTR, notLPCTSTR, i.e. it is notconst.This means that
CreateProcessreserves the right to actually modify the contents oflpCommandLine. However, you have provided a pointer to a string literal as parameter, and string literals are immutable (they come from your program’s read-only data segment and attempts to alter them will typically result in an access violation error.)To fix this, simply change your code not to use an immutable string literal:
Interestingly enough,
CreateProcessW(UNICODE) attempts to write tolpCommandLinewhereasCreateProcessA(ANSI) does not, and surprise — your first program is built as UNICODE (were you to build it as ANSI it would work out of the box, at least on Windows XP.)I can confirm that, with the above modification, your code works.
Also note that:
D:\\betriebssystemePRA1PRO2.exe‘s window title, position etc. you do not need to supply aSTARTUPINFOstructure at all, you can simply passlpStartupInfoasNULLand a default will be usedWaitForSingleObjecton a closed handle