Microsoft KB Archive/174261

= MqPers.exe Sending Persistent COM Objects Using MSMQ in C++/ATL =

Article ID: 174261

Article Last Modified on 8/21/2007

-

APPLIES TO


 * Microsoft Message Queue Server 1.0

-



This article was previously published under Q174261



SUMMARY
With Microsoft Message Queuing Server it's possible to send the state of COM objects supporting the IPersistStream or IPersistStorage interface. This article discusses what is necessary to take advantage of this feature using Visual C++ and ATL.



MORE INFORMATION
The following files are available for download from the Microsoft Download Center:

MQPers.exe

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

Designing the Object Whose State Will Be Sent Over a Queue
The sample has two ActiveX components, Point and Line, which are implemented using ATL and Visual C++. In addition to the code generated by the ATL wizard, some additional lines have to be added for IPersistStream implementation. An object that implements IPersistStream is ready for sending its state over a MSMQ's queue.

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

194179 SAMPLE: AtlEvnt.exe Creates ATL Sinks Using IDispEventImpl

Sending the Objects State Over the Queue
We use the #import directive in Visual C++ to manipulate the MSQM COM and the Point and Line objects. For additional information on how to initialize OLE when using the #import directive, please see the following article in the Microsoft Knowledge Base:

169496 INFO: Using ActiveX Data Objects (ADO) via #import in VC++"

The following code shows the declaration for the various ActiveX components used. The data types are defined in the compiler generated files Mqoa.tlh, Mqoa.tli, GraphObj.tlh, and GraphObj.tli: // For these ActiveX components we need only smart interface pointer IMSMQQueueInfosPtr  pQueueInfos; IMSMQQueueInfoPtr     pQueueInfo; IMSMQQueuePtr     pQueue; IUnknownPtr     pIUnknown;

// Instantiate the following ActiveX components IMSMQQueryPtr     pQuery(__uuidof(MSMQQuery)); IMSMQMessagePtr     pMessage(__uuidof(MSMQMessage)); ILinePtr        pLine(__uuidof(Line)); IPointPtr        pPoint(__uuidof(Point)); Sending object state requires a pointer to the IUnknown interface. The MSMQMessage component uses this interface to ask the object for the IPersistStream interface. The object's IUnknown interface pointer must be stored in a VARIANT and could then be assigned to the body property of the MSMQMessage object: ...

if (n == 1) { // Initialize point object pPoint->x = 8; pPoint->y = 9;

// The message body gets the IUnknown pointer pMessage->Body = static_cast(pPoint);

} else { ...  }   pMessage->Send(pQueue); ...

Receiving the Objects State from the Queue
When an MSMQMessage instructs an ActiveX component to save its state to a stream, it also asks the component to save its CLSID to the stream. When receiving an object state, this enables the Microsoft Message Queue Server to instantiate the ActiveX component belonging to the transmitted CLSID.

The message body property of the received message actually is a VARIANT containing an IUnknown pointer to the interface of newly created object. Now it's up to the program to determine which object state was received and which object was created. One way to do this is to ask the IUnknown for ILine or the IPoint interface. ...

// Get the next message pMessage = pQueue->Receive;

IPointPtr     pPoint((IUnknown*)pMessage->Body);

// Check if it's a Point object if (pPoint != NULL) { cout << "\nGot a Point Object: " << pPoint->x << " " << \ pPoint->y << "\n";

}  else { ILinePtr  pLine((IUnknown*)pMessage->Body);

// Check if it's a Line object if (pLine != NULL) { cout << "\nGot a Line Object: " << pLine->x1 << " " << \       pLine->y1 << " " << pLine->x2 << " " << pLine->y2 << "\n"; }     else cout << "\nUnknown Object !!!\n"; }  pQueue->Close; ... The complete sample source is provided. To compile the project you need Visual C++ 5.0

