MSMQ c ++到c#问题

时间:2011-04-28 09:11:26

标签: c# c++ msmq

我想使用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作为消息,因为我不想使用任何消息格式化程序。

由于

2 个答案:

答案 0 :(得分:3)

我解决了自己的问题。我将在这里发布代码,也许还有其他人感兴趣。

        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 :(得分:0)

也许你可以试试:

aMsgPropVar[cPropId].ulVal = VT_BSTR;

VT_BSTR用于表示unicode字符串。

VT_LPWSTR需要以null结尾,我认为你的字符串不是。