1 | { *********************************************************************** }
|
---|
2 | { }
|
---|
3 | { Delphi Runtime Library }
|
---|
4 | { }
|
---|
5 | { Copyright (c) 1997-2001 Borland Software Corporation }
|
---|
6 | { }
|
---|
7 | { *********************************************************************** }
|
---|
8 |
|
---|
9 | {*******************************************************}
|
---|
10 | { COM server support }
|
---|
11 | {*******************************************************}
|
---|
12 |
|
---|
13 | unit uComServ;
|
---|
14 |
|
---|
15 | {$DENYPACKAGEUNIT}
|
---|
16 |
|
---|
17 | interface
|
---|
18 |
|
---|
19 | uses Windows, Messages, ActiveX, SysUtils, ComObj;
|
---|
20 |
|
---|
21 | type
|
---|
22 |
|
---|
23 | { Application start mode }
|
---|
24 |
|
---|
25 | TStartMode = (smStandalone, smAutomation, smRegServer, smUnregServer);
|
---|
26 |
|
---|
27 | { Class manager event types }
|
---|
28 |
|
---|
29 | TLastReleaseEvent = procedure(var Shutdown: Boolean) of object;
|
---|
30 |
|
---|
31 | { TComServer }
|
---|
32 |
|
---|
33 | TComServer = class(TComServerObject)
|
---|
34 | private
|
---|
35 | FObjectCount: Integer;
|
---|
36 | FFactoryCount: Integer;
|
---|
37 | FTypeLib: ITypeLib;
|
---|
38 | FServerName: string;
|
---|
39 | FHelpFileName: string;
|
---|
40 | FIsInprocServer: Boolean;
|
---|
41 | FStartMode: TStartMode;
|
---|
42 | FStartSuspended: Boolean;
|
---|
43 | FRegister: Boolean;
|
---|
44 | FUIInteractive: Boolean;
|
---|
45 | FOnLastRelease: TLastReleaseEvent;
|
---|
46 | procedure FactoryFree(Factory: TComObjectFactory);
|
---|
47 | procedure FactoryRegisterClassObject(Factory: TComObjectFactory);
|
---|
48 | procedure FactoryUpdateRegistry(Factory: TComObjectFactory);
|
---|
49 | procedure LastReleased;
|
---|
50 | protected
|
---|
51 | function CountObject(Created: Boolean): Integer; override;
|
---|
52 | function CountFactory(Created: Boolean): Integer; override;
|
---|
53 | function GetHelpFileName: string; override;
|
---|
54 | function GetServerFileName: string; override;
|
---|
55 | function GetServerKey: string; override;
|
---|
56 | function GetServerName: string; override;
|
---|
57 | function GetStartSuspended: Boolean; override;
|
---|
58 | function GetTypeLib: ITypeLib; override;
|
---|
59 | procedure SetHelpFileName(const Value: string); override;
|
---|
60 | public
|
---|
61 | constructor Create;
|
---|
62 | destructor Destroy; override;
|
---|
63 | procedure Initialize;
|
---|
64 | procedure LoadTypeLib;
|
---|
65 | procedure SetServerName(const Name: string);
|
---|
66 | procedure UpdateRegistry(Register: Boolean);
|
---|
67 | property IsInprocServer: Boolean read FIsInprocServer write FIsInprocServer;
|
---|
68 | property ObjectCount: Integer read FObjectCount;
|
---|
69 | property StartMode: TStartMode read FStartMode;
|
---|
70 | property UIInteractive: Boolean read FUIInteractive write FUIInteractive;
|
---|
71 | property OnLastRelease: TLastReleaseEvent read FOnLastRelease write FOnLastRelease;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | var
|
---|
75 | ComServer: TComServer;
|
---|
76 |
|
---|
77 | function DllGetClassObject(const CLSID, IID: TGUID; var Obj): HResult; stdcall;
|
---|
78 | function DllCanUnloadNow: HResult; stdcall;
|
---|
79 | function DllRegisterServer: HResult; stdcall;
|
---|
80 | function DllUnregisterServer: HResult; stdcall;
|
---|
81 |
|
---|
82 | implementation
|
---|
83 |
|
---|
84 | uses ComConst;
|
---|
85 |
|
---|
86 | function GetModuleFileName: string;
|
---|
87 | var
|
---|
88 | Buffer: array[0..261] of Char;
|
---|
89 | begin
|
---|
90 | SetString(Result, Buffer, Windows.GetModuleFileName(HInstance,
|
---|
91 | Buffer, SizeOf(Buffer)));
|
---|
92 | end;
|
---|
93 |
|
---|
94 | function GetModuleName: string;
|
---|
95 | begin
|
---|
96 | Result := ChangeFileExt(ExtractFileName(GetModuleFileName), '');
|
---|
97 | end;
|
---|
98 |
|
---|
99 | function LoadTypeLibrary(const ModuleName: string): ITypeLib;
|
---|
100 | begin
|
---|
101 | OleCheck(LoadTypeLib(PWideChar(WideString(ModuleName)), Result));
|
---|
102 | end;
|
---|
103 |
|
---|
104 | procedure RegisterTypeLibrary(TypeLib: ITypeLib; const ModuleName: string);
|
---|
105 | var
|
---|
106 | Name: WideString;
|
---|
107 | HelpPath: WideString;
|
---|
108 | begin
|
---|
109 | Name := ModuleName;
|
---|
110 | HelpPath := ExtractFilePath(ModuleName);
|
---|
111 | OleCheck(RegisterTypeLib(TypeLib, PWideChar(Name), PWideChar(HelpPath)));
|
---|
112 | end;
|
---|
113 |
|
---|
114 | procedure UnregisterTypeLibrary(TypeLib: ITypeLib);
|
---|
115 | type
|
---|
116 | TUnregisterProc = function(const GUID: TGUID; VerMajor, VerMinor: Word;
|
---|
117 | LCID: TLCID; SysKind: TSysKind): HResult stdcall;
|
---|
118 | var
|
---|
119 | Handle: THandle;
|
---|
120 | UnregisterProc: TUnregisterProc;
|
---|
121 | LibAttr: PTLibAttr;
|
---|
122 | begin
|
---|
123 | Handle := GetModuleHandle('OLEAUT32.DLL');
|
---|
124 | if Handle <> 0 then
|
---|
125 | begin
|
---|
126 | @UnregisterProc := GetProcAddress(Handle, 'UnRegisterTypeLib');
|
---|
127 | if @UnregisterProc <> nil then
|
---|
128 | begin
|
---|
129 | OleCheck(ComServer.TypeLib.GetLibAttr(LibAttr));
|
---|
130 | with LibAttr^ do
|
---|
131 | UnregisterProc(guid, wMajorVerNum, wMinorVerNum, lcid, syskind);
|
---|
132 | ComServer.TypeLib.ReleaseTLibAttr(LibAttr);
|
---|
133 | end;
|
---|
134 | end;
|
---|
135 | end;
|
---|
136 |
|
---|
137 | function GetTypeLibName(TypeLib: ITypeLib): string;
|
---|
138 | var
|
---|
139 | Name: WideString;
|
---|
140 | begin
|
---|
141 | OleCheck(TypeLib.GetDocumentation(-1, @Name, nil, nil, nil));
|
---|
142 | Result := Name;
|
---|
143 | end;
|
---|
144 |
|
---|
145 | function DllGetClassObject(const CLSID, IID: TGUID; var Obj): HResult;
|
---|
146 | var
|
---|
147 | Factory: TComObjectFactory;
|
---|
148 | begin
|
---|
149 | Factory := ComClassManager.GetFactoryFromClassID(CLSID);
|
---|
150 | if Factory <> nil then
|
---|
151 | if Factory.GetInterface(IID, Obj) then
|
---|
152 | Result := S_OK
|
---|
153 | else
|
---|
154 | Result := E_NOINTERFACE
|
---|
155 | else
|
---|
156 | begin
|
---|
157 | Pointer(Obj) := nil;
|
---|
158 | Result := CLASS_E_CLASSNOTAVAILABLE;
|
---|
159 | end;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | function DllCanUnloadNow: HResult;
|
---|
163 | begin
|
---|
164 | if (ComServer = nil) or
|
---|
165 | ((ComServer.FObjectCount = 0) and (ComServer.FFactoryCount = 0)) then
|
---|
166 | Result := S_OK
|
---|
167 | else
|
---|
168 | Result := S_FALSE;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | function DllRegisterServer: HResult;
|
---|
172 | begin
|
---|
173 | Result := S_OK;
|
---|
174 | try
|
---|
175 | ComServer.UpdateRegistry(True);
|
---|
176 | except
|
---|
177 | Result := E_FAIL;
|
---|
178 | end;
|
---|
179 | end;
|
---|
180 |
|
---|
181 | function DllUnregisterServer: HResult;
|
---|
182 | begin
|
---|
183 | Result := S_OK;
|
---|
184 | try
|
---|
185 | ComServer.UpdateRegistry(False);
|
---|
186 | except
|
---|
187 | Result := E_FAIL;
|
---|
188 | end;
|
---|
189 | end;
|
---|
190 |
|
---|
191 | { Automation TerminateProc }
|
---|
192 |
|
---|
193 | function AutomationTerminateProc: Boolean;
|
---|
194 | begin
|
---|
195 | Result := True;
|
---|
196 | if (ComServer <> nil) and (ComServer.ObjectCount > 0) and ComServer.UIInteractive then
|
---|
197 | begin
|
---|
198 | Result := MessageBox(0, PChar(SNoCloseActiveServer1 + SNoCloseActiveServer2),
|
---|
199 | PChar(SAutomationWarning), MB_YESNO or MB_TASKMODAL or
|
---|
200 | MB_ICONWARNING or MB_DEFBUTTON2) = IDYES;
|
---|
201 | end;
|
---|
202 | end;
|
---|
203 |
|
---|
204 | { TComServer }
|
---|
205 |
|
---|
206 | constructor TComServer.Create;
|
---|
207 |
|
---|
208 | function FindSwitch(const Switch: string): Boolean;
|
---|
209 | begin
|
---|
210 | Result := FindCmdLineSwitch(Switch, ['-', '/'], True);
|
---|
211 | end;
|
---|
212 |
|
---|
213 | begin
|
---|
214 | FTypeLib := nil;
|
---|
215 | FIsInprocServer := ModuleIsLib;
|
---|
216 | if FindSwitch('AUTOMATION') or FindSwitch('EMBEDDING') then
|
---|
217 | FStartMode := smAutomation
|
---|
218 | else if FindSwitch('REGSERVER') then
|
---|
219 | FStartMode := smRegServer
|
---|
220 | else if FindSwitch('UNREGSERVER') then
|
---|
221 | FStartMode := smUnregServer;
|
---|
222 | FUIInteractive := True;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | destructor TComServer.Destroy;
|
---|
226 | begin
|
---|
227 | ComClassManager.ForEachFactory(Self, FactoryFree);
|
---|
228 | end;
|
---|
229 |
|
---|
230 | function TComServer.CountObject(Created: Boolean): Integer;
|
---|
231 | begin
|
---|
232 | if Created then
|
---|
233 | begin
|
---|
234 | Result := InterlockedIncrement(FObjectCount);
|
---|
235 | if (not IsInProcServer) and (StartMode = smAutomation)
|
---|
236 | and Assigned(ComObj.CoAddRefServerProcess) then
|
---|
237 | ComObj.CoAddRefServerProcess;
|
---|
238 | end
|
---|
239 | else
|
---|
240 | begin
|
---|
241 | Result := InterlockedDecrement(FObjectCount);
|
---|
242 | if (not IsInProcServer) and (StartMode = smAutomation)
|
---|
243 | and Assigned(ComObj.CoReleaseServerProcess) then
|
---|
244 | begin
|
---|
245 | if ComObj.CoReleaseServerProcess = 0 then
|
---|
246 | LastReleased;
|
---|
247 | end
|
---|
248 | else if Result = 0 then
|
---|
249 | LastReleased;
|
---|
250 | end;
|
---|
251 | end;
|
---|
252 |
|
---|
253 | function TComServer.CountFactory(Created: Boolean): Integer;
|
---|
254 | begin
|
---|
255 | if Created then
|
---|
256 | Result := InterlockedIncrement(FFactoryCount)
|
---|
257 | else
|
---|
258 | Result := InterlockedDecrement(FFactoryCount);
|
---|
259 | end;
|
---|
260 |
|
---|
261 | procedure TComServer.FactoryFree(Factory: TComObjectFactory);
|
---|
262 | begin
|
---|
263 | Factory.Free;
|
---|
264 | end;
|
---|
265 |
|
---|
266 | procedure TComServer.FactoryRegisterClassObject(Factory: TComObjectFactory);
|
---|
267 | begin
|
---|
268 | Factory.RegisterClassObject;
|
---|
269 | end;
|
---|
270 |
|
---|
271 | procedure TComServer.FactoryUpdateRegistry(Factory: TComObjectFactory);
|
---|
272 | begin
|
---|
273 | if Factory.Instancing <> ciInternal then
|
---|
274 | Factory.UpdateRegistry(FRegister);
|
---|
275 | end;
|
---|
276 |
|
---|
277 | function TComServer.GetHelpFileName: string;
|
---|
278 | begin
|
---|
279 | Result := FHelpFileName;
|
---|
280 | end;
|
---|
281 |
|
---|
282 | function TComServer.GetServerFileName: string;
|
---|
283 | begin
|
---|
284 | Result := GetModuleFileName;
|
---|
285 | end;
|
---|
286 |
|
---|
287 | function TComServer.GetServerKey: string;
|
---|
288 | begin
|
---|
289 | if FIsInprocServer then
|
---|
290 | Result := 'InprocServer32' else
|
---|
291 | Result := 'LocalServer32';
|
---|
292 | end;
|
---|
293 |
|
---|
294 | function TComServer.GetServerName: string;
|
---|
295 | begin
|
---|
296 | if FServerName <> '' then
|
---|
297 | Result := FServerName
|
---|
298 | else
|
---|
299 | if FTypeLib <> nil then
|
---|
300 | Result := GetTypeLibName(FTypeLib)
|
---|
301 | else
|
---|
302 | Result := GetModuleName;
|
---|
303 | end;
|
---|
304 |
|
---|
305 | procedure TComServer.SetServerName(const Name: string);
|
---|
306 | begin
|
---|
307 | if FTypeLib = nil then
|
---|
308 | FServerName := Name;
|
---|
309 | end;
|
---|
310 |
|
---|
311 | function TComServer.GetTypeLib: ITypeLib;
|
---|
312 | begin
|
---|
313 | LoadTypeLib;
|
---|
314 | Result := FTypeLib;
|
---|
315 | end;
|
---|
316 |
|
---|
317 |
|
---|
318 | procedure TComServer.LastReleased;
|
---|
319 | var
|
---|
320 | Shutdown: Boolean;
|
---|
321 | begin
|
---|
322 | if not FIsInprocServer then
|
---|
323 | begin
|
---|
324 | Shutdown := FStartMode = smAutomation;
|
---|
325 | try
|
---|
326 | if Assigned(FOnLastRelease) then FOnLastRelease(Shutdown);
|
---|
327 | finally
|
---|
328 | if Shutdown then PostThreadMessage(MainThreadID, WM_QUIT, 0, 0);
|
---|
329 | end;
|
---|
330 | end;
|
---|
331 | end;
|
---|
332 |
|
---|
333 | procedure TComServer.LoadTypeLib;
|
---|
334 | var
|
---|
335 | Temp: ITypeLib;
|
---|
336 | begin
|
---|
337 | if FTypeLib = nil then
|
---|
338 | begin
|
---|
339 | // this may load typelib more than once, but avoids need for critical section
|
---|
340 | // and releases the interface correctly
|
---|
341 | Temp := LoadTypeLibrary(GetModuleFileName);
|
---|
342 | Integer(Temp) := InterlockedExchange(Integer(FTypeLib), Integer(Temp));
|
---|
343 | end;
|
---|
344 | end;
|
---|
345 |
|
---|
346 | procedure TComServer.UpdateRegistry(Register: Boolean);
|
---|
347 | begin
|
---|
348 | if FTypeLib <> nil then
|
---|
349 | if Register then
|
---|
350 | RegisterTypeLibrary(FTypeLib, GetModuleFileName) else
|
---|
351 | UnregisterTypeLibrary(FTypeLib);
|
---|
352 | FRegister := Register;
|
---|
353 | ComClassManager.ForEachFactory(Self, FactoryUpdateRegistry);
|
---|
354 | end;
|
---|
355 |
|
---|
356 | var
|
---|
357 | SaveInitProc: Pointer = nil;
|
---|
358 | OleAutHandle: Integer;
|
---|
359 |
|
---|
360 | procedure InitComServer;
|
---|
361 | begin
|
---|
362 | if SaveInitProc <> nil then TProcedure(SaveInitProc);
|
---|
363 | ComServer.FStartSuspended := (CoInitFlags <> -1) and
|
---|
364 | Assigned(ComObj.CoInitializeEx) and Assigned(ComObj.CoResumeClassObjects);
|
---|
365 | ComServer.Initialize;
|
---|
366 | if ComServer.FStartSuspended then
|
---|
367 | ComObj.CoResumeClassObjects;
|
---|
368 | end;
|
---|
369 |
|
---|
370 | function TComServer.GetStartSuspended: Boolean;
|
---|
371 | begin
|
---|
372 | Result := FStartSuspended;
|
---|
373 | end;
|
---|
374 |
|
---|
375 | procedure TComServer.SetHelpFileName(const Value: string);
|
---|
376 | begin
|
---|
377 | FHelpFileName := Value;
|
---|
378 | end;
|
---|
379 | { older proc replaced by one below this
|
---|
380 | procedure TComServer.Initialize;
|
---|
381 | begin
|
---|
382 | try
|
---|
383 | UpdateRegistry(FStartMode <> smUnregServer);
|
---|
384 | except
|
---|
385 | on E: EOleRegistrationError do
|
---|
386 | // User may not have write access to the registry.
|
---|
387 | // Squelch the exception unless we were explicitly told to register.
|
---|
388 | if FStartMode = smRegServer then raise;
|
---|
389 | end;
|
---|
390 | if FStartMode in [smRegServer, smUnregServer] then Halt;
|
---|
391 | ComClassManager.ForEachFactory(Self, FactoryRegisterClassObject);
|
---|
392 | end;
|
---|
393 | }
|
---|
394 | procedure TComServer.Initialize;
|
---|
395 | begin
|
---|
396 | try
|
---|
397 | UpdateRegistry(FStartMode <> smUnregServer);
|
---|
398 | except
|
---|
399 | on E: EOleRegistrationError do
|
---|
400 | // User may not have write access to the registry.
|
---|
401 | // Squelch the exception unless we were explicitly told to register.
|
---|
402 | if FStartMode = smRegServer then raise;
|
---|
403 | on E: EOleSysError do
|
---|
404 | if FStartMode = smRegServer then raise;
|
---|
405 | end;
|
---|
406 | if FStartMode in [smRegServer, smUnregServer] then Halt;
|
---|
407 | ComClassManager.ForEachFactory(Self, FactoryRegisterClassObject);
|
---|
408 | end;
|
---|
409 | initialization
|
---|
410 | begin
|
---|
411 | OleAutHandle := SafeLoadLibrary('OLEAUT32.DLL');
|
---|
412 | ComServer := TComServer.Create;
|
---|
413 | if not ModuleIsLib then
|
---|
414 | begin
|
---|
415 | SaveInitProc := InitProc;
|
---|
416 | InitProc := @InitComServer;
|
---|
417 | AddTerminateProc(@AutomationTerminateProc);
|
---|
418 | end;
|
---|
419 | end;
|
---|
420 |
|
---|
421 | finalization
|
---|
422 | begin
|
---|
423 | ComServer.Free;
|
---|
424 | ComServer := nil;
|
---|
425 | FreeLibrary(OleAutHandle);
|
---|
426 | end;
|
---|
427 |
|
---|
428 | end.
|
---|