【问题标题】:MSMQ c++ to c# problemMSMQ c++ 转 c# 问题
【发布时间】:2011-08-14 13:09:09
【问题描述】:

我想使用 C++ 将消息写入 MSMQ 队列并使用 C# 读取它们。 我将消息作为字符串发送。 如果我在 C++ 中编写和读取消息,它工作正常,但如果我尝试使用 C# 读取消息,我只会得到消息的第一个字符。 (例如:我发送“abcd”,我只收到“a”)。

我还必须提到我在 Windows Mobile 中使用此代码。

这里是c++代码:

HRESULT MSMQManager::WriteMessage(LPWSTR text){

// define the required constants and variables.
const int NUMBEROFPROPERTIES = 5;                   // number of properties
DWORD cPropId = 0;                                  // property counter
HRESULT hr = MQ_OK;                                 // return code
HANDLE hQueue = NULL;                               // queue handle

// define an MQMSGPROPS structure.
MQMSGPROPS msgProps;
MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
HRESULT aMsgStatus[NUMBEROFPROPERTIES];

// message label
aMsgPropId[cPropId] = PROPID_M_LABEL;
aMsgPropVar[cPropId].vt = VT_LPWSTR;
aMsgPropVar[cPropId].pwszVal = L"msg";
cPropId++;

// message body
aMsgPropId[cPropId] = PROPID_M_BODY;
aMsgPropVar[cPropId].vt = VT_VECTOR | VT_UI1;
aMsgPropVar[cPropId].caub.pElems = (LPBYTE)text;
aMsgPropVar[cPropId].caub.cElems = wcslen(text)*2;
cPropId++;

// message body type
aMsgPropId[cPropId] = PROPID_M_BODY_TYPE;
aMsgPropVar[cPropId].vt = VT_UI4;
aMsgPropVar[cPropId].ulVal = VT_LPWSTR;
cPropId++;

// initialize the MQMSGPROPS structure.
msgProps.cProp = cPropId;
msgProps.aPropID = aMsgPropId;
msgProps.aPropVar = aMsgPropVar;
msgProps.aStatus = aMsgStatus;

// Call MQSendMessage to send the message to the queue.
hr = MQSendMessage(
                    this->writeHandle,                          // Queue handle
                    &msgProps,                       // Message property structure
                    MQ_NO_TRANSACTION               // Not in a transaction
                 );

这里是c#代码:

 public string ReadMessageUnformatted()
    {   
        try
        {
            Message received;
            Stream bodyStream = null;
            StreamReader sr = null;
            char[] buffer = new char[256];

            received = this.readMQ.Receive();
            bodyStream = received.BodyStream;
            sr = new StreamReader(bodyStream);
            //this.lastReceived = sr.ReadLine();
            sr.Read(buffer, 0, 256);

            this.lastReceived = new string(buffer);

            return lastReceived;
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.ToString(), "Exception");
            return null;
        }
    }

我使用 BodyStream 而不是 Body 作为消息,因为我不想使用任何消息格式化程序。

谢谢

【问题讨论】:

  • 它是否适用于string text = sr.ReadToEnd()
  • 不,它也不适用于 ReadToEnd()。这样应用程序会阻塞等待流的结束。你认为这可能是一个问题,因为我将消息的正文设置为指针?
  • 看看我的答案,也许在那儿。

标签: c# c++ msmq


【解决方案1】:

我自己解决了这个问题。我会把代码贴在这里,也许有其他人感兴趣。

        try
        {
            Message received;
            Stream bodyStream = null;
            int bufLength = 512;
            byte[] buffer = new byte[bufLength];                

            received = this.readMQ.Receive();
            bodyStream = received.BodyStream;
            bodyStream.Read(buffer, 0, bufLength);
            this.lastReceived = Encoding.Unicode.GetString(buffer, 0, buffer.Length);

            return lastReceived;
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.ToString(), "Exception");
            return null;
        }

【讨论】:

  • 巨大的+1。我自己也遇到了这个问题。拯救了这一天!
【解决方案2】:

也许你可以试试:

aMsgPropVar[cPropId].ulVal = VT_BSTR;

VT_BSTR 用于表示一个 unicode 字符串。

VT_LPWSTR 需要以空值结尾,我认为您的字符串不是。

【讨论】:

  • 我通过读取字节而不是字符来解决问题,然后将字节数组转换为字符串。感谢您的帮助和兴趣。
猜你喜欢
  • 2010-10-16
  • 2011-08-13
  • 1970-01-01
  • 2013-11-17
  • 2016-11-27
  • 1970-01-01
  • 2013-03-14
相关资源
最近更新 更多