Can anyone please give me a working example of using CreateProcess?

 
#import "kernel32.dll"
   int CreateProcessA(string lpApplicationName,
                      string lpCommandLine,
                      int lpProcessAttributes,
                      int lpThreadAttributes,
                      int bInheritHandles,
                      int dwCreationFlags,
                      int lpEnvironment,
                      string lpCurrentDirectory,
                      int & lpStartupInfo[],
                      int & lpProcessInformation[]);

   int WaitForSingleObject(int Handle, int Timeout);                      
   int GetLastError();
#import

int StartTerminal()
{
   // Create an array to mimic a STARTUPINFO structure, which is 68 bytes long 
   int startupinfo[17];
   
   // Set the cb value of the STARTUPINFO structure. All the other values can
   // be left set to zero
   startupinfo[0] = 17 * 4;
   
   // Create an array to mimic a PROCESS_INFORMATION structure. Doesn't need
   // initialisation
   int processinfo[4];
   
   // Call create process
   if (CreateProcessA("c:\\windows\\system32\\calc.exe", "c:\\windows\\system32\\calc.exe", 0, 0, 0, 0, 0, ".", startupinfo, processinfo) != 0) {
      // Wait for calc.exe to be closed 
      // Handle is in processinfo[0];
      WaitForSingleObject(processinfo[0], 60 * 1000);
      
      Print("Calc.exe started and closed...");
   } else {
      Print("CreateProcess() failed");
      Print(kernel32::GetLastError());
   }
}

(the piece of code was taken somewhere from here: https://www.mql5.com/en/forum/131239 )

returns with error code=2, which means "The system cannot find the file specified.", regardless of what to pass as application name or a command line

Most likely, the matter is in the incorrect specification of structures (STARTUPINFO, PROCESS_INFORMATION, etc.). But I do not know how to create real structure with all of these pointers, etc.

How to pass a null pointer to a DLL?
How to pass a null pointer to a DLL?
  • 2011.01.16
  • www.mql5.com
I'm trying to call CreateProcess() from MQL, without success...
 
CreateProcessW

MT4/MT5 strings are unicode.

Beyond that, I didn't check your code.

 
Alain Verleyen:

MT4/MT5 strings are unicode.

Beyond that, I didn't check your code.

Thank you! Got it working.
Reason: