MSMQ c ++ to c # проблема - PullRequest
       7

MSMQ c ++ to c # проблема

1 голос
/ 28 апреля 2011

Я хочу записывать сообщения в очередь MSMQ, используя C ++, и читать их, используя 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 ]

3 голосов
/ 29 апреля 2011

Я решил сам проблему.Я выложу здесь код, может быть, есть кто-то еще заинтересованный.

        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;
        }
0 голосов
/ 28 апреля 2011

Может быть, вы можете попробовать:

aMsgPropVar[cPropId].ulVal = VT_BSTR;

VT_BSTR используется для обозначения строки в юникоде.

VT_LPWSTR должен заканчиваться нулем, что, по-моему, не является вашей строкой.

...