开发者

MSMQ c++ to c# problem

I want to write messages to a MSMQ queue using C++ and read them using C#. I send the messages as strings. It works fine if I both write and read the messages in C++ but if I try to read the message using C#, I get only the first character of the message. (eg: I send "abcd" and I receive only "a").

I also must mention that I am using this code in Windows Mobile.

Here is the c++ code:

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
                 );

Here is the c# code:

 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)
        {
            MessageB开发者_如何转开发ox.Show(exc.ToString(), "Exception");
            return null;
        }
    }

I am using BodyStream instead of Body for the message because I don't want to use any message formatter.

Thanks


I solved myself the problem. I shall post here the code maybe there is someone else interested.

        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;
        }


Maybe you can try:

aMsgPropVar[cPropId].ulVal = VT_BSTR;

VT_BSTR is used to indicate a unicode string.

VT_LPWSTR needs to be null-terminated, which I don't think your string is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