MSMQ c++;接收消息问题 我试图从C++中的MSMQ队列中读取消息。 队列已成功打开,但从中读取任何消息时遇到问题

MSMQ c++;接收消息问题 我试图从C++中的MSMQ队列中读取消息。 队列已成功打开,但从中读取任何消息时遇到问题,c++,msmq,C++,Msmq,此代码示例取自MSDN,并在一些地方进行了修改。 代码如下: // Define the required constants and variables. const int NUMBEROFPROPERTIES = 5; DWORD cPropId = 0; HRESULT hr = MQ_OK; // Return code ULONG ulBufferSize = 256; // Define an MQMSGPROPS

此代码示例取自MSDN,并在一些地方进行了修改。 代码如下:

// Define the required constants and variables.
const int NUMBEROFPROPERTIES = 5;
DWORD cPropId = 0;
HRESULT hr = MQ_OK;                                 // Return code
ULONG ulBufferSize = 256;

// Define an MQMSGPROPS structure.
MQMSGPROPS msgprops;
MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
HRESULT aMsgStatus[NUMBEROFPROPERTIES];

// Specify the message properties to be retrieved.
aMsgPropId[cPropId] = PROPID_M_LABEL_LEN;           // Property ID
aMsgPropVar[cPropId].vt = VT_UI4;                   // Type indicator
aMsgPropVar[cPropId].ulVal = MQ_MAX_MSG_LABEL_LEN;  // Length of label
cPropId++;

WCHAR wszLabelBuffer[MQ_MAX_MSG_LABEL_LEN];         // Label buffer
aMsgPropId[cPropId] = PROPID_M_LABEL;               // Property ID
aMsgPropVar[cPropId].vt = VT_LPWSTR;                // Type indicator
aMsgPropVar[cPropId].pwszVal = wszLabelBuffer;      // Label buffer
cPropId++;

UCHAR * pucBodyBuffer = NULL;
pucBodyBuffer = (UCHAR*)malloc(ulBufferSize); 
if (pucBodyBuffer == NULL)
{
    return MQ_ERROR_INSUFFICIENT_RESOURCES;
}
memset(pucBodyBuffer, 0, ulBufferSize);
aMsgPropId[cPropId] = PROPID_M_BODY_SIZE;           // Property ID
aMsgPropVar[cPropId].vt = VT_NULL;                  // Type indicator
cPropId++;

aMsgPropId[cPropId] = PROPID_M_BODY;                // Property ID
aMsgPropVar[cPropId].vt = VT_VECTOR | VT_UI1;       // Type indicator
aMsgPropVar[cPropId].caub.pElems = (UCHAR*)pucBodyBuffer;  // Body buffer
aMsgPropVar[cPropId].caub.cElems = ulBufferSize;    // Buffer size
cPropId++;

aMsgPropId[cPropId] = PROPID_M_BODY_TYPE;           // Property ID
aMsgPropVar[cPropId].vt = VT_NULL;                  // Type indicator
cPropId++;


// Initialize the MQMSGPROPS structure.
msgprops.cProp = cPropId;                           // Number of message properties
msgprops.aPropID = aMsgPropId;                      // IDs of the message properties
msgprops.aPropVar = aMsgPropVar;                    // Values of the message properties
msgprops.aStatus = aMsgStatus;                      // Error reports

    // HERE IS THE ERROR
hr = MQReceiveMessage(
                      this->readHandle,                     // Queue handle
                        // Max time to (msec) to receive the message
                        // wait soooooo much 
                      INFINITE,                       
                      MQ_ACTION_RECEIVE          // Receive action
                      &msgprops,                  // Message property structure
                      NULL,                       // No OVERLAPPED structure
                      NULL,                       // No callback function
                      NULL,                       // No cursor handle
                      MQ_NO_TRANSACTION           // Not in a transaction
                      );

// log reading operation result
f<<"log receive operation"<<endl;
f<<hex<<hr<<endl;
f.close();

