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),
PChar(commandLine),
nil,
nil,
False,
0,
nil,
PChar(nil),
si,
pi
);
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:

, CreateProcess . CreateProcess , CreateProcess , .
,
C:\Windows\System32\cmd.exe
. -1:

, .
, :
var
commandLine: string;
si: TStartupInfo;
pi: TProcessInformation;
l: Integer;
buffer: TByteDynArray;
commandLine := 'C:\Windows\System32\cmd.exe';
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),
@buffer[0],
nil,
nil,
False,
0,
nil,
PChar(nil),
si,
{var}pi
);
.
, , . , -
CreateProcess Delphi?
? ?
ShellExecute
, ShellExecute:
var
shi: TShellExecuteInfo;
shi := Default(TShellExecuteInfo);
shi.cbSize := SizeOf(TShellExecuteInfo);
shi.lpFile := PChar(commandLine);
shi.nShow := SW_SHOWNORMAL;
ShellExecuteEx(@shi);