EZ Jcom
Java to COM/ActiveX Bridge

COM Object Persistence

Some COM objects support persistence. Such objects can save their current state to memory or file, and then restore the saved state later.

EZ JCom supports COM object persistence via two methods provided in the class ezjcom.JComObject. These methods are

  • public byte[] JComSaveObjectState()
    Use this method to save the current state of the COM object, and return a byte array containing the saved state.
  • public void JComRestoreObjectState(byte[] savedState)
    Use this method to load the saved state back into the object.
These methods should normally be called on the underlying COM object, not on the interfaces.

The byte array returned by JComSaveObjectState may be saved into a file and loaded later on in a different program or the same program, or passed over a network or other protocol, saved in a database, etc.

Sample usage is shown below:

    MyComObject comObject = new MyComObject();
    // Call methods on interfaces of comObject
    byte[] savedData = comObject.JComSaveObjectState();
    saveBytesToDatabase( savedData );
Later on, to restore the state,
    byte[] savedData = getBytesFromDatabase();
    MyComObject comObject = new MyComObject();
    comObject.JComRestoreObjectState( savedData );
    // Now "comObject" will be in same state as it
    // was when calling JComSaveObjectState

Note that many COM objects do not support persistence. In such cases, JComSaveObjectState and JComRestoreObjectState will throw an exception.

The COM object must support one of the standard COM persistent interfaces, IPersistStreamInit, IPersistStream, IPersistStorage or IPersistFile.