vc队列:vc操作微软消息队列的方法

定义

IMSMQQueuePtr qDest; // Represents Destination Queue
IMSMQQueueInfoPtr qInfo; //Represents an open instance of the destination queue
IMSMQQueueInfoPtr qRead;
IMSMQMessagePtr qMsg; //Represents the message



初始化

qDest = NULL;
CoInitialize(NULL);
try{
qInfo.CreateInstance("MSMQ.MSMQQueueInfo");
qRead.CreateInstance("MSMQ.MSMQQueueInfo");
qMsg.CreateInstance("MSMQ.MSMQMessage");

lpszSystemInfo = tchBuffer;
GetComputerName(lpszSystemInfo,&cchBuff); // Getting the ComputerName
m_ServerName=lpszSystemInfo;
UpdateData(FALSE);
}
catch (_com_error)
{
MessageBox("Failed in CreateInstance of MSMQQueueInfo","MSMQ QueueInfo",1);
PostQuitMessage(1);
ReleaseAll();
}



创建消息队列

try
{
//
// m_ServerName = .\Private$\aaa 创建专有队列
//
//
//
// m_ServerName = .\aaa 公用队列
//
// qInfo->PathName = m_ServerName.AllocSysString(); // Setting the Queue Path
CString fname;
fname="DIRECT=OS:";
fname+=m_ServerName;
qInfo->PathName = m_ServerName.AllocSysString();
qInfo->FormatName = fname.AllocSysString();
qInfo->Label= m_msgLabel.AllocSysString(); // Setting the Queue Label
ht = qInfo->Create();

qInfo->Journal=1; // Enabling the Journal Option
qInfo->Update();
qInfo->Refresh();
}
catch (_com_error e)
{
CString str;
str.Format("%s",e.ErrorMessage());

MessageBox("Creating a Queue Failed","Create Queue",1);
//ReleaseAll();
}



给消息队列发送数据

try
{
// Create a direct format name of the queue, and
// set the FormatName property of the MSMQQueueInfo object.

// qInfo->FormatName L"DIRECT=TCP:10.9.255.71\\Private$\\test" 私有
// qInfo->FormatName L"DIRECT=TCP:10.9.255.71\\test" 公共

fname="DIRECT=OS:";
fname+=m_ServerName;
qInfo->PathName=m_ServerName.AllocSysString();
qInfo->FormatName = fname.AllocSysString();

// Open the queue.

qDest = qInfo->Open(MQ_SEND_ACCESS, MQ_DENY_NONE);
qMsg->Label = m_msgLabel.AllocSysString();

VariantInit(&vMessage);
vMessage.vt=VT_BSTR;
vMessage.bstrVal=m_msgData.AllocSysString();
qMsg->put_Body(vMessage);
VariantClear(&vMessage);
qMsg->Send(qDest);
qDest->Close();

}
catch(_com_error)
{
MessageBox("Sending the Queue failed","Send Queue",1);
//ReleaseAll();
}



读取消息队列内容

try
{
qRead->PathName=m_ServerName.AllocSysString();
fname="DIRECT=OS:";
fname+=m_ServerName;

if(status==1) fname=fname+";Journal"; // If Journal Option is Checked

qInfo->PathName=m_ServerName.AllocSysString();
qRead->FormatName = fname.AllocSysString();
qDest = qRead->Open(MQ_RECEIVE_ACCESS,MQ_DENY_NONE);
qMsg = qDest->Receive(&vtMissing,&vtMissing,&vtMissing,&vtReceiveTimeout);

if (qMsg == NULL)
{
return;
}
label=qMsg->GetLabel();
body=qMsg->GetBody().bstrVal;
m_msgLabel=(LPSTR)label;
m_msgData=(LPSTR)body;
UpdateData(FALSE);
qDest->Close();
}
catch(_com_error)
{
MessageBox("Reading the Queue Failed","Read Queue",1);
//ReleaseAll();
}
Tags:  消息队列 微软vc vc队列类 vc队列

延伸阅读

最新评论

发表评论