An important point about programming with COM objects is that the existence of a COM object does not directly coincide with the scope of its DataFlex wrapper object. The COM object may get created when the DataFlex wrapper object is created but this should not be relied upon. For example, ActiveX controls are usually created when the control is paged (i.e. when it is activated and added to the DataFlex focus tree). In such a case the ActiveX is destroyed again when the control is deactivated. This will happen automatically and as often as the DataFlex wrapper object is activated and deactivated. All the while, the DataFlex wrapper object that you use to communicate with the COM object is still in scope.
Another example is with Automation objects. Most Automation objects must be manually created and released by sending CreateComObject or AttachActiveObject and ReleaseComObject to the DataFlex wrapper object. Once again this cycle could occur multiple times, while the DataFlex wrapper object still remains in scope (look to the Simple Automation COM sample to see this behavior in action).
Attempting to access a COM object's interface while it is not active will produce runtime errors. It is important to protect your code against this.
The DataFlex COM classes provide several ways to ensure that you access the COM Object's interface only when you should:
IsComObjectCreated : Use this function to test whether the COM object is currently active.
OnCreate: Write a handler for this event to set properties each time the COM object is created.
peAutoCreate: Use this property to manipulate the create / release behavior of a COM object with respect to its DataFlex wrapper object.
Setting Properties with the OnCreate Event Handler,
Manipulating COM Object Life Span with peAutoCreate.
Programming with COM – General Topics