Error creating process

Hi, I want to get a startet with programming using WIN32, so I wrote a program that creates a process, but in the line of code where I create the process, the program gets an error, it doesn't work (abend). I do not know if the code in program 1 is incorrect or the code in the second program that must be created first. (I don’t know if the code in the first program after "createprocess" is correct, because I didn’t debug further, because I get an error in this line. (I tested it without cout, waitforobject and close handle but I didn’t work either )).

First program:

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

The second program:

 #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

+4
source share
2 answers

Note the type of the lpCommandLine on CreateProcess - this is LPTSTR , not LPCTSTR , i.e. not const .

This means that CreateProcess reserves the right to modify the contents of lpCommandLine . However, you pointed to the string literal as a parameter, and the string literals are immutable (they come from your data segment as a read-only program and attempts to modify them usually result in an access violation error.)

To fix this, just change your code so that you don't use an immutable string literal :

 wchar_t wcsCommandLine[] = L"D:\\betriebssystemePRA1PRO2.exe"; ret = CreateProcess(NULL, wcsCommandLine, NULL, NULL, ... 

Interestingly, CreateProcessW (UNICODE) tries to write to lpCommandLine , while CreateProcessA (ANSI) does not do this and it is surprising - your first program was built as UNICODE (you had to build it as ANSI, it worked out of the box, at least on Windows XP.)

I can confirm that your code works with this modification.

Also note:

  • if you do not need to specify the window title D:\\betriebssystemePRA1PRO2.exe , position, etc., you do not need to create the STARTUPINFO structure STARTUPINFO all , you can simply pass lpStartupInfo as NULL and the default value will be used
  • you should not call WaitForSingleObject on a private handle
+4
source

You must set the size of startupinfo struct:

 startupinfo.cb = sizeof(startupinfo); 

Perhaps that is why CreateProcess does not work.

And by the way - why are you calling GetStartupInfo ? You should just reset the startupinfo memory (besides setting the size as above).

See an example here .

+3
source

Source: https://habr.com/ru/post/1305491/


All Articles