Tutorial :How to Create a process in c++ to execute exe?


I have to excecute a exe which is available in some drive how can i do this using c++??

I am doing like this

#include <stdio.h>  #include <conio.h>  #include <windows.h>    void main()  {      STARTUPINFO si;        PROCESS_INFORMATION pi;        ZeroMemory( &si, sizeof(si) );      si.cb = sizeof(si);      ZeroMemory( &pi, sizeof(pi) );        if(!CreateProcess(L"c:\\DOTNET.exe",NULL,NULL, NULL,FALSE, 0,NULL,NULL,&si,&pi ) )       {          printf( "CreateProcess failed (%d).\n", GetLastError() );      }      else      {          printf("Prcess Creation Success");      }        WaitForSingleObject( pi.hProcess, INFINITE );      CloseHandle( pi.hProcess );      CloseHandle( pi.hThread );         getch();  }  

But every time it is showing process creation failed with error code 2(i.e can not find the path specified) but i place the DOTNET.exe at c:\DOTNET.exe only.

what is wrong in the above code.can any one suggest me...

Any help is greately appreciated..

Thabks in advance.


I've just tested your code and it's working here with :

if(!CreateProcess(L"C:\\Program Files\\Mozilla Firefox\\firefox.exe",NULL,NULL, NULL,FALSE, 0,NULL,NULL,&si,&pi ) )   

A C++/Win32 solution for your C/Win32 code :)

void ExecuteAndWait (wstring toto)  {      STARTUPINFO si = { sizeof(si) };      PROCESS_INFORMATION  pi;      vector<TCHAR> V( toto.length() + 1);      for (int i=0;i< (int) toto.length();i++)      V[i] = toto[i];      CreateProcess(NULL, &V[0],0, 0, FALSE, 0, 0, 0, &si, &pi);      WaitForSingleObject(pi.hProcess, INFINITE);      CloseHandle(pi.hProcess);      CloseHandle(pi.hThread);  }  


I think you need to call CreateProcess() differently.

Try this;

if (!CreateProcess( NULL, L"C:\\DOTNET.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))  


Are you building a Unicode executable? Try _T("C:\\DOTNET.exe") instead.

Or does your DOTNET.exe have some dependent DLLs which are not being found?

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »