HRESULT MSMQReceiveMessage(LPSTR pszQueueName, LPSTR pszComputerName,
LPSTR pszLabel, LPSTR pszMessageBody, size_t *pmsgsize) {
#define NUMBEROFPROPERTIES 5
#define LABELSIZE 250
#define MSGSIZE 2048
DWORD cPropId = 0;
HRESULT hr;
HANDLE hQueue;
WCHAR *wszQueueName;
WCHAR *wszComputerName;
LPSTR p;
MQMSGPROPS msgProps;
MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES];
HRESULT aMsgStatus[NUMBEROFPROPERTIES];
WCHAR wszLabelBuffer[LABELSIZE];
WCHAR wszMessageBody[MSGSIZE];
WCHAR * wszFormatName;
wszQueueName = Ansi2Unicode(pszQueueName, NULL);
wszComputerName = Ansi2Unicode(pszComputerName, NULL);
aMsgPropId[cPropId] = PROPID_M_LABEL_LEN;
aMsgPropVar[cPropId].vt = VT_UI4;
aMsgPropVar[cPropId].ulVal = LABELSIZE;
cPropId++;
aMsgPropId[cPropId] = PROPID_M_LABEL;
aMsgPropVar[cPropId].vt = VT_LPWSTR;
aMsgPropVar[cPropId].pwszVal = wszLabelBuffer;
cPropId++;
aMsgPropId[cPropId] = PROPID_M_BODY_SIZE;
aMsgPropVar[cPropId].vt = VT_UI4;
cPropId++;
aMsgPropId[cPropId] = PROPID_M_BODY_TYPE;
aMsgPropVar[cPropId].vt = VT_UI4;
cPropId++;
aMsgPropId[cPropId] = PROPID_M_BODY;
aMsgPropVar[cPropId].vt = VT_VECTOR | VT_UI1;
aMsgPropVar[cPropId].caub.pElems = pszMessageBody;
aMsgPropVar[cPropId].caub.cElems = *pmsgsize;
cPropId++;
msgProps.cProp = cPropId;
msgProps.aPropID = aMsgPropId;
msgProps.aPropVar = aMsgPropVar;
msgProps.aStatus = aMsgStatus;
wszFormatName = (WCHAR *)malloc((wcslen(wszQueueName) + wcslen(wszComputerName) + 12) * 2);
swprintf(wszFormatName,
L"DIRECT=OS:%s\\%s",
wszComputerName,
wszQueueName
);
hr = MQOpenQueue(wszFormatName,
MQ_RECEIVE_ACCESS,
MQ_DENY_NONE,
&hQueue
);
free(wszFormatName);
if (FAILED(hr))
{
printf("MQOpenQueue error.\n");
*pmsgsize = 0;
return hr;
}
hr = MQReceiveMessage(hQueue,
1000,
MQ_ACTION_RECEIVE,
&msgProps,
NULL,
NULL,
NULL,
MQ_NO_TRANSACTION
);
if (FAILED(hr)) {
wprintf(L"No messages. Closing queue\n");
*pmsgsize = 0;
}
else {
*pmsgsize = msgProps.aPropVar[2].ulVal;
if(msgProps.aPropVar[2].ulVal > 0) {
;
}
}
GlobalFree(wszQueueName);
GlobalFree(wszComputerName);
hr = MQCloseQueue(hQueue);
if (FAILED(hr))
{
return hr;
}
return hr;
} |