Autor Beitrag
Experience1986
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 192

Win XP Pro SP2
Delphi 7
BeitragVerfasst: Sa 27.05.06 18:47 
Guten Abend,

ich entwickel zur Zeit eine neue Version meiner Software "AutostartAdministrator" zur Verwaltung von Windows Autostarts.

Info: support.lothargeisin...on&application=2

Hierbei ist wichtig Befehle die in Autostart-Einträgen vermerkt sind zu interpretieren und deren Ziel zu ermitteln. Beispiele:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
Befehl: Explorer.exe
Ziel: C:\Windows\Explorer.exe
Parameter=

Befehl=%windir%\system32\regedit.exe /s c:\test.reg
Ziel: C:\Windows\System32\regedit.exe
Parameter: /s c:\test.reg


Bislang hab ich eine eigene Funktion hierfür, diese geht den vorliegenden String Zeichen für Zeichen durch, solange bis der Pfad existiert. Dies ist sehr Rechenintensiv und führt nicht immer zum Ziel.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
function GetCommandPath(CMD:String; Debug:Boolean=false; GoSelf:Boolean=true):String;
var i, j:integer;
    tmp, tmp2, tmp3, StartParams, OrgCMD:String;
    SystemPaths:TStringArray;
    Exts:TStringlist;
begin

  Result:='';

  try

      OrgCMD:=CMD;
      Result:='n/\a';
      StartParams:='';
      SetLength(SystemPaths, 0);
      if FileOrDirExists(CMD) or StringEmpty(CMD) then
        begin
          Result:=CMD;
          exit;
        end;

                                                             // "C:\Programme\Firma\Programm.exe" /Parameter1 /Parameter2
      tmp:='';
      for i:=1 to length(CMD) do
        begin
          if not (CMD[i] = '"') then
            begin
              if not FileExists(tmp) then tmp:=tmp+CMD[i]
              else StartParams:=StartParams+CMD[i];
            end;
        end;
      CMD:=tmp;

      if FileExists(CMD) then
        begin
          Result:=CMD;
          exit;
        end;

      tmp:=CMD;                                                  // rundll32.exe Shell32.dll
      SystemPaths:=GetSystemPaths;
      Exts:=GetFileExtensions;
      for i:=0 to length(SystemPaths)-1 do
        begin
          tmp2:=SystemPaths[i]+tmp;                              // notepad.exe -> c:\windows\notepad.exe
          if FileExists(tmp2) then
            begin
              Result:=tmp2;
              exit;
            end;

          if GoSelf then
            begin
              tmp2:=GetCommandPath(tmp2, false, false);          // rundll32.exe Shell32.dll -> c:\windows\system32\rundll32.exe Shell32.dll
              if FileExists(tmp2) then
                begin
                  Result:=tmp2;
                  exit;
                end;

              // Mit Dateierweiterungen versuchen
              tmp2:=SystemPaths[i]+tmp;                          // notepad -> c:\Windows\notepad.exe
              for j:=0 to Exts.Count-1 do
                begin
                  tmp3:=tmp2+Exts[j];
                  tmp3:=GetCommandPath(tmp3, false, false);
                  if FileExists(tmp3) then
                    begin
                      Result:=tmp3;
                      exit;
                    end;

                  tmp3:=Copy(tmp2,0,pos(' ',tmp2)-1)+Exts[j];
                  tmp3:=GetCommandPath(tmp3, false, false);
                  if FileExists(tmp3) then
                    begin
                      Result:=tmp3;
                      exit;
                    end;
                end;

            end;
        end;

      if not FileExists(Result) then Result:=OrgCMD;

      Exts.Free;
  except
    Result:='';
  end;
end;


Deshalb wollt ich wissen ob die Windows API hier etwas zur Verfügung stellt, schließlich muss auch Windows (Beispiel: Start->Ausführen) solche Befehle interpretieren.

Vielen Dank schon mal im Voraus.


Zuletzt bearbeitet von Experience1986 am So 28.05.06 11:58, insgesamt 1-mal bearbeitet
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 28.05.06 11:17 
Das Schlüsselwort nennt sich Umgebungsvariablen ... Windows ersetzt alles, was zwischen zwei %-Zeichen steht durch den entsprechenden Wert von den Umgebungsvariablen.

d.h. %windir% wird druch den Wert der Umgebungsvariable WINDIR ersetzt.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Experience1986 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 192

Win XP Pro SP2
Delphi 7
BeitragVerfasst: So 28.05.06 11:27 
Wirklich ;-)

