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;

[ComVisible(true)]
[ComImport]
[Guid("7FD52380-4E07-101B-AE2D-08002B2EC713")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPersistStreamInit
{
    // IPersist interface 
    void GetClassID(ref Guid pClassID);

    [PreserveSig]
    int IsDirty();
    [PreserveSig]
    int Load(IStream pstm);
    [PreserveSig]
    int Save(IStream pstm, bool fClearDirty);
    [PreserveSig]
    int GetSizeMax([InAttribute(), Out(), MarshalAs(UnmanagedType.U8)] 
		ref long pcbSize);
    [PreserveSig]
    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).

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s


%d bloggers like this: