0

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

Wayne
  • 3
  • 2
  • Have you tried using an `ActiveXMessageFormatter` instead of an `XmlMessageFormatter`? I've used this in the past for similar scenarios. – Daniel Kelley Jun 25 '14 at 08:54
  • yup, i tried that but it only works for plain text message but not a property bag from VB6. – Wayne Jun 25 '14 at 09:24
  • You sure you should be using UnicodeEncoding?, did you try UTF8 or ASCII encoder in the C# side of things. – Bigtoe Jun 25 '14 at 10:52
  • I tried UTF8 and ASCII too but both of them get worst result. At least I can read the word "customer", "Bob" by using UnicodeEncoding but nothing can I read if using UTF8 and ASCII – Wayne Jun 26 '14 at 00:43

2 Answers2

1

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.

Karell Ste-Marie
  • 1,022
  • 1
  • 10
  • 22
  • 2
    +1 Drop propertybags. Or alternatively, write a VB6 COM DLL (standard serialiser) to read the propertybag and call it from the C#. – MarkJ Jun 26 '14 at 15:41
  • Thanks Karell and @MarkJ, I believe serialize propertybag is not the best way to solve this. I will go with MarkJ suggestion to have a VB6 COM DLL to do that work. Thank guys – Wayne Jun 27 '14 at 00:51
0

The VB6 propertybag store data in binary format. And you try to read the data in text format. That's the whole problem. Unrecognized characters - is a type and a size of the data in the PropertyBag.Try to make the exchange of data in binary form on both sides.

Yargo
  • 58
  • 5
  • This isn't actually an answer - it's more of a comment. There is nothing concrete the OP can take away from this to solve the problem. – Daniel Kelley Jun 25 '14 at 18:28
  • @Yargo I tried 'mes.Formatter = new BinaryMessageFormatter();' too, but how to going to parse this into PropertyBag? I got the byteArray which is same with what i got in VB – Wayne Jun 26 '14 at 00:51