Ist mir schon bekannt, und meine Funktion berücksichtig dies auch. Problem ist eben wenn man folgende Befehle hat:

%windir%\explorer

so muss ich erst %windir% ersetzen (geh recht schnell, mir meiner Methode) und dann prüfen ob die Datei "explorer" besteht, ist das nicht der Fall, dann prüf ich die Dateiendungen. Da man mit Windows nichtt nur Executables in den Autostart legen kann, muss ich alle Endungen durchgehen, es könnte ja auch eine txt oder zip datei sein. Deshalb prüfe ich dann C:\Windows\explorer.*** in einer Schleife. Dieses Beispiel geht von der Zeit auch noch relativ schnell...

aber was ist mit dem Befehl "explorer"?

Hier muss ich erst mal alle Umgebungsvariablen (Standard und Benutzerdefinierte) durchgehen, hab ich dann noch nichts gefunden muss ich alle diese Pfade nochmals mit der Dateiendung überprüfen...

Im Regelfall (Etwa 90%) findet meine Funktion auch einen Pfad, nur gerade bei solche Befehlen die die geringste Information mitbringen (explorer) wird das Programm durch mein Vorgehen schnell zu langsam.

Alle hier gemachten Vorschläge helfen sicherlich im Einzelfall, nur soll die Funktion nicht nur Einzelfälle behandeln, sondern soll für alle Möglichen Fälle, dh. Schreibweisen anwendbar sein.

Da Windows dies an Zahlreichen Stellen mit höchstgeschwindigkeit Leisten kann, würde ich gern diese Funktion nutzen.

Aber Danke für deinen Hinweis.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 28.05.06 11:42 
Dazu gibt es seit Windows 2000 eine weitere Variable PATHEXT, die eine Liste möglicher Erweiterungen beinhaltet.

Unter Windows 9x kannst Du für diese Liste .EXE;.COM;.BAT annehmen.

