How to call CreateProcess in Delphi?

Following the documentation CreateProcessand an example from MSDN , I am trying to call CreateProcess:

var
   commandLine: string;
   si: TStartupInfo;
   pi: TProcessInformation;
begin
   commandLine := 'C:\Windows\System32\cmd.exe';

   si := Default(TStartupInfo);
   si.cb := sizeof(si);

   CreateProcess(
        PChar(nil),         //no module name (use command line)
        PChar(commandLine), //Command Line
        nil,                //Process handle not inheritable
        nil,                //Thread handle not inheritable
        False,              //Don't inherit handles
        0,                  //No creation flags
        nil,                //Use parent environment block
        PChar(nil),         //Use parent starting directory
        si,                 //Startup Info
        pi                  //Process Info
   );

Access violation call failure:

EAccessViolation exception in kernel32.dll module on 0003B77B.
Access Violation at address 7671B77B in the kernel32.dll module. Write address 00B47EA6.

Now I understand why it crashes for me, but I don’t understand why it doesn’t crash for the MSDN code sample , and I don’t understand why it didn’t fail for you David .

The documentation for CreateProcessstates that if the first parameter is null (like this in my example, the MSDN example and another example), then the argument CreateProcesswill changecommandLine :

lpCommandLine [in, out, optional]

...

Unicode, CreateProcessW, . (, ). , .

, 0x00B47EA6:

enter image description here

, CreateProcess . CreateProcess , CreateProcess , .

,

C:\Windows\System32\cmd.exe

. -1:

enter image description here

, .

, :

var
    commandLine: string;
    si: TStartupInfo;
    pi: TProcessInformation;
    l: Integer;
    buffer: TByteDynArray;

commandLine := 'C:\Windows\System32\cmd.exe';

//Copy to writable buffer (including null terminator)    
l := (Length(commandLine)+1)*sizeof(Char);
SetLength(buffer, l);
Move(commandLine[1], buffer[0], l);

si := Default(TStartupInfo);
si.cb := sizeof(si);

if not CreateProcess(
            PChar(nil),               //no module name (use command line)
//          PChar(commandLine), //Command Line
            @buffer[0],
            nil,                //Process handle not inheritable
            nil,                //Thread handle not inheritable
            False,              //Don't inherit handles
            0,                  //No creation flags
            nil,                //Use parent environment block
            PChar(nil),         //Use parent starting directory
            si,                 //Startup Info
            {var}pi             //Process Info
);

.

, , . , -

CreateProcess Delphi?

? ?

ShellExecute

, ShellExecute:

var
   shi: TShellExecuteInfo;

shi := Default(TShellExecuteInfo);
shi.cbSize := SizeOf(TShellExecuteInfo);
shi.lpFile := PChar(commandLine); 
shi.nShow := SW_SHOWNORMAL;

ShellExecuteEx(@shi);
+4
1

PChar (commandLine) PChar (WideString (commandLine)). Delphi XE6.

, - , Delphi XE .

+1

All Articles