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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
unit MySQLCommonFuncs;
interface
uses
gnugettext, SysUtils, AuxFuncs, Windows, Forms, TntRegistry, TntClasses, TntSysUtils;
function GetMySQLCommandLineClientPath: WideString;
function GetMySQLAdministratorCmd: WideString;
function GetMySQLQueryBrowserCmd: WideString;
procedure RegisterMySQLApplication(Name: WideString; Version: WideString; InstallPath: WideString);
function ScanRegistryForProductVersion(theRoot: HKEY; RegMainPath: WideString; RegAppName: WideString;
Key: WideString; KeyType: Integer): WideString;
//----------------------------------------------------------------------------------------------------------------------
implementation
uses
Math;
//----------------------------------------------------------------------------------------------------------------------
function GetMySQLCommandLineClientPath: WideString;
var
InstallDir: WideString;
begin
InstallDir := ScanRegistryForProductVersion(HKEY_LOCAL_MACHINE, 'SOFTWARE\MySQL AB', 'MySQL Server', 'Location', 0);
if InstallDir <> '' then
begin
Result := InstallDir + 'bin\mysql.exe';
if not FileExists(Result) then
Result := '';
end
else
begin
// No registered server found. Try to find one by looking at usual installation locations.
if FileExists(GetProgramFilesDir + 'MySQL\bin\mysql.exe') then
Result := GetProgramFilesDir + 'MySQL\bin\mysql.exe'
else
if FileExists('c:\mysql\bin\mysql.exe') then
Result := 'c:\mysql\bin\mysql.exe'
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function GetMySQLAdministratorCmd: WideString;
var
InstallDir: WideString;
begin
InstallDir := ScanRegistryForProductVersion(HKEY_CURRENT_USER, 'SOFTWARE\MySQL AB', 'MySQL Administrator', 'Location', 0);
if InstallDir <> '' then
Result := InstallDir + 'MySQLAdministrator.exe'
else
if FileExists(ExtractFilePath(Application.ExeName) + 'MySQLAdministrator.exe') then
Result := ExtractFilePath(Application.ExeName) + 'MySQLAdministrator.exe'
end;
//----------------------------------------------------------------------------------------------------------------------
function GetMySQLQueryBrowserCmd: WideString;
var
InstallDir: WideString;
begin
InstallDir := ScanRegistryForProductVersion(HKEY_CURRENT_USER, 'SOFTWARE\MySQL AB', 'MySQL Query Browser', 'Location', 0);
if InstallDir <> '' then
Result := InstallDir + 'MySQLQueryBrowser.exe'
else
if FileExists(ExtractFilePath(Application.ExeName) + 'MySQLQueryBrowser.exe') then
Result := ExtractFilePath(Application.ExeName) + 'MySQLQueryBrowser.exe'
end;
//----------------------------------------------------------------------------------------------------------------------
function GetSubkeyNameByPrefix(theRoot: HKEY; RegMainPath: WideString; Prefix: WideString) : WideString;
// Returns the last subkey under the main path, which begins with the given prefix.
// This is usually used to find a product that is installed under different version numbers and the assumption is
// that newer versions are later added (and thus come later in the enumeration) than older versions.
var
ParentKey: HKEY;
I: Integer;
NumSubKeys: Integer;
MaxKeySize: Integer;
KeyName: WideString;
KeyLen: Cardinal;
L: Integer;
RegResult: Integer;
begin
Result := '';
if RegOpenKeyExW(theRoot, PWideChar(RegMainPath), 0, KEY_READ, ParentKey) = ERROR_SUCCESS then
begin
if RegQueryInfoKey(ParentKey, nil, nil, nil, @NumSubKeys, @MaxKeySize, nil, nil, nil, nil, nil, nil) = ERROR_SUCCESS then
begin
// Add room for terminating 0.
Inc(MaxKeySize);
L := Length(Prefix);
SetLength(KeyName, MaxKeySize);
for I := 0 to NumSubKeys - 1 do
begin
KeyLen := MaxKeySize;
RegResult := RegEnumKeyExW(ParentKey, I, PWideChar(KeyName), KeyLen, nil, nil, nil, nil);
if RegResult = ERROR_SUCCESS then
begin
if WideSameText(Prefix, Copy(KeyName, 1, Min(L, KeyLen))) then
begin
Result := Copy(KeyName, 1, KeyLen);
// Dont break as we are looking for the last key (in the hope the last key is also that with the
// highest version number).
end;
end;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function ScanRegistryForProductVersion(theRoot: HKEY; RegMainPath: WideString; RegAppName: WideString; Key: WideString;
KeyType: Integer): WideString;
var
FullAppKey: WideString;
begin
FullAppKey := RegMainPath + '\' + GetSubkeyNameByPrefix(theRoot, RegMainPath, RegAppName);
Result := ReadFromReg(theRoot, FullAppKey, Key, KeyType, '', False);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure RegisterMySQLApplication(Name: WideString; Version: WideString; InstallPath: WideString);
var
RegVersion: WideString;
KeyName: WideString;
MainVersion: WideString;
p, p2: integer;
begin
MainVersion := '';
p := Pos('.', Version);
if p > 0 then
begin
p2 := Pos('.', Copy(Version, p + 1, Length(Version)));
if p2 > 0 then
MainVersion := Copy(Version, 1, p2 + 1);
end;
if MainVersion <> '' then
KeyName := Name + ' ' + MainVersion
else
KeyName := Name;
RegVersion := ReadFromReg(HKEY_CURRENT_USER, '\SOFTWARE\MySQL AB\'+KeyName, 'Version', 0, '');
if RegVersion <> Version then
begin
WriteToReg(HKEY_CURRENT_USER, '\SOFTWARE\MySQL AB\' + KeyName, 'Version', 0, Version);
WriteToReg(HKEY_CURRENT_USER, '\SOFTWARE\MySQL AB\' + KeyName, 'Location', 0, InstallPath);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
end.
|