Der Code
Delphi-Quellcode:
procedure GetCommandLineArgs(Args: TStrings);
var
NumArgs: Integer;
TempArgs: PPWideChar;
i: Integer;
begin
if CommandLineToArgvW(GetCommandLineW, NumArgs) <>
nil then
begin
try
try
TempArgs := CommandLineToArgvW(GetCommandLineW, NumArgs);
for i := 0
to NumArgs - 1
do
begin
Args.Add(TempArgs^);
Inc(TempArgs);
end;
except
raise Exception.Create(SysErrorMessage(GetLastError));
end;
finally
GlobalFree(THandle(TempArgs));
end;
end;
end;
erzeugt zwei Speicherlöcher:
Erstes Speicherloch:
if CommandLineToArgvW(GetCommandLineW, NumArgs) <> nil then
denn das Ergebnis wird nicht freigegeben (wie Dezipaitor schon richtig schreibt)
Zweites Speicherloch:
GlobalFree(THandle(TempArgs));
denn TempArgs ist nicht mehr gültig (wegen Inc()).