Example: Using Data Queues APIs

The following example illustrates using IBM® i data queues APIs.

// Sample Data Queues application
 
#ifdef UNICODE
   #define _UNICODE
   #define CWB_UNICODE
#endif
#include <windows.h>
 
// Include the necessary DQ Classes
#include <stdlib.h>
#include <iostream>
#include "cwbdq.h"
 
using namespace std;
/**********************************************************************/
 
void main()
{
 
   cwbDQ_Attr queueAttributes;
   cwbDQ_QueueHandle queueHandle;
   cwbDQ_Data queueData;
 
   // Create an attribute object
   if ( (queueAttributes = cwbDQ_CreateAttr()) == 0 )
      return;
 
   // Set the maximum record length to 100
   if ( cwbDQ_SetMaxRecLen(queueAttributes,
                           100) != 0 )
      return;
 
   // Set the order to First-In-First-Out
   if (cwbDQ_SetOrder(queueAttributes, CWBDQ_SEQ_FIFO) != 0 )
      return;
 
   // obtain a handle to the system
   cwbCO_SysHandle system = NULL;
   if(cwbCO_CreateSystem( TEXT("SYSNAMEXXX"),&system) != 0) 
	   return;  
 
   // Create the data queue DTAQ in library QGPL on system SYS1
   if ( cwbDQ_CreateEx(system,
						TEXT("DTAQX"),
                        TEXT("QGPL"),
                        queueAttributes,
                        NULL) != 0 )
      return;
 
   // Delete the attributes
   if ( cwbDQ_DeleteAttr( queueAttributes ) != 0 )
      return;
 
   // Open the data queue
   if ( cwbDQ_OpenEx(system, 
	                   TEXT("DTAQ"),
                     TEXT("QGPL"),
                     &queueHandle,
                     NULL) != 0 )
 
       return;
 
   // Create a data object
   if ( (queueData = cwbDQ_CreateData()) == 0 )
      return;
 
   // Set the data length and the data
   if ( cwbDQ_SetData(queueData, (unsigned char*)"Test Data!", 10) != 0 )
      return;
 
   // Write the data to the data queue
   if ( cwbDQ_Write(queueHandle, queueData, CWB_TRUE, NULL) != 0 )
      return;
 
   // Delete the data object
   if ( cwbDQ_DeleteData(queueData) != 0 )
      return;
 
   // Close the data queue
   if ( cwbDQ_Close(queueHandle) != 0 )
      return;
 
}