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

不想你离开。 提交于 2019-12-12 02:04:54

问题


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


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/24404013/how-to-extract-a-propertybag-or-complex-object-send-from-vb6-through-msmq-in-c-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!