I have a custom COM server implementation. The server is using a TComObjectFactory to create the COM objects that are used for client connections.
I had written it like this:
function TPsOPCProviderClientFactory.CreateComObject(const Controller:
IUnknown): TComObject;
begin
FStartupCompleteEvent.WaitFor(INFINITE);
if Provider.State = OPC_STATUS_SUSPENDED then // Server already shutdown
raise EAbort.Create('Server shut down.');
Result := inherited CreateComObject(Controller);
(Result as TPsOPCProviderClient).Provider := Provider;
end;
The FStartupCompleteEvent is used to ensure that the application is ready before the first connection is let through and the Provider.State is used to disable client connections when the server is going to shut down.
Now it appears that this is not the proper way to cancel the object creation: if the exception is raised it will make a message dialog with "DAX Error" popup on the screen and the client may crash.
What is the correct way to make the object creation fail - or is it compulsory to always succeed?
The code is used in a component framework that is available for all Delphi/C++Builder versions since Delphi 5.
You shouldn't let any exception escape from your DllGetClassObject COM entry point.
Instead you should trap this exception and return an appropriate HResult value (possibly E_UNEXPECTED).
It could look similar to this:
function DllGetClassObject(const CLSID, IID: TGUID; var Obj): HResult;
begin
try
Result := ComServ.DllGetClassObject(CLSID, IID, Obj);
except
Pointer(Obj) := nil;
Result := E_UNEXPECTED;
end;
end;
Edit: Alternatively, it would be simpler to modify your CreateComObject method to return nil instead of raising an exception. The default DllGetClassObject implementation in ComServ will return CLASS_E_CLASSNOTAVAILABLE in that case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With