| [541] | 1 | { ******************************************** }
 | 
|---|
 | 2 | {       RegFuncs ver 1.1 (Jan. 16, 2004)         }
 | 
|---|
 | 3 | {                                              }
 | 
|---|
 | 4 | {       For Delphi 4, 5 and 6                  }
 | 
|---|
 | 5 | {                                              }
 | 
|---|
 | 6 | {       Copyright (C) 1999-2003, Kurt Senfer.  }
 | 
|---|
 | 7 | {       All Rights Reserved.                   }
 | 
|---|
 | 8 | {                                              }
 | 
|---|
 | 9 | {       Support@ks.helpware.net                }
 | 
|---|
 | 10 | {                                              }
 | 
|---|
 | 11 | {       Documentation and updated versions:    }
 | 
|---|
 | 12 | {                                              }
 | 
|---|
 | 13 | {       http://KS.helpware.net                 }
 | 
|---|
 | 14 | {                                              }
 | 
|---|
 | 15 | { ******************************************** }
 | 
|---|
 | 16 | {
 | 
|---|
 | 17 |     This library is free software; you can redistribute it and/or
 | 
|---|
 | 18 |     modify it under the terms of the GNU Lesser General Public
 | 
|---|
 | 19 |     License as published by the Free Software Foundation; either
 | 
|---|
 | 20 |     version 2.1 of the License, or (at your option) any later version.
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 |     This library is distributed in the hope that it will be useful,
 | 
|---|
 | 23 |     but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 24 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
|---|
 | 25 |     Lesser General Public License for more details.
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 |     You should have received a copy of the GNU Lesser General Public
 | 
|---|
 | 28 |     License along with this library; if not, write to the Free Software
 | 
|---|
 | 29 |     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
|---|
 | 30 | }
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | Unit RegFuncs;
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | Interface
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | Uses Windows;
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | Function ReadRegString(MainKey: HKey; SubKey, ValName: String): String;
 | 
|---|
 | 41 | Procedure WriteRegString(MainKey: HKey; SubKey, ValName: String; const Data: String);
 | 
|---|
 | 42 | function GetExeOpen(Ext: string; var Exefil, Params: string; sielent: boolean = true): Boolean;
 | 
|---|
 | 43 | procedure ExecuteDefaultOpen(ext, aFile: String);
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | Implementation
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | uses
 | 
|---|
 | 48 |   Sysutils, KS_procs, ShellAPI;
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | //------------------------------------------------------------------------------
 | 
|---|
 | 52 | function GetMainKeyAsString(Key: HKey):string;
 | 
|---|
 | 53 | begin 
 | 
|---|
 | 54 |   //asm int 3 end; //trap
 | 
|---|
 | 55 |   case Key of
 | 
|---|
 | 56 |      $80000000 : result := 'HKEY_CLASSES_ROOT';
 | 
|---|
 | 57 |      $80000001 : result := 'HKEY_CURRENT_USER';
 | 
|---|
 | 58 |      $80000002 : result := 'HKEY_LOCAL_MACHINE';
 | 
|---|
 | 59 |      $80000003 : result := 'HKEY_USERS';
 | 
|---|
 | 60 |      $80000004 : result := 'HKEY_PERFORMANCE_DATA';
 | 
|---|
 | 61 |      $80000005 : result := 'HKEY_CURRENT_CONFIG';
 | 
|---|
 | 62 |      $80000006 : result := 'HKEY_DYN_DATA';
 | 
|---|
 | 63 |   else result := 'Unknown key';
 | 
|---|
 | 64 |   end;
 | 
|---|
 | 65 | end;
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | //------------------------------------------------------------------------------
 | 
|---|
 | 68 | Function ReadRegString(MainKey: HKey; SubKey, ValName: String): String;
 | 
|---|
 | 69 |   // NB default value is read if subkey isent ended with a backslash
 | 
|---|
 | 70 | Var
 | 
|---|
 | 71 |   Key: HKey;
 | 
|---|
 | 72 |   C: Array[0..1023] of Char;
 | 
|---|
 | 73 |   D: Cardinal;  //value type
 | 
|---|
 | 74 |   D2: Cardinal; //buffer size
 | 
|---|
 | 75 | Begin 
 | 
|---|
 | 76 |   //asm int 3 end; //trap
 | 
|---|
 | 77 |   result := '';
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 |   if RegOpenKeyEx(MainKey, Pchar(NoEndBackSlash(SubKey)), 0, KEY_READ, Key) = ERROR_SUCCESS
 | 
|---|
 | 80 |      then begin
 | 
|---|
 | 81 |         try
 | 
|---|
 | 82 |            C := '';
 | 
|---|
 | 83 |            D2 := SizeOf(C);
 | 
|---|
 | 84 |            if (RegQueryValueEx(Key, Pchar(ValName), Nil, @D, @C, @D2) = ERROR_SUCCESS) and
 | 
|---|
 | 85 |               ((D = REG_EXPAND_SZ) or (D = REG_SZ))
 | 
|---|
 | 86 |               then result := C
 | 
|---|
 | 87 |               else result := '';
 | 
|---|
 | 88 |         finally
 | 
|---|
 | 89 |            RegCloseKey(Key);
 | 
|---|
 | 90 |         end;
 | 
|---|
 | 91 |      end
 | 
|---|
 | 92 |      else DeveloperMessage('Failed to open registry key for reading string'+CrLf+ GetMainKeyAsString(MainKey) + ', '+ SubKey);
 | 
|---|
 | 93 | End;
 | 
|---|
 | 94 | //------------------------------------------------------------------------------
 | 
|---|
 | 95 | const
 | 
|---|
 | 96 |   KeyVal: Integer = KEY_WRITE  or  KEY_EXECUTE or KEY_QUERY_VALUE;
 | 
|---|
 | 97 | //------------------------------------------------------------------------------
 | 
|---|
 | 98 | Procedure WriteRegString(MainKey: HKey; SubKey, ValName: String; const Data: String);
 | 
|---|
 | 99 | Var
 | 
|---|
 | 100 |   Key: HKey;
 | 
|---|
 | 101 |   D: Cardinal;
 | 
|---|
 | 102 | Begin
 | 
|---|
 | 103 |   //asm int 3 end; //trap
 | 
|---|
 | 104 |   if RegCreateKeyEx(MainKey, Pchar(NoEndBackSlash(SubKey)), 0, Nil, REG_OPTION_NON_VOLATILE, KeyVal, Nil, Key, @D) = ERROR_SUCCESS
 | 
|---|
 | 105 |      then begin
 | 
|---|
 | 106 |         try
 | 
|---|
 | 107 |            RegSetValueEx(Key, Pchar(ValName), 0, REG_SZ, PChar(Data), Length(Data));
 | 
|---|
 | 108 |         finally
 | 
|---|
 | 109 |            RegCloseKey(Key);
 | 
|---|
 | 110 |         end;
 | 
|---|
 | 111 |      end
 | 
|---|
 | 112 |      else KSMessageW('Failed to open registry key for writing string'+CrLf+ GetMainKeyAsString(MainKey) + ', '+ SubKey);
 | 
|---|
 | 113 | End;
 | 
|---|
 | 114 | //------------------------------------------------------------------------------
 | 
|---|
 | 115 | function GetExe_(Ext, Actiontype: string; var Exefil, Params: string; sielent: boolean = true): Boolean;
 | 
|---|
 | 116 | { the best way to find an exe is trugh the registry entries - ther we get any
 | 
|---|
 | 117 |   command line param that might be neded, but if it fails we can try FindExecutable
 | 
|---|
 | 118 |   (witch is in the center og the _GetExeOpen function), but it only returns the
 | 
|---|
 | 119 |   exe file }
 | 
|---|
 | 120 | var
 | 
|---|
 | 121 |   S: string;
 | 
|---|
 | 122 |   I: integer;
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 |   //------------------------------------------
 | 
|---|
 | 125 |   procedure LastTry;
 | 
|---|
 | 126 |   begin
 | 
|---|
 | 127 |      Params :='';
 | 
|---|
 | 128 |      Result :=_GetExeOpen(ext, ExeFil, sielent);
 | 
|---|
 | 129 |      if not result
 | 
|---|
 | 130 |         then DeveloperMessage('_GetExeOpen failed');
 | 
|---|
 | 131 |   end;
 | 
|---|
 | 132 |   //------------------------------------------
 | 
|---|
 | 133 |   procedure HandleRegInfo(RegData: String);
 | 
|---|
 | 134 |   begin
 | 
|---|
 | 135 |      if length(RegData) = 0
 | 
|---|
 | 136 |         then begin
 | 
|---|
 | 137 |            //somthings missing in the registry
 | 
|---|
 | 138 |            DeveloperMessage('GetExeOpen: read exe from registry failed');
 | 
|---|
 | 139 |            LastTry;
 | 
|---|
 | 140 |            exit;
 | 
|---|
 | 141 |         end;
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 |      { there might be several traling "%x" params - we remove them all
 | 
|---|
 | 144 |        our caling procedure expect that the string we return can be used
 | 
|---|
 | 145 |        to start a program and open a file just by using the file as a
 | 
|---|
 | 146 |        trailing param }
 | 
|---|
 | 147 | 
 | 
|---|
 | 148 |      I := pos('"%', RegData);
 | 
|---|
 | 149 |      while I > 0 do
 | 
|---|
 | 150 |         begin
 | 
|---|
 | 151 |            delete(RegData, I, length('"%1"')); //we expect max 9 params
 | 
|---|
 | 152 |            I := pos('"%', RegData);
 | 
|---|
 | 153 |         end;
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 |      Exefil := Trim(RegData);
 | 
|---|
 | 156 | 
 | 
|---|
 | 157 |      { now we have an exefile and it can have some params starting with
 | 
|---|
 | 158 |        " /" or " -" }
 | 
|---|
 | 159 |      I := Pos(' /', Exefil);
 | 
|---|
 | 160 |      if I > 0
 | 
|---|
 | 161 |         then begin
 | 
|---|
 | 162 |            //we have params
 | 
|---|
 | 163 |            Params := Exefil;
 | 
|---|
 | 164 |            Exefil := Copy(Exefil, 1, I - 1);
 | 
|---|
 | 165 |            Delete(Params, 1, I);
 | 
|---|
 | 166 |         end
 | 
|---|
 | 167 |         else begin
 | 
|---|
 | 168 |            I := Pos(' -', Exefil);
 | 
|---|
 | 169 |            if I > 0
 | 
|---|
 | 170 |               then begin
 | 
|---|
 | 171 |                  //we have params
 | 
|---|
 | 172 |                  Params := Exefil;
 | 
|---|
 | 173 |                  Exefil := Copy(Exefil, 1, I - 1);
 | 
|---|
 | 174 |                  Delete(Params, 1, I);
 | 
|---|
 | 175 |               end
 | 
|---|
 | 176 |               else Params := '';
 | 
|---|
 | 177 |         end;
 | 
|---|
 | 178 | 
 | 
|---|
 | 179 |      //params is now in Params - if any
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |      // Remove sourounding " from the file path
 | 
|---|
 | 182 |      if (Copy(Exefil, 1, 1) = #34) and //leading "
 | 
|---|
 | 183 |         (Exefil[Length(Exefil)] = #34)  //trailing "
 | 
|---|
 | 184 |         then Exefil := Copy(Exefil, 2 , length(Exefil) -2);
 | 
|---|
 | 185 | 
 | 
|---|
 | 186 |      //we migt have an exe without a path - try the windows folder
 | 
|---|
 | 187 |      if pos('\', ExeFil) = 0
 | 
|---|
 | 188 |         then ExeFil := GetWinDir+ExeFil;
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 |      result := FileExists(Exefil);
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 |      if not result
 | 
|---|
 | 193 |         then LastTry;
 | 
|---|
 | 194 |   end;
 | 
|---|
 | 195 |   //------------------------------------------
 | 
|---|
 | 196 | begin
 | 
|---|
 | 197 |   //asm int 3 end; //trap
 | 
|---|
 | 198 |   Result := false;
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 |   if length(ext) = 0
 | 
|---|
 | 201 |      then begin
 | 
|---|
 | 202 |         S := 'Call to GetExeOpen with an empty extension param';
 | 
|---|
 | 203 |         if not Sielent
 | 
|---|
 | 204 |            then KSMessageE(S)
 | 
|---|
 | 205 |            else DeveloperMessage(S);
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 |         exit;
 | 
|---|
 | 208 |      end;
 | 
|---|
 | 209 | 
 | 
|---|
 | 210 |   if ext[1] = '.'
 | 
|---|
 | 211 |      then delete(ext, 1, 1);
 | 
|---|
 | 212 | 
 | 
|---|
 | 213 |   S := ReadRegString(HKEY_CLASSES_ROOT, '.' + ext, '');
 | 
|---|
 | 214 |   if length(s) > 0
 | 
|---|
 | 215 |      then HandleRegInfo(ReadRegString(HKEY_CLASSES_ROOT, s + '\shell\'+Actiontype+'\command', ''))
 | 
|---|
 | 216 | 
 | 
|---|
 | 217 |           //try The open command - a wery exotic way, maybe an oldish way ?
 | 
|---|
 | 218 |      else HandleRegInfo(ReadRegString(HKEY_CLASSES_ROOT, '.' + ext + '\shell\'+Actiontype+'\command', ''));
 | 
|---|
 | 219 | 
 | 
|---|
 | 220 |   if (not result) and (not Sielent)
 | 
|---|
 | 221 |      then KSMessageE('No default '+Actiontype+' program for "'+Ext+'"');
 | 
|---|
 | 222 | end;
 | 
|---|
 | 223 | 
 | 
|---|
 | 224 | //------------------------------------------------------------------------------
 | 
|---|
 | 225 | function GetExeOpen(Ext: string; var Exefil, Params: string; sielent: boolean = true): Boolean;
 | 
|---|
 | 226 | begin
 | 
|---|
 | 227 |   //asm int 3 end; //trap
 | 
|---|
 | 228 |   result := GetExe_(Ext, 'Open', Exefil, Params, sielent);
 | 
|---|
 | 229 | end;
 | 
|---|
 | 230 | //------------------------------------------------------------------------------
 | 
|---|
 | 231 | procedure ExecuteDefaultOpen(ext, aFile: String);
 | 
|---|
 | 232 | var
 | 
|---|
 | 233 |   ExeFil: string;
 | 
|---|
 | 234 |   Params: string;
 | 
|---|
 | 235 | begin
 | 
|---|
 | 236 |   //asm int 3 end; //trap
 | 
|---|
 | 237 |   DeveloperMessage('Findeing default EXE for: '+ext);
 | 
|---|
 | 238 | 
 | 
|---|
 | 239 |   if GetExeOpen(ext, ExeFil, Params, cNotSilent)
 | 
|---|
 | 240 |      then DeveloperMessage('Default EXE for: '+ext+CrLf+ExeFil)
 | 
|---|
 | 241 |      else exit;
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 |   DeveloperMessage('Starting: '+ExeFil);
 | 
|---|
 | 244 | 
 | 
|---|
 | 245 |   if length(Params) > 0
 | 
|---|
 | 246 |      then Params := ' ' + Params + ' "'+ aFile + '"'
 | 
|---|
 | 247 |      else Params := ' "' + aFile + '"';
 | 
|---|
 | 248 | 
 | 
|---|
 | 249 |   if not fileExec('"' + ExeFil + '"' + Params, '', false)
 | 
|---|
 | 250 |      then begin
 | 
|---|
 | 251 |         KSMessageE('Could not run "'+ExtractFileName(ExeFil)+'"');
 | 
|---|
 | 252 |      end;
 | 
|---|
 | 253 | end;
 | 
|---|
 | 254 | //------------------------------------------------------------------------------
 | 
|---|
 | 255 | end.
 | 
|---|