(Oder alternativ aus der Registry jegliche Dateitypen auslesen, die sich selbst ausführen (wo also shell\open\command\@ = "%1" ist).

Achja: Tipp: Wenn Du einen vollqualifizierten Pfad hast, mach doch einfach eine Dateisuche auf explorer.* und prüf dann, welche Datei gemeint sein könnte (anhand der gültigen Erweiterungen).

Weiterhin bietet Windows eine API-Funktion, die aus einem Dateinamen anhand des Suchpfads die gültigen Dateien ermittelt.

Dazu kannst Du dir ggf. auch einmal in der Unit OVFSPathUtils.pas von Projekt Omorphia die Funktionen GetPathFromFile und aus der Kernel32.dll die Funktion Function SearchPath (Deklaration gibt's auch in besagter Unit) anschauen ... Beides sollte gut erweiterbar sein ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.


Zuletzt bearbeitet von BenBE am So 28.05.06 11:54, insgesamt 1-mal bearbeitet
Experience1986 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 192

Win XP Pro SP2
Delphi 7
BeitragVerfasst: So 28.05.06 11:52 
Hier nochmal meine Funktion:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
function GetCommandPath(CMD:String; Debug:Boolean=false; GoSelf:Boolean=true):String;
var i, j:integer;
    tmp, tmp2, tmp3, StartParams, OrgCMD:String;
    SystemPaths:TStringArray;
    Exts:TStringlist;
begin

  Result:='';

  try

      OrgCMD:=CMD;
      Result:='n/\a';
      StartParams:='';
      SetLength(SystemPaths, 0);
      if FileOrDirExists(CMD) or StringEmpty(CMD) then
        begin
          Result:=CMD;
          exit;
        end;

                                                             // "C:\Programme\Firma\Programm.exe" /Parameter1 /Parameter2
      tmp:='';
      for i:=1 to length(CMD) do
        begin
          if not (CMD[i] = '"') then
            begin
              if not FileExists(tmp) then tmp:=tmp+CMD[i]
              else StartParams:=StartParams+CMD[i];
            end;
        end;
      CMD:=tmp;

      if FileExists(CMD) then
        begin
          Result:=CMD;
          exit;
        end;

      tmp:=CMD;                                                  // rundll32.exe Shell32.dll
      SystemPaths:=GetSystemPaths;
      Exts:=GetFileExtensions;
      for i:=0 to length(SystemPaths)-1 do
        begin
          tmp2:=SystemPaths[i]+tmp;                              // notepad.exe -> c:\windows\notepad.exe
          if FileExists(tmp2) then
            begin
              Result:=tmp2;
              exit;
            end;

          if GoSelf then
            begin
              tmp2:=GetCommandPath(tmp2, false, false);          // rundll32.exe Shell32.dll -> c:\windows\system32\rundll32.exe Shell32.dll
              if FileExists(tmp2) then
                begin
                  Result:=tmp2;
                  exit;
                end;

              // Mit Dateierweiterungen versuchen
              tmp2:=SystemPaths[i]+tmp;                          // notepad -> c:\Windows\notepad.exe
              for j:=0 to Exts.Count-1 do
                begin
                  tmp3:=tmp2+Exts[j];
                  tmp3:=GetCommandPath(tmp3, false, false);
                  if FileExists(tmp3) then
                    begin
                      Result:=tmp3;
                      exit;
                    end;

                  tmp3:=Copy(tmp2,0,pos(' ',tmp2)-1)+Exts[j];
                  tmp3:=GetCommandPath(tmp3, false, false);
                  if FileExists(tmp3) then
                    begin
                      Result:=tmp3;
                      exit;
                    end;
                end;

            end;
        end;

      if not FileExists(Result) then Result:=OrgCMD;

      Exts.Free;
  except
    Result:='';
  end;
end;


Über die Unterfunktionen GetFileExtensions und GetSystemPaths erhalte ich bereits alle möglichen Dateierweiterungen und Umgebungsvariablen/Systempfade. Nur ist meine Funktion nicht ausreichend schnell und ist auch nur in etwa 80-90% der Fälle erfolgreich. Windows schafft das schneller, deshalb will ich eine Windows Funktion nutzen.

Ein Eintrag wo diese Funktion nicht erfolgreich ist, ist:
%systemroot%\system32\dumprep 0 -k

obwohl in \Windows\System32 eine Andwendung Namens "dumprep.exe" vorliegt. Und selbst wenn ich dieses Problem in den Griff bekomme, bleibt noch immer die mangelhafte Geschwindigkeit, welche WIndows in Zusammenhang mit der "Auflösung" von Befehlen nicht vorweist.
Experience1986 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 192

Win XP Pro SP2
Delphi 7
BeitragVerfasst: So 28.05.06 11:55 
Ach misst... Sorry nochmal das ich schon Lösungen als Bekannt vorausgesetzt hatte... Merke gerade das ich in diesem Forum bislang noch keine Antwort bekam. Hab das Thema noch bei Delpho-Praxis.de gestellt. :oops:
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 28.05.06 12:01 
Ich hoffe, Du hast gesehen, dass ich oben noch mal kurz was editiert \ ergänzt hab...

Allgemeines Vorgehen sollte sein:
1. Umgebungsvariablen auflösen (%windir% --> C:\Windows)
2. Bei relativen Pfaden den Absoluten Pfad anhand des Suchpfads ermitteln (explorer --> C:\Windows\explorer.exe)
3. Bei fehlender Erweiterung alle in %PATHEXT% enthaltenen Erweiterungen ausprobieren.
4. Sollte Die Datei bis jetzt nicht existieren: Die Systemverzeichnisse einzeln auflisten nach name.* und bei Erfolg diesen Namen nutzen

HTH.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Experience1986 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 192

Win XP Pro SP2
Delphi 7
BeitragVerfasst: So 28.05.06 14:52 
So nach dem Hinweis von BenBE ist mit aufgefallen das ich die Umgebungsvariable in meiner bisherigen Funktion garnicht aufgelöst habe. Nun hab ich die gesamte Funktion innerhalt der vergangen 02:40 Stunden neu entwickelt:

Die Funktion:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
function GetCommandPath(CMD:String; Debug:Boolean=false; AppendExtensions:Boolean=false):String;
var i, DebugStep:integer;
    SystemPaths:TStringArray;
    Exts:TStringlist;
    tmp:String;

    // Debuggin-Meldung
    procedure DebugMsg(extra:String='');
    begin
      inc(DebugStep);
      showmessage('DebugStep: '+inttostr(DebugStep)+#13+
                  'CMD: '+ CMD+#13+
                  'Zusatz: '+extra);
    end;

    // Löst Umgebungsvariablen in Pfade auf
    function ExpandEnvStr(const strValue: string): string;
    var
      chrResult: array[0..1023] of Char;
      wrdReturn: DWORD;
    begin
      wrdReturn := ExpandEnvironmentStrings(PChar(strValue), chrResult, 1024);
      if wrdReturn = 0 then Result := strValue
      else Result := Trim(chrResult);
    end;

    // Prüft ob ein Verzeichnis ODER eine Datei existiert
    function FileOrDirExists(Filename:TFilename):Boolean;
    begin
      Result:=(DirectoryExists(Filename) or Fileexists(Filename));
    end;

    // Fügt System-Pfade vor einen Befehl
    function AddSystemPaths(CMD:String):String;
    var i:integer;
    begin
          SystemPaths:=GetSystemPaths;
          Result:=CMD;
          for i:=0 to length(SystemPaths)-1 do
            begin
              if FileOrDirExists(SystemPaths[i]+CMD) then
                begin
                  CMD:=SystemPaths[i]+CMD;
                  Result:=CMD;
                  exit;
                end;
            end;
    end;

begin

  Result:='';
  DebugStep:=0;

  try
      if Debug then DebugMsg;

      // Dateipfad / Verzeichnispfas liegt bereits in Reinform vor.
      if FileOrDirExists(CMD) then
        begin
          Result:=CMD;
          exit;
        end;

      // Variablen initialisieren
      SetLength(SystemPaths, 0);

      //Debug:=(CMD = 'explorer');

      if Debug then DebugMsg;

      // Start Parameter / Argumente entfernen
      // Funktion "PathGetArgs()" auf Unit "ShLwAPI"
      CMD:=StringReplace(CMD, String(PathGetArgs(PCHAR(CMD))), '', []);

      // Anhängen von Dateiendungen, gescheiht automatisch durch Selbstaufruf
      // wenn Pfad bis dahin nicht ermittelt worden konnte.
      if AppendExtensions then
        begin
          if not FileOrDirExists(CMD) then
            begin
              Exts:=GetFileExtensions(true);
              for i:=0 to Exts.Count-1 do
                begin
                  tmp:=CMD+Exts[i];
                  if FileOrDirExists(tmp) then
                    begin
                      Result:=tmp;
                      exit;
                    end;
                end;

              for i:=0 to Exts.Count-1 do
                begin
                  tmp:=GetCommandPath(CMD+Exts[i], Debug, false);
                  if FileOrDirExists(tmp) then
                    begin
                      Result:=tmp;
                      exit;
                    end;
                end;
            end;

          Result:=CMD;
          exit;
        end;

      if Debug then DebugMsg;

      // Anführungszeichen entfernen
      // "C:\Mein Ordner\Programm.exe" => C:\Mein Ordner\Programm.exe
      CMD:=StringReplace(CMD, '"', '', [rfReplaceAll]);

      if Debug then DebugMsg;

      // Eventuelles Leerzeichen am Ende entfernen
      if length(CMD) > 0 then
      if CMD[length(CMD)] = ' ' then CMD:=Copy(CMD, 0, length(CMD)-1);

      if Debug then DebugMsg;

      // Umgebungsvariable auflösen
      // %windir%\explorer.exe => c:\windows\explorer.exe
      CMD:=ExpandEnvStr(CMD);

      if Debug then DebugMsg;

      // Wenn nur Dateiname vorhanden, dann setze SystemVariablen davor
      // explorer.exe => c:\dokumente und einstellungen\explorer.exe
      // explorer.exe => c:\windows\explorer.exe
      if not DirectoryExists(ExtractFilepath(CMD)) then
        begin
          CMD:=AddSystemPaths(CMD);
        end;

      if Debug then DebugMsg;

      // Prüfen ob Pfad bereits existiert
      if FileOrDirExists(CMD) then
        begin
          Result:=CMD;
        end
       else
        begin
          Result:=GetCommandPath(CMD, Debug, true);
        end;

  finally
    Exts.Free;
  end;

end;


Da ich hier meist kompetente Antworten für meine Fragen finde, habe ich für alle Interessenten dieses Forums die notwendigen Units gezippt und angehängt. Einfach im eigenen Projekt die Unit "UN_GETCOMMANDPATH" und "SHLwApi" einbinden und folgende Funktionen aufrufen:

ausblenden Delphi-Quelltext
1:
2:
3:
Befehl:='explorer /s c:\autoexec.bat';
Pfad:=GetCommandPath(Befehl);
Parameter:=GetCommandPara(Befehl);


Bitte den Copyright-Hinweis in UN_GETCOMMANDPATH beachten.
Einloggen, um Attachments anzusehen!
Experience1986 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 192

Win XP Pro SP2
Delphi 7
BeitragVerfasst: Mo 29.05.06 14:53 
Ein kleines aber notwendiges Update:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
function GetCommandPath(CMD:String; Debug:Boolean=false; AppendExtensions:Boolean=false; SelfCalls:integer=0):String;
var i, DebugStep:integer;
    SystemPaths:TStringArray;
    Exts:TStringlist;
    tmp:String;

    // Debuggin-Meldung
    procedure DebugMsg(extra:String='');
    begin
      inc(DebugStep);
      showmessage('DebugStep: '+inttostr(DebugStep)+#13+
                  'CMD: '+ CMD+#13+
                  'Zusatz: '+extra);
    end;

    // Löst Umgebungsvariablen in Pfade auf
    function ExpandEnvStr(const strValue: string): string;
    var
      chrResult: array[0..1023] of Char;
      wrdReturn: DWORD;
    begin
      wrdReturn := ExpandEnvironmentStrings(PChar(strValue), chrResult, 1024);
      if wrdReturn = 0 then Result := strValue
      else Result := Trim(chrResult);
    end;

    // Prüft ob ein Verzeichnis ODER eine Datei existiert
    function FileOrDirExists(Filename:TFilename):Boolean;
    begin
      Result:=(DirectoryExists(Filename) or Fileexists(Filename));
    end;

    // Fügt System-Pfade vor einen Befehl
    function AddSystemPaths(CMD:String):String;
    var i:integer;
    begin
          SystemPaths:=GetSystemPaths;
          Result:=CMD;
          for i:=0 to length(SystemPaths)-1 do
            begin
              if FileOrDirExists(SystemPaths[i]+CMD) then
                begin
                  CMD:=SystemPaths[i]+CMD;
                  Result:=CMD;
                  exit;
                end;
            end;
    end;

begin

  Result:=CMD;
  DebugStep:=0;

  inc(SelfCalls);

  try
      if Debug then DebugMsg;

      // Dateipfad / Verzeichnispfas liegt bereits in Reinform vor.
      if FileOrDirExists(CMD) then
        begin
          Result:=CMD;
          exit;
        end;

      // Variablen initialisieren
      SetLength(SystemPaths, 0);

      //Debug:=(CMD = 'explorer');

      if Debug then DebugMsg;

      // Start Parameter / Argumente entfernen
      // Funktion "PathGetArgs()" auf Unit "ShLwAPI"
      CMD:=StringReplace(CMD, String(PathGetArgs(PCHAR(CMD))), '', []);

      // Anhängen von Dateiendungen, gescheiht automatisch durch Selbstaufruf
      // wenn Pfad bis dahin nicht ermittelt worden konnte.
      if AppendExtensions then
        begin
          if not FileOrDirExists(CMD) then
            begin
              Exts:=GetFileExtensions(true);
              for i:=0 to Exts.Count-1 do
                begin
                  tmp:=CMD+Exts[i];
                  if FileOrDirExists(tmp) then
                    begin
                      Result:=tmp;
                      exit;
                    end;
                end;

              for i:=0 to Exts.Count-1 do
                begin
                  tmp:=GetCommandPath(CMD+Exts[i], Debug, false, SelfCalls);
                  if FileOrDirExists(tmp) then
                    begin
                      Result:=tmp;
                      exit;
                    end;
                end;
            end;

          Result:=CMD;
          exit;
        end;

      if Debug then DebugMsg;

      // Anführungszeichen entfernen
      // "C:\Mein Ordner\Programm.exe" => C:\Mein Ordner\Programm.exe
      CMD:=StringReplace(CMD, '"', '', [rfReplaceAll]);

      if Debug then DebugMsg;

      // Eventuelles Leerzeichen am Ende entfernen
      if length(CMD) > 0 then
      if CMD[length(CMD)] = ' ' then CMD:=Copy(CMD, 0, length(CMD)-1);

      if Debug then DebugMsg;

      // Umgebungsvariable auflösen
      // %windir%\explorer.exe => c:\windows\explorer.exe
      CMD:=ExpandEnvStr(CMD);

      if Debug then DebugMsg;

      // Wenn nur Dateiname vorhanden, dann setze SystemVariablen davor
      // explorer.exe => c:\dokumente und einstellungen\explorer.exe
      // explorer.exe => c:\windows\explorer.exe
      if not DirectoryExists(ExtractFilepath(CMD)) then
        begin
          CMD:=AddSystemPaths(CMD);
        end;

      if Debug then DebugMsg;

      // Prüfen ob Pfad bereits existiert
      if FileOrDirExists(CMD) then
        begin
          Result:=CMD;
        end
       else
        begin
          if SelfCalls=1 then
          Result:=GetCommandPath(CMD, Debug, true, SelfCalls);
        end;

  finally
    Exts.Free;
  end;

end;