if (hr == MQ_ERROR_BUFFER_OVERFLOW)
{
    //MessageBox(NULL, TEXT("buffer overflow"), TEXT("Message"), MB_OK);

    ulBufferSize = aMsgPropVar[2].ulVal*sizeof(UCHAR);
    pucBodyBuffer = (UCHAR*)realloc(pucBodyBuffer, ulBufferSize);
    if (pucBodyBuffer == NULL)
    {
    return MQ_ERROR_INSUFFICIENT_RESOURCES;
    }
    memset(pucBodyBuffer, 0, ulBufferSize);
    aMsgPropVar[3].caub.pElems = (UCHAR*)pucBodyBuffer;
    aMsgPropVar[3].caub.cElems = ulBufferSize;
}
//定义所需的常量和变量。
常数int NUMBEROFPROPERTIES=5;
DWORD cPropId=0;
HRESULT hr=MQ_正常;//返回码
ULONG ulBufferSize=256;
//定义MQMSGPROPS结构。
MQMSGPROPS msgprops;
MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
HRESULT aMsgStatus[NUMBEROFPROPERTIES];
//指定要检索的消息属性。
aMsgPropId[cPropId]=PROPID\u M\u标签\u LEN;//属性ID
aMsgPropVar[cPropId].vt=vt_UI4;//类型指示器
aMsgPropVar[cPropId].ulVal=MQ\u MAX\u MSG\u LABEL\u LEN;//标签长度
cPropId++;
WCHAR wszLabelBuffer[MQ_MAX_MSG_LABEL_LEN];//标签缓冲区
aMsgPropId[cPropId]=PROPID\u M\u标签;//属性ID
aMsgPropVar[cPropId].vt=vt_LPWSTR;//类型指示器
aMsgPropVar[cPropId].pwszVal=wszLabelBuffer;//标签缓冲区
cPropId++;
UCHAR*pucBodyBuffer=NULL;
pucBodyBuffer=(UCHAR*)malloc(ulBufferSize);
if(pucBodyBuffer==NULL)
{
返回MQ\u错误\u资源不足;
}
memset(pucBodyBuffer,0,ulBufferSize);
aMsgPropId[cPropId]=PROPID\u M\u BODY\u SIZE;//属性ID
aMsgPropVar[cPropId].vt=vt_NULL;//类型指示器
cPropId++;
aMsgPropId[cPropId]=PROPID\u M\u BODY;//属性ID
aMsgPropVar[cPropId].vt=vt_向量| vt_UI1;//类型指示器
aMsgPropVar[cPropId].caub.pElems=(UCHAR*)pucBodyBuffer;//身体缓冲器
aMsgPropVar[cPropId].caub.cElems=ulBufferSize;//缓冲区大小
cPropId++;
aMsgPropId[cPropId]=PROPID\u M\u BODY\u TYPE;//属性ID
aMsgPropVar[cPropId].vt=vt_NULL;//类型指示器
cPropId++;
//初始化MQMSGPROPS结构。
msgprops.cProp=cPropId;//消息属性数
msgprops.aPropID=aMsgPropId;//消息属性的ID
msgprops.aPropVar=aMsgPropVar;//消息属性的值
msgprops.aStatus=aMsgStatus;//错误报告
//这里是错误
hr=MQReceiveMessage(
this->readHandle,//队列句柄
//接收消息的最长时间(毫秒)
//等等
无限,
MQ_ACTION_RECEIVE//RECEIVE ACTION
&msgprops,//消息属性结构
NULL,//无重叠结构
NULL,//没有回调函数
NULL,//没有光标句柄
MQ_NO_事务//不在事务中
);
//日志读取操作结果

错误代码表明您请求的属性无效。我猜设置
aMsgPropVar[cPropId].vt=vt\u NULL对于车身尺寸和车身类型是错误的-MDSN文档表明两者都有类型VT_UI4-例如,请参见。

它起作用了。我在下面的例子中遇到了麻烦。谢谢你,很高兴你又来了