Solved

Getting commandline info (exe name, path and any parameters) from a registry string value that may include quoted strings

Posted on 2006-06-10
5
320 Views
Last Modified: 2013-11-18
I'm getting a command line string from the registry, but it's coming in a variety of different formats:

Sometimes the full path and name is enclosed in double quotes, but not always.
    example - "C:\Program Files\My Apps\Foo.exe"

Sometimes there are  parameters (outside the quotes - if they exist) but not always.
    example - "C:\Program Files\My Apps\Foo.exe" -onbootup

The parameters (if any) may have a leading char like  - or \ but not always.
   example - C:\Program Files\My Apps\Foo.exe startup

I need a way to break these strings down to extract the information as separate items, something like ParamStr does. although the string supplied is a widestring not an ansichar.

TIA Nick
0
Comment
Question by:nickglasier
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
ID: 16876882
I just wrote this procedure
it returns "filename" (without quotes) and a Tstringlist with each param

e.g.
  sl := TStringList.create;
  s := 'C:\Program Files\My Apps\Foo.exe startup';
  GuessFilenameAndParameters(s, filename, sl);
  showmessage('filename = ' + filename);
  showmessage(''params are :- ' + #13#10 + sl.text;

works for
C:\Program Files\My Apps\Foo.exe
"C:\Program Files\My Apps\Foo.exe"
C:\Program Files\My Apps\Foo.exe startup
"C:\Program Files\My Apps\Foo.exe" startup
C:\Program Files\My Apps\Foo.exe startup test
"C:\Program Files\My Apps\Foo.exe startup" test
C:\Program Files\My Apps\Foo.exe startup test "part three"
"C:\Program Files\My Apps\Foo.exe startup" test "part three"


procedure GuessFilenameAndParameters(CommandLine: string; var Filename: string; var ResultParams: TStringList);
var
  i: integer;
  s: string;
  appfoundat: integer;
begin
  ResultParams.Clear;
  ResultParams.Delimiter := ' ';
  ResultParams.DelimitedText := CommandLine;
  Filename := '';
  appfoundat := -1;
  i := Pred(ResultParams.Count);
  while (i >= 0) do
  begin // find 1st "field" that has a . in the 4th to last character
      s := ResultParams[i];
      if (length(S) > 4) and (s[length(s)-3] = '.') then
        appfoundat := i;
      dec(i);
  end;
  if appfoundat <> -1 then
  begin
      i := 0;
      for i := 0 to appfoundat do
      begin
          Filename := Filename + ResultParams[0] + ' ';
          ResultParams.Delete(0);
      end;
      Delete(Filename, length(Filename), 1); // remove last space
  end;
end;
0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 16878633
check out this link:
http://immortals.fake.hu/delphiportal/modules.php?name=News&file=article&sid=382

I use the code myself to do all my cmd line parsing.
It does not matter the ordering of the params passed.
Im not sure how it will do with file paths and spaces though

function GetParamVal(const TaggedParm: string;
  IgnoreCase: boolean = true): string;
var
  Cmd: string;
  i, Len: integer;
  Comp1, Comp2: string;
begin
  Cmd := '';
  Comp1 := TaggedParm + '=';
  if IgnoreCase then
    Comp1 := UpperCase(Comp1);

  Len := length(Comp1);

  for i := 1 to ParamCount do
  begin
    Comp2 := copy(ParamStr(i), 1, Len);
    if IgnoreCase then
      Comp2 := UpperCase(Comp2);
    if (Comp1 = Comp2) then
    begin
      Cmd := trim(copy(ParamStr(i), Len + 1, length(ParamStr(i))));
      break;
    end;
  end;

  Result := UpperCase(Cmd);
end;
0
 

Author Comment

by:nickglasier
ID: 16879398


LMuadDib's answer requires knowledge of the parameter's tags which is not a controllable factor.

TheRealLoki's answer is the best of these two, and works really well for ansi or pascal strings, but doesn't deal with the widestrings issue. I tried changing the TStringlist for a jcl WideStringList object, but it doesn't have either a delimiter or a delimited text property. Any further ideas?

If no further answers are available I'll award the points to TheRealLoki.

 
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16879804
sure, i just assumed you were using Delphi 7 since your question didn't state.
I made a very simple TDelimitedStringList based on Delphi 5's TStringList so I could have "Delimiters' ni Delphi 5.
you can use that too if you like.
you can download it from here https://sourceforge.net/project/showfiles.php?group_id=168004&package_id=192765
you do not need to "install it" just put it in a directory on Delphi's search path (or in your project dir)
and make the following changes to my original code posted above

*add*
uses DelimitedStringList;

*Change* _all_ refrences to "TStringList" to TDelimitedStringList in my code above.

*Replace*
    ResultParams.Delimiter := ' ';
with
    ResultParams.Delimiters.Add(' ');

As for the widestring, can't you just convert the widestring to a string? eg. s := wides;
0
 

Author Comment

by:nickglasier
ID: 16879929
Hi TheRealLoki,

I am using Delphi 7, the absence of Delimiter and DelimitedText properties is in the jcl widestringlist object.  Right now I'm using the jcl libraries WideStringToStringEx and StringToWideStringEx functions to convert, and hoping nothing gets lost in the translation.

Nick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
character entities and entity references in XML 17 111
CSS HELP 6 84
CheckListBox usage 3 50
.php tree directory? 5 55
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now