Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apps + Params Intervention

Status
Not open for further replies.

SEP800

IS-IT--Management
Dec 28, 2008
8
PH
Hi! I'm trying to create a program that processes params when run.

Basically, if you run the program with this syntax: "C:\Program.exe REQUEST INFO1 INFO2 INFO3" it should be able to process the commands accordingly and spit out result data.

Here's what I have so far. It compiles without any errors or warnings but when I run it in a live environment, I get an Error 216 as the result.

Any help would be highly appreciated.

Thanks!

program MsgProc;

uses
SysUtils;

function Receive(MsgData: Array of String): String;
begin
if MsgData[0] = 'HELP' then
begin
Result := 'When this application is complete, a list of valid keywords and their use will be displayed here.';
end
else
if MsgData[0] = 'REG' then
begin
Result := 'Congratulations ' + MsgData[1] + '! You are now a registered user.';
end
else
if MsgData[0] = 'DATA' then
begin
Result := 'Coming soon!';
end
else
Result := 'Sorry! ' + MsgData[0] + ' is not a valid keyword. Text HELP for a list of valid keywords and their use.';
end;

procedure ProcessQueue(const TxtDelimiter, TxtData: String; MaxArrayData: Integer = 0);
var
i, DataStart, ArrayItem, DelimLength: Integer;
ArrayData: Array of String;

procedure AddString(ArrayEnd: Integer = -1);
var
EndingPos: Integer;
begin
if (ArrayEnd = -1) then
EndingPos := i
else
EndingPos := ArrayEnd + 1;

if (DataStart < EndingPos) then
ArrayData[ArrayItem] := Copy(TxtData, DataStart, EndingPos - DataStart)
else
ArrayData[ArrayItem] := '';

Inc(ArrayItem);
end;
begin
if (TxtData = '') or (MaxArrayData < 0) then
begin
SetLength(ArrayData, 0);

Exit;
end;

if (TxtDelimiter = '') then
begin
SetLength(ArrayData, 1);

ArrayData[0] := TxtData;

Exit;
end;

DelimLength := Length(TxtDelimiter);
SetLength(ArrayData, (Length(TxtData) div DelimLength) + 1);

i := 1;
DataStart := i;
ArrayItem := 0;

while (i <= (Length(TxtData) - DelimLength + 1)) do
begin
if (TxtData = TxtDelimiter[1]) then
if (Copy(TxtData, i, DelimLength) = TxtDelimiter) then
begin
AddString;

if (ArrayItem = MaxArrayData) then
begin
SetLength(ArrayData, ArrayItem);

Exit;
end;

Inc(i, DelimLength - 1);

DataStart := i + 1;
end;

Inc(i);
end;

AddString(Length(TxtData));

SetLength(ArrayData, ArrayItem);

Receive(ArrayData);
end;

var
i: Integer;
ComBuf: String;

begin
for i := 0 to ParamCount do
begin
ComBuf := ParamStr(i);

ProcessQueue(' ', ComBuf);
end;
end.
 
To start off, ParamStr(0) represents the command-line of your app, so there is one error. Your runtime error 216 is an access violation, which is likely occurring because you are passing an invalid index value to your arraydata variable.

However, you are going to way more trouble than is necessary to get this done. All that is required to pull all the parms into an array is something like:

Code:
program MsgProc; uses sysutils;
  var
    // system maximum is 9 parms
    parmarray: array[1..9] of string; 
    i: integer;
  begin
    for i := 1 to ParamCount do
      parmarray[i] := ParamStr(i);
    // do your processing against the parms here
  end.



I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
I'll try this out. Thanks Glenn9999!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top