Search code examples
c#vb6msmqpropertybag

How to extract a propertybag or complex object send from VB6 through MSMQ in C#


I'm new to VB6 and also MSMQ. I went through a lot of tutorials online but seems like there is no solution for my question.

I managed to sending from C# to C# or VB6 to VB6 but not from VB6 to C# or vice versa. So I wonder is it a way to do that or there is no way to do this kind of communication.

For example: I want to send this to MSMQ

Dim PropBag As PropertyBag
 Set PropBag = New PropertyBag
 PropBag.WriteProperty "Customer", "Bob"
 PropBag.WriteProperty "Product", "MoeHairSuit"
 PropBag.WriteProperty "Quantity", 4

and get the details in C#, there is "Invalid character in the given encoding. Line 1, position 1." error when I use XmlMessageFormatter

Message mes = mq.Receive(new TimeSpan(0, 0, 3));
mes.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
result = mes.Body.ToString();

I also tried to read from the stream but it come out with a weird symbol in my string. Below is the code and this is the output "늓\0\0\b\b휖ꭑ(\0customer\0Bob\0\b\a劑틠4\0product\v\0MoeHairSuit\b調⫳ᄂ.quantity\0"

Message mes;
mes = mq.Receive(new TimeSpan(0, 0, 3));
mes.BodyStream.Position = 0;
byte[] b = new byte[mes.BodyStream.Length];
mes.BodyStream.Read(b, 0, (int)mes.BodyStream.Length);
UnicodeEncoding uniCoder = new UnicodeEncoding();
result = uniCoder.GetString(b);

I get this exception "Cannot deserialize the message passed as an argument. Cannot recognize the serialization format." when using ActiveXMessageFormatter like below

mes = mq.Receive(new TimeSpan(0, 0, 3));
mes.Formatter = new ActiveXMessageFormatter();
result = mes.Body.ToString();

Do you guys have any idea how to do that? Thanks in advanced


Solution

  • I've dealt with this type of problem before and the best solution that I've found is actually to serialize the object into XML - afterwards it doesn't matter what language/platform you use to encode/decode the language as in text format you will always have options. In binary format you are at the mercy of the immediate formatter which won't necessarily work the same way across the platforms (VB6/C#).

    Reference: http://www.codeproject.com/Articles/33296/Serialization-and-De-serialization

    In other words, you will need to have a standard serializer across both platforms and not try to serialize the propertybag itself.