1 |
tony |
45 |
procedure TFBClientAPI.LoadIBLibrary; |
2 |
|
|
|
3 |
tony |
56 |
function IsValidHandle(aHandle: TLibHandle): boolean; |
4 |
|
|
begin |
5 |
|
|
Result := aHandle > HINSTANCE_ERROR; |
6 |
|
|
end; |
7 |
|
|
|
8 |
tony |
45 |
function DoLoadLibrary(LibName: string): TLibHandle; |
9 |
|
|
begin |
10 |
|
|
Result := LoadLibrary(PChar(LibName)); |
11 |
tony |
56 |
if IsValidHandle(Result) then |
12 |
tony |
45 |
begin |
13 |
|
|
FFBLibraryName := ExtractFileName(LibName); |
14 |
|
|
FFBLibraryPath := ExtractFileDir(LibName); |
15 |
|
|
end; |
16 |
|
|
end; |
17 |
|
|
|
18 |
tony |
56 |
function GetSpecialFolder(const CSIDL: integer) : string; |
19 |
|
|
{$IFDEF FPC} |
20 |
|
|
begin |
21 |
|
|
Result := GetWindowsSpecialDir(CSIDL); |
22 |
|
|
end; |
23 |
|
|
{$ELSE} |
24 |
|
|
var |
25 |
|
|
RecPath : PChar; |
26 |
|
|
begin |
27 |
|
|
RecPath := StrAlloc(MAX_PATH); |
28 |
|
|
try |
29 |
|
|
FillChar(RecPath^, MAX_PATH, 0); |
30 |
|
|
if SHGetSpecialFolderPath(0, RecPath, CSIDL, false) |
31 |
|
|
then result := RecPath |
32 |
|
|
else result := ''; |
33 |
|
|
finally |
34 |
|
|
StrDispose(RecPath); |
35 |
|
|
end; |
36 |
|
|
end; |
37 |
|
|
{$ENDIF} |
38 |
|
|
|
39 |
tony |
68 |
var curPath: string; |
40 |
|
|
|
41 |
|
|
procedure Add2Path(dirname: string); |
42 |
|
|
var newPath: string; |
43 |
|
|
begin |
44 |
|
|
newPath := dirname + ';' + curPath; |
45 |
|
|
SetEnvironmentVariable('PATH',PChar(newPath)); |
46 |
|
|
end; |
47 |
|
|
|
48 |
tony |
45 |
var InstallDir: string; |
49 |
|
|
dllPathName: string; |
50 |
|
|
oldFirebirdEV: string; |
51 |
|
|
begin |
52 |
tony |
56 |
if IsValidHandle(IBLibrary) then Exit; |
53 |
tony |
45 |
|
54 |
tony |
68 |
curPath := GetEnvironmentVariable('PATH'); |
55 |
|
|
|
56 |
tony |
45 |
{First try any user override} |
57 |
|
|
dllPathName := GetOverrideLibName; |
58 |
|
|
if dllPathName <> '' then |
59 |
|
|
begin |
60 |
|
|
IBLibrary := DoLoadLibrary(dllPathName); |
61 |
tony |
56 |
FOwnsIBLibrary := IsValidHandle(IBLibrary); |
62 |
tony |
45 |
Exit; |
63 |
|
|
end; |
64 |
|
|
|
65 |
|
|
{Then look in application installation directory} |
66 |
|
|
InstallDir := ExtractFilePath(Paramstr(0)); {Using ParamStr(0) assumes windows conventions} |
67 |
|
|
|
68 |
|
|
//First look for Firebird Embedded Server in installation dir |
69 |
|
|
if FileExists(InstallDir + FIREBIRD_EMBEDDED) then |
70 |
|
|
begin |
71 |
|
|
dllPathName := InstallDir + FIREBIRD_EMBEDDED; |
72 |
|
|
IBLibrary := DoLoadLibrary(dllPathName) |
73 |
|
|
end |
74 |
|
|
else |
75 |
|
|
//Otherwise look for Firebird Client in installation dir |
76 |
|
|
if FileExists(InstallDir + FIREBIRD_CLIENT) then |
77 |
|
|
begin |
78 |
|
|
//assume firebird.conf and firebird.msg in same dir |
79 |
|
|
oldFirebirdEV := GetEnvironmentVariable('FIREBIRD'); |
80 |
|
|
SetEnvironmentVariable('FIREBIRD',PChar(InstallDir)); |
81 |
|
|
dllPathName := InstallDir + FIREBIRD_CLIENT; |
82 |
|
|
try |
83 |
|
|
IBLibrary := DoLoadLibrary(dllPathName) |
84 |
|
|
finally |
85 |
tony |
56 |
if not IsValidHandle(IBLibrary) then |
86 |
tony |
45 |
SetEnvironmentVariable('FIREBIRD',PChar(oldFirebirdEV)); {restore} |
87 |
|
|
end; |
88 |
|
|
end; |
89 |
|
|
|
90 |
|
|
// writeln('Dir = ',InstallDir); |
91 |
|
|
{If FIREBIRD environment variable available then try this} |
92 |
tony |
56 |
if not IsValidHandle(IBLibrary) then |
93 |
tony |
45 |
begin |
94 |
|
|
InstallDir := GetEnvironmentVariable('FIREBIRD'); |
95 |
|
|
if (length(InstallDir) > 0) and (InstallDir[length(InstallDir)] <> DirectorySeparator) then |
96 |
tony |
56 |
InstallDir := InstallDir + DirectorySeparator; |
97 |
tony |
45 |
if (InstallDir <> '') and FileExists(InstallDir + FIREBIRD_CLIENT) then |
98 |
|
|
begin |
99 |
|
|
//assume firebird.conf and firebird.msg in same dir |
100 |
|
|
dllPathName := InstallDir + FIREBIRD_CLIENT; |
101 |
tony |
68 |
Add2Path(InstallDir); |
102 |
tony |
45 |
IBLibrary := DoLoadLibrary(dllPathName) |
103 |
|
|
end |
104 |
|
|
else |
105 |
tony |
68 |
if (InstallDir <> '') then |
106 |
tony |
45 |
begin |
107 |
tony |
68 |
InstallDir := InstallDir + 'bin' + DirectorySeparator; |
108 |
|
|
if FileExists(InstallDir + FIREBIRD_CLIENT) then |
109 |
|
|
begin |
110 |
|
|
dllPathName := InstallDir + FIREBIRD_CLIENT; |
111 |
|
|
Add2Path(InstallDir); |
112 |
|
|
IBLibrary := DoLoadLibrary(dllPathName) |
113 |
|
|
end |
114 |
tony |
45 |
end |
115 |
|
|
end; |
116 |
|
|
|
117 |
tony |
56 |
if not IsValidHandle(IBLibrary) then |
118 |
tony |
68 |
{Use Registry key if it exists to locate library Firebird 2 only} |
119 |
tony |
45 |
begin |
120 |
|
|
with TRegistry.Create do |
121 |
|
|
try |
122 |
|
|
RootKey := HKEY_LOCAL_MACHINE; |
123 |
|
|
if OpenKey('SOFTWARE\Firebird Project\Firebird Server\Instances',false) then |
124 |
|
|
begin |
125 |
|
|
if ValueExists('DefaultInstance') then |
126 |
|
|
begin |
127 |
|
|
InstallDir := ReadString('DefaultInstance') + 'bin' + DirectorySeparator ; |
128 |
|
|
dllPathName := InstallDir + FIREBIRD_CLIENT; |
129 |
tony |
68 |
Add2Path(InstallDir); |
130 |
tony |
45 |
IBLibrary := DoLoadLibrary(dllPathName) |
131 |
|
|
end |
132 |
|
|
end |
133 |
|
|
finally |
134 |
|
|
Free |
135 |
|
|
end; |
136 |
|
|
|
137 |
|
|
{Now try default install dir} |
138 |
tony |
56 |
if not IsValidHandle(IBLibrary) then |
139 |
tony |
45 |
begin |
140 |
tony |
56 |
InstallDir := GetSpecialFolder(CSIDL_PROGRAM_FILES) + |
141 |
tony |
68 |
'Firebird' + |
142 |
|
|
DirectorySeparator + 'Firebird_3_0' + DirectorySeparator; |
143 |
|
|
dllPathName := InstallDir + FIREBIRD_CLIENT; |
144 |
|
|
Add2Path(InstallDir); |
145 |
tony |
45 |
IBLibrary := DoLoadLibrary(dllPathName) |
146 |
|
|
end; |
147 |
|
|
|
148 |
tony |
56 |
if not IsValidHandle(IBLibrary) then |
149 |
tony |
45 |
begin |
150 |
tony |
56 |
InstallDir := GetSpecialFolder(CSIDL_PROGRAM_FILES) + |
151 |
tony |
68 |
'Firebird' + |
152 |
tony |
45 |
DirectorySeparator + 'Firebird_2_5' + |
153 |
|
|
DirectorySeparator + 'bin' + DirectorySeparator; |
154 |
|
|
dllPathName := InstallDir + FIREBIRD_CLIENT; |
155 |
tony |
68 |
Add2Path(InstallDir); |
156 |
tony |
45 |
IBLibrary := DoLoadLibrary(dllPathName) |
157 |
|
|
end; |
158 |
|
|
|
159 |
tony |
56 |
if not IsValidHandle(IBLibrary) then |
160 |
tony |
45 |
begin |
161 |
tony |
56 |
InstallDir := GetSpecialFolder(CSIDL_PROGRAM_FILES) + |
162 |
tony |
68 |
'Firebird' + |
163 |
tony |
45 |
DirectorySeparator + 'Firebird_2_1' + |
164 |
|
|
DirectorySeparator + 'bin' + DirectorySeparator; |
165 |
|
|
dllPathName := InstallDir + FIREBIRD_CLIENT; |
166 |
tony |
68 |
Add2Path(InstallDir); |
167 |
tony |
45 |
IBLibrary := DoLoadLibrary(dllPathName) |
168 |
|
|
end; |
169 |
|
|
|
170 |
|
|
//Otherwise see if Firebird client is in path |
171 |
|
|
//and rely on registry for location of firebird.conf and firebird.msg |
172 |
tony |
56 |
if not IsValidHandle(IBLibrary) then |
173 |
tony |
45 |
begin |
174 |
tony |
68 |
SetEnvironmentVariable('PATH',PChar(curPath)); |
175 |
tony |
45 |
IBLibrary := DoLoadLibrary(FIREBIRD_CLIENT); |
176 |
|
|
if IBLibrary <= HINSTANCE_ERROR then |
177 |
|
|
//well maybe InterBase is present... |
178 |
|
|
IBLibrary := DoLoadLibrary(IBASE_DLL); |
179 |
|
|
end; |
180 |
|
|
end; |
181 |
tony |
56 |
FOwnsIBLibrary := IsValidHandle(IBLibrary); |
182 |
tony |
45 |
end; |
183 |
|
|
|
184 |
|
|
|