VC++ MSMQ微软消息队列简单使用举例Word文件下载.docx
- 文档编号:20852899
- 上传时间:2023-01-26
- 格式:DOCX
- 页数:19
- 大小:19.55KB
VC++ MSMQ微软消息队列简单使用举例Word文件下载.docx
《VC++ MSMQ微软消息队列简单使用举例Word文件下载.docx》由会员分享,可在线阅读,更多相关《VC++ MSMQ微软消息队列简单使用举例Word文件下载.docx(19页珍藏版)》请在冰豆网上搜索。
PSECURITY_DESCRIPTORpSecurityDescriptor,
LPWSTRwszOutFormatName,
DWORD*pdwOutFormatNameLength)
{
//定义队列最大属性.
constintNUMBEROFPROPERTIES=2;
//定义属性
MQQUEUEPROPSQueueProps;
MQPROPVARIANTaQueuePropVar[NUMBEROFPROPERTIES];
QUEUEPROPIDaQueuePropId[NUMBEROFPROPERTIES];
HRESULTaQueueStatus[NUMBEROFPROPERTIES];
HRESULThr=MQ_OK;
//Validatetheinputparameters.
if(wszPathName==NULL||wszOutFormatName==NULL||pdwOutFormatNameLength==NULL)
{
returnMQ_ERROR_INVALID_PARAMETER;
}
//设置属性
DWORDcPropId=0;
aQueuePropId[cPropId]=PROPID_Q_PATHNAME;
aQueuePropVar[cPropId].vt=VT_LPWSTR;
aQueuePropVar[cPropId].pwszVal=wszPathName;
cPropId++;
WCHARwszLabel[MQ_MAX_Q_LABEL_LEN]=L"
HaveaTestQueue"
;
aQueuePropId[cPropId]=PROPID_Q_LABEL;
aQueuePropVar[cPropId].pwszVal=wszLabel;
//初始化MQQUEUEPROPS结构体.
QueueProps.cProp=cPropId;
//Numberofproperties
QueueProps.aPropID=aQueuePropId;
//IDsofthequeueproperties
QueueProps.aPropVar=aQueuePropVar;
//Valuesofthequeueproperties
QueueProps.aStatus=aQueueStatus;
//Pointertothereturnstatus
//调用MQCreateQueue创建队列
WCHARwszFormatNameBuffer[256];
DWORDdwFormatNameBufferLength=sizeof(wszFormatNameBuffer)/sizeof(wszFormatNameBuffer[0]);
hr=MQCreateQueue(pSecurityDescriptor,//Securitydescriptor
&
QueueProps,//Addressofqueuepropertystructure
wszFormatNameBuffer,//Pointertoformatnamebuffer
dwFormatNameBufferLength);
//Pointertoreceivethequeue'
sformatnamelength
if(FAILED(hr))
//fprintf(stderr,"
FailedMQCreateQueue,error=0x%x\n"
hr);
wprintf(L"
MQCreateQueueerrorret=0x%x.\n"
hr);
//Returntheformatnameifthequeueiscreatedsuccessfully.
if(hr==MQ_OK||hr==MQ_INFORMATION_PROPERTY)
wprintf(L"
Thequeuewascreated.\n"
);
if(*pdwOutFormatNameLength>
=dwFormatNameBufferLength)
{
wcsncpy(wszOutFormatName,wszFormatNameBuffer,*pdwOutFormatNameLength-1);
wszOutFormatName[*pdwOutFormatNameLength-1]=L'
\0'
*pdwOutFormatNameLength=dwFormatNameBufferLength;
}
else
wprintf(L"
Butitsformatnamecannotbereturned.\n"
returnhr;
}
//打开队列
intOpenMyQueue(
LPWSTRwszPathName,
DWORDdwAccess,
DWORDdwShareMode,
QUEUEHANDLE*phQueue
)
if((wszPathName==NULL)||(phQueue==NULL))
//CallMQPathNameToFormatNametoobtaintheformatnamerequired
//toopenthequeue.
DWORDdwFormatNameBufferLength=256;
//Lengthoftheformatnamebuffer
//Formatnamebuffer
hr=MQPathNameToFormatName(wszPathName,
wszFormatNameBuffer,
if(FAILED(hr))
fprintf(stderr,"
AnerroroccurredinMQPathNameToFormatName(error:
0x%x).\n"
returnhr;
//CallMQOpenQueuetoopenthequeuewiththeaccessand
//sharemodeprovidedbythecaller.
hr=MQOpenQueue(
wszFormatNameBuffer,//Formatnameofthequeue
dwAccess,//Accessmode
dwShareMode,//Sharemode
phQueue//OUT:
Queuehandle
);
AnerroroccurredinMQOpenQueue(error:
0x%x.)\n"
hr);
//发消息
HRESULTSendMessage(WCHAR*wszQueueName,WCHAR*wszComputerName)
//Validatetheinputstrings.
if(wszQueueName==NULL||wszComputerName==NULL)
//Definetherequiredconstantsandvariables.
constintNUMBEROFPROPERTIES=5;
//Propertycounter
//Returncode
HANDLEhQueue=NULL;
//Queuehandle
//DefineanMQMSGPROPSstructure.
MQMSGPROPSmsgProps;
MSGPROPIDaMsgPropId[NUMBEROFPROPERTIES];
MQPROPVARIANTaMsgPropVar[NUMBEROFPROPERTIES];
HRESULTaMsgStatus[NUMBEROFPROPERTIES];
//Specifythemessagepropertiestobesent.
aMsgPropId[cPropId]=PROPID_M_LABEL;
//PropertyID
aMsgPropVar[cPropId].vt=VT_LPWSTR;
//Typeindicator
aMsgPropVar[cPropId].pwszVal=L"
The_message_label"
//Themessagelabel
//aMsgPropId[cPropId]=PROPID_M_BODY_TYPE;
//aMsgPropVar[cPropId].vt=VT_LPWSTR;
//aMsgPropVar[cPropId].pwszVal=L"
MassageContent"
//Themessagebody
//cPropId++;
/*************************************MsgBody*****************************************************/
WCHARwszMessageBody[]=L"
TheMessageBodyTest"
aMsgPropId[cPropId]=PROPID_M_BODY;
aMsgPropVar[cPropId].vt=VT_VECTOR|VT_UI1;
aMsgPropVar[cPropId].caub.pElems=(LPBYTE)wszMessageBody;
aMsgPropVar[cPropId].caub.cElems=sizeof(wszMessageBody);
DWORDdwBodyType=VT_BSTR;
aMsgPropId[cPropId]=PROPID_M_BODY_TYPE;
aMsgPropVar[cPropId].vt=VT_UI4;
aMsgPropVar[cPropId].ulVal=dwBodyType;
cPropId++;
//InitializetheMQMSGPROPSstructure.
msgProps.cProp=cPropId;
msgProps.aPropID=aMsgPropId;
msgProps.aPropVar=aMsgPropVar;
msgProps.aStatus=aMsgStatus;
//Createadirectformatnameforthequeue.
WCHAR*wszFormatName=NULL;
DWORDdwBufferLength=0;
dwBufferLength=wcslen(wszQueueName)+wcslen(wszComputerName)+12;
wszFormatName=newWCHAR[dwBufferLength];
if(wszFormatName==NULL)
returnMQ_ERROR_INSUFFICIENT_RESOURCES;
memset(wszFormatName,0,dwBufferLength*sizeof(WCHAR));
if(_snwprintf(
wszFormatName,
dwBufferLength-1,
L"
DIRECT=OS:
%s\\%s"
//原来使用L"
%s//%s"
//L"
%sprivate$\\%s"
wszComputerName,
wszQueueName
)<
0)
Theformatnameistoolongforthebufferspecified.\n"
returnFALSE;
else
wszFormatName[dwBufferLength-1]=L'
//GetFormateName(wszFormatName,dwFormatNameBufferLength,wszFormatNameBuffer);
wprintf(L"
FormateName\t"
wprintf(wszFormatNameBuffer);
\t"
wprintf(wszFormatName);
\n"
//CallMQOpenQueuetoopenthequeuewithsendaccess.
wszFormatName,//原来使用wszFormatName//Formatnameofthequeue
MQ_SEND_ACCESS,//Accessmode
MQ_DENY_NONE,//Sharemode
hQueue//OUT:
//Freethememorythatwasallocatedfortheformatnamestring.
delete[]wszFormatName;
//HandleanyerrorreturnedbyMQOpenQueue.
fprintf(stderr,"
FailedMQSendMessage,MQOpenQueuereterror=0x%x\n"
//CallMQSendMessagetosendthemessagetothequeue.
hr=MQSendMessage(
hQueue,//Queuehandle
msgProps,//Messagepropertystructure
MQ_NO_TRANSACTION//Notinatransaction
MQCloseQueue(hQueue);
FailedMQSendMessage,error=0x%x\n"
//returnhr;
printf("
MQSendMessageret=0x%x\n"
//CallMQCloseQueuetoclosethequeue.
hr=MQCloseQueue(hQueue);
//读消息
HRESULTReadingDestQueue(WCHAR*wszQueueName,WCHAR*wszComputerName)
ULONGulBufferSize=1024;
MQMSGPROPSmsgprops;
//Specifythemessagepropertiestoberetrieved.
aMsgPropId[cPropId]=PROPID_M_LABEL_LEN;
aMsgPropVar[cPropId].ulVal=MQ_MAX_MSG_LABEL_LEN;
//Lengthoflabel
WCHARwszLabelBuffer[MQ_MAX_MSG_LABEL_LEN];
//Labelbuffer
aMsgPropVar[cPropId].pwszVal=wszLabelBuffer;
UCHAR*pucBodyBuffer=NULL;
pucBodyBuffer=(UCHAR*)malloc(ulBufferSize);
if(pucBodyBuffer==NULL)
memset(pucBodyBuffer,0,ulBufferSize);
aMsgPropId[cPropId]=PROPID_M_BODY_SIZE;
aMsgPropVar[cPropId].vt=VT_NULL;
aMsgPropVar[cPropId].caub.pElems=(UCHAR*)pucBodyBuffer;
//Bodybuffer
aMsgPropVar[cPropId].caub.cElems=ulBufferSize;
//Buffersize
msgprops.cProp=cPropId;
//Numberofmessageproperties
msgprops.aPropID=aMsgPropId;
//IDsofthemessageproperties
msgprops.aPropVar=aMsgPropVar;
//Valuesofthemessageproperties
msgprops.aStatus=aMsgStatus;
//Errorreports
returnMQ_E
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VC+ MSMQ微软消息队列简单使用举例 VC MSMQ 微软 消息 队列 简单 使用 举例
![提示](https://static.bdocx.com/images/bang_tan.gif)