如何使用.NET代码从VB6应用程序中读取MSMQ消息?

时间:2008-11-06 18:27:22

标签: .net vb6 msmq

我正在从VB6应用程序向MSMQ编写一个简单的xml字符串,但是当我尝试使用XmlMessageFormatter在C#中从队列中读取消息时,我收到以下错误:

“名称不能以'。'开头。字符“

如何使用.Net代码成功阅读这些消息?

4 个答案:

答案 0 :(得分:2)

我相信你必须使用ActiveXMessageFormatter,而不是XmlMessageFormatter。 XmlMessageFormatter用于在.net应用程序之间发送对象。你发送的不是xml而是字符串。而不是.net字符串。根据ActiveXMessageFormatter的文档,它适用于:

  

序列化或反序列化原语   数据类型和其他对象或   来自消息队列的主体   消息,使用的格式   与MSMQ ActiveX兼容   成分

从vb6发送时,您正在使用msmq com接口。这是ActiveX接口的另一个名称。使用ActiveXMessageFormatter收到字符串后。明确地将其转换为xml对象。

答案 1 :(得分:0)

首先检查您的数据,以确保它确实如错误消息所暗示的那样。如果是,首先将数据读取为文本或二进制,删除有问题的'。',然后使用xmlmessageformatter

答案 2 :(得分:0)

我有一些坏消息。我遵循提供的建议而没有用。

我最终创建了VC ++ COM对象,它会将消息从我的.NET应用程序发布到队列中,这样另一方的VC ++ COM接收器就能理解该消息。

我怀疑您需要创建一个从.NET应用程序调用的VB6 COM对象,以发送消息。

COM对象使用的mqao.dll似乎使用了与.NET不同的格式化程序,甚至ActiveX也不起作用。

显然,这意味着两个队列,一个用于遗留COM应用,一个用于.NET应用。因此,您将为每个目标客户端发送两次相同的消息。

答案 3 :(得分:0)

我刚刚花了一整天的时间来讨论通过格式化程序放入和读取MSMQ的消息,这些格式化程序隐藏在如此多的层抽象和配置中,而我在这一生中无法找到它们。我构建了以下函数作为对任何似乎至少包含一些可读ASCII的msmq消息的暴力攻击:

    private static string MsmqMsgBodyWtf(Message recalcitrantMsmqMessage, bool showHex = false, bool showChars = false)
    {
        recalcitrantMsmqMessage.Formatter = new ActiveXMessageFormatter();
        byte[] bytes = (byte[])recalcitrantMsmqMessage.Formatter.Read(recalcitrantMsmqMessage);
        StringBuilder dottedHex = new StringBuilder();
        StringBuilder dottedAscii = new StringBuilder();
        StringBuilder plainAscii = new StringBuilder();

        for (int i = 0; i < bytes.Length; i++)
        {
            byte b = bytes[i];

            string hexString;
            hexString = String.Format("{0:x2}", b);
            dottedHex.Append(hexString + ".");

            string charString = byte2char(b);
            string escapedCharString = (b > 31 && b < 128) ? charString : "?";
            dottedAscii.Append(escapedCharString + " .");
            plainAscii.Append(escapedCharString);
        }

        StringBuilder composedOutput = new StringBuilder(plainAscii.ToString());
        if (showHex || showChars) composedOutput.Append(System.Environment.NewLine);
        if (showHex) composedOutput.AppendLine(dottedHex.ToString());
        if (showChars) composedOutput.AppendLine(dottedAscii.ToString());

        return composedOutput.ToString(); ;
    }

现在我可以将消息转发到某处并使用其他工具进行分析。 YIPPEE!