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
DllGetClassObjectCOM entry point.Instead you should trap this exception and return an appropriate
HResultvalue (possiblyE_UNEXPECTED).It could look similar to this:
Edit: Alternatively, it would be simpler to modify your
CreateComObjectmethod to return nil instead of raising an exception. The defaultDllGetClassObjectimplementation inComServwill returnCLASS_E_CLASSNOTAVAILABLEin that case.