System.Messaging, COM objects and PropertyBags

I’m posting this pretty much as a reminder to myself.

There is a bug in VB6 which means that when you try to write a persistable VB6 COM object to an MSMQ queue using .NET’s System.Messaging then it fails with error 0x800A02E0:

Cannot save an uninitialized class. You must use the global InitProperties method to initialize the class, or load the class from a PropertyBag before trying to save it.

The workaround is to call the InitNew method on the IPersistStreamInit interface of the COM object. In .NET however this is easier said than done. The following goo is needed:

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

public interface IPersistStreamInit
    // IPersist interface 
    void GetClassID(ref Guid pClassID);

    int IsDirty();
    int Load(IStream pstm);
    int Save(IStream pstm, bool fClearDirty);
    int GetSizeMax([InAttribute(), Out(), MarshalAs(UnmanagedType.U8)] 
		ref long pcbSize);
    int InitNew();

You can then go ahead and cast your object to IPersistStreamInit and call the InitNew method. The object will now persist to the queue with no problems (remember to set up your Message to use the ActiveXFormatter though).


One Response to “System.Messaging, COM objects and PropertyBags”

  1. EstT Says:

    Thanks! You’re a life saver!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: