This was taken from MSDN online(msdn.microsoft.com) <br><br><FONT FACE=monospace><br>Callback Function Example<br>This example uses a callback function (fnReceiveCallback is not NULL) to read the first message in a queue. In this case, the message is read by MQReceiveMessage and the returned message properties (and error codes) are handled by the registered callback function.<br><br>Note Once registered, a callback function cannot be unregistered. Closing the queue, closes the callback function as well.<br><br>When using a callback function, it is very important to allocate the memory for the MQMSGPROPS structure on the heap. Not on the stack. In addition, always free MQMSGPROPS within the callback function<br><br>When multiple asynchronous MQReceiveMessage calls are outstanding, several callbacks are registered; upon arrival of a message, the first registered callback is called. Internally, MSMQ uses the WaitForMultipleObjects API which is limited to 64 objects per process.<br><br>To read a single message using a callback function <br><br>Write the callback function. <br>Call MQOpenQueue to open the queue with receive or peek access. If the queue is opened with receive access, the application can still peek at the messages in the queue. <br>Specify the message properties to be retrieved, retrieving only those properties that are needed. <br>Initialize the MQMSGPROPS structure on the heap. <br>Call MQReceiveMessage to read the message from the queue, making sure to free the MQMSGPROPS structure from within the callback function. <br>Call MQCloseQueue to close the queue, freeing all memory allocated for any pending MQReceiveMessage calls. Closing the queue, closes the callback function as well. <br>Code Example<br>The following example reads the first message of the queue using a callback function.<br><br>//////////////////////////////<br>// Receive callback function.<br>/////////////////////////////<br> <br>void APIENTRY ReceiveCallbackRoutine(<br> HRESULT hr,<br> QUEUEHANDLE hSource,<br> DWORD dwTimeout,<br> DWORD dwAction,<br> MQMSGPROPS* pMessageProps,<br> LPOVERLAPPED lpOverlapped,<br> HANDLE hCursor<br>  

<br> {<br> if (FAILED(hr))<br> {<br> // Error handler for Callback routine.<br> }<br> else<br> {<br> // Process message.<br> }<br> }<br><br>//////////////<br>// Open Queue<br>//////////////<br> <br>HRESULT hr;<br>QUEUEHANDLE hQueue;<br> <br>hr = MQOpenQueue(<br> szwFormatNameBuffer, // Format Name of the queue to be opened<br> MQ_RECEIVE_ACCESS, // Access rights to the Queue<br> 0, // No receive Exclusive<br> &hQueue // OUT: handle to the opened Queue<br>);<br> <br>if (FAILED(hr))<br>{<br> // Error handler for MQOpenQueue.<br>}<br> <br> <br>MQMSGPROPS * pmsgprops;<br>MQPROPVARIANT *paVariant;<br>MSGPROPID * paPropId;<br>DWORD dwcPropId = 0;<br> <br>//<br>// The output parameters to an asynchronous call to MQReceiveMessage <br>// should be kept intact until the operation completes, you should <br>// not free or reuse them until the operation is complete.<br>//<br>pmsgprops = new MQMSGPROPS;<br>paVariant = new MQPROPVARIANT[ 10];<br>paPropId = new MSGPROPID[ 10];<br> <br> <br>//////////////////////////////////////////////////<br>// Prepare the message properties to be retrieved.<br>/////////////////////////////////////////////////<br> <br> <br>// Set the PROPID_M_BODY property.<br>paPropId[dwcPropId] = PROPID_M_BODY; //PropId<br>paVariant[dwcPropId].vt = VT_VECTOR¦VT_UI1; //Type<br>paVariant[dwcPropId].caub.cElems = MSG_BODY_LEN ; //Value<br>paVariant[dwcPropId].caub.pElems = new unsigned char[ MSG_BODY_LEN];<br> <br> dwcPropId++;<br> <br>////////////////////////////////<br>// Initialize the MQMSGPROPS structure<br>///////////////////////////////<br>pmsgprops->cProp = dwcPropId; //Number of properties<br>pmsgprops->aPropID = paPropId; //Ids of properties<br>pmsgprops->aPropVar = paVariant; //Values of properties<br>pmsgProps->aStatus = NULL; //No Error report<br> <br> <br>///////////////////////////////////////////////<br>// Receive the message using callback function<br>// ReceiveCallbackRoutine.<br>///////////////////////////////////////////////<br> <br>hr = MQReceiveMessage(<br> hQueue, // Handle to the Queue<br> 5 * 60 * 1000, // Max time (msec) to wait<br> MQ_ACTION_RECEIVE, // Action<br> pmsgprops, // Properties to retrieve<br> NULL, // No OVERLAPPED structure<br> ReceiveCallbackRoutine, // Callback function<br> NULL, // No Cursor<br> NULL // No transaction<br>  

;<br> <br>if (FAILED(hr))<br> {<br> // Error handler for MQReceiveMessage.<br> }<br><br>Built on Wednesday, January 05, 2000<br></font><br> <p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href=
</a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>