Examples Delphi

Title: Fixing problem with *long* command line parameters
Question: Delphi 4/5 is limited to parameters totalling 4095 bytes before an exception is raised. As my application was in the Send To menu and needed lots of files to be 'sent' to it I had to overcome this. The following are the functions needed to do this.
Answer:
// the following functions fix the 4095 byte limit for command line parameter(s)
function Fixed_GetParamStr(P: PChar; var Param: string): PChar;
function StrLen(Str: PChar): Cardinal; assembler;
asm
MOV EDX,EDI
MOV EDI,EAX
MOV ECX,0FFFFFFFFH
XOR AL,AL
REPNE SCASB
MOV EAX,0FFFFFFFEH
SUB EAX,ECX
MOV EDI,EDX
end;
type
PCharArray = ^TCharArray;
TCharArray = array[0..MaxInt-1] of Char;
var
BufLen, Len: Integer;
Buffer: PCharArray;
begin
BufLen := StrLen(P);
GetMem(Buffer, BufLen);
while True do
begin
while (P[0] #0) and (P[0] if (P[0] = '"') and (P[1] = '"') then Inc(P, 2) else Break;
end;
Len := 0;
while P[0] ' ' do
if P[0] = '"' then
begin
Inc(P);
while (P[0] #0) and (P[0] '"') do
begin
Buffer[Len] := P[0];
Inc(Len);
Inc(P);
end;
if P[0] #0 then Inc(P);
end else
begin
Buffer[Len] := P[0];
Inc(Len);
Inc(P);
end;
SetString(Param, Buffer^, Len);
Result := P;
FreeMem(Buffer, BufLen);
end;
function Fixed_ParamCount: Integer;
var
P: PChar;
S: string;
begin
P := Fixed_GetParamStr(GetCommandLine, S);
Result := 0;
while True do
begin
P := Fixed_GetParamStr(P, S);
if S = '' then Break;
Inc(Result);
end;
end;
function Fixed_ParamStr(Index: Integer): string;
var
P: PChar;
Buffer: array[0..260] of Char;
begin
if Index = 0 then
SetString(Result, Buffer, GetModuleFileName(0, Buffer, SizeOf(Buffer)))
else
begin
P := GetCommandLine;
while True do
begin
P := Fixed_GetParamStr(P, Result);
if (Index = 0) or (Result = '') then Break;
Dec(Index);
end;
end;
end;