2011-04-27 5 views
0

C++의 MSMQ 대기열에서 메시지를 읽으려고합니다. 대기열이 성공적으로 열려 있지만 그곳에서 메시지를 읽는 데 문제가 있습니다.MSMQ C++ 메시지 수신 문제가 발생했습니다.

이 코드 샘플은 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; 
} 

지시 "MQReceiveMessage"내가 얻을 "MQ_ERROR_ILLEGAL_PROPERTY_VALUE"오류 코드 : 다음은 코드입니다.

누구든지 내 코드를 살펴보고 무엇이 잘못되었는지, 어떤 속성이 불법인지 말해 줄 수 있습니까?

감사합니다,

답변

1

오류 코드는 사용자가 요청한 속성이 유효 함을 나타냅니다. 내 생각 엔 aMsgPropVar[cPropId].vt = VT_NULL; 설정이 본문 크기 및 본문 유형에 맞지 않습니다. MDSN 문서는 VT_UI4 유형이 모두 있음을 나타냅니다. 예를 들어 here을 참조하십시오.

+0

효과가있었습니다. 나는 [this] (http://msdn.microsoft.com/en-us/library/ms706009%28v=VS.85%29.aspx) 예제를 따라 문제를 겪었다. 감사합니다. –

+0

다시 만나서 반갑습니다. –

관련 문제