This section covers the high level coding required for the "HelloWorld" application.
The following code is in the example HelloWorld_Runtime.c in its complete state. The example contains code to handle the specifics of running a program on a PocketPC, which mainly involves writing to a file to cope with the lack of command line options. Use the display function to write to a file, as shown in the examples contained in the following sections.
You need to include just one header file to access the APIs.
#define NATIVE #define MQE_PLATFORM = PLATFORM_WINCE #include<published/MQe_API.h>
All of the code, including variable declarations, is inside the main method. You require structures for error checking. The MQeExceptBlock structure is passed into all functions to get the error information back. In addition, all functions return a code indicating success or failure, which is cached in a local variable:
/* ... Local return flag */ MQERETURN rc; MQeExceptBlock exceptBlock;
You must create a number of strings, for example for the queue manager name:
MQeStringHndl hLocalQMName; ... if ( MQERETURN_OK == rc ) { rc = mqeString_newUtf8(&exceptBlock, &hLocalQMName, "LocalQM"); }
The first API call made is session initialize:
/* ... Initalize the session */ rc = mqeSession_initialize(&exceptBlock);
This process involves two steps:
Creating the queue manager requires two sets of parameters, one set for the queue manager and one for the registry. Both sets of parameters are initialized. The queue store and the registry require directories.
if (MQERETURN_OK == rc) { MQeQueueManagerParms qmParams = QMGR_INIT_VAL; MQeRegistryParms regParams = REGISTRY_INIT_VAL; qmParams.hQueueStore = hQueueStore; qmParams.opFlags = QMGR_Q_STORE_OP; /* ... create the registry parameters - minimum that are required */ regParams.hBaseLocationName = hRegistryDir; display("Loading Queue Manager from registry \n"); rc = mqeQueueManager_new( &exceptBlock, &hQueueManager, hLocalQMName, &qmParams, ®Params); }
You can now start the queue manager and carry out messaging operations:
/* Start the queue manager */ if ( MQERETURN_OK == rc ) { display("Starting the Queue Manager\n"); rc = mqeQueueManager_start(hQueueManager, &exceptBlock); }
To create a message, firstly create a new fields object. The following example adds a single field. Note that the field label strings are passed in:
MQeFieldsHndl hMsg; display("Creating a new message\n"); rc = mqeFields_new(&exceptBlock,&hMsg); if ( MQERETURN_OK == rc ) { rc = mqeFields_putInt32(hMsg,&exceptBlk, hFieldLabel,42); }
Once you have created the message, you can put it to a local queue using the putMessage function. Note that the queue and queue manager names are passed in. NULL and 0 are passed in for the security and assured delivery parameters, as they are not required in this example. Once the message has been put, you can free the MQeFields object:
if ( MQERETURN_OK == rc ) { display("Putting a message \n"); rc = mqeQueueManager_putMessage(hQueueManager, &exceptBlock, hLocalQMName, hLocalQueueName, hMsg, NULL, 0); (void) mqeFields_free(hMsg,NULL); }
Once the message has been put to a queue, you can retrieve and check it. Similar options are passed to the getMessage function. The difference is that a pointer to a fields handle is passed in. A new Fields object is created, removing the message from the queue:
MQeFieldsHndl hReturnedMessage; display("Getting the message back \n"); rc = mqeQueueManager_getMessage(hQueueManager, &exceptBlock, &hReturnedMessage, hLocalQMName, hLocalQueueName, NULL, NULL, 0); }
Once the message has been obtained, you can check it for the value that was entered. Obtain this by using the getInt32 function. If the result is valid, you can print it out:
if (MQERETURN_OK == rc) { MQEINT32 answer; rc = mqeFields_getInt32(hReturnedMessage, &exceptBlock, &answer, hFieldLabel); if (MQERETURN_OK == rc) { display("Answer is %d\n",answer); } else { display( "\n\n %s (0x%X) %s (0x%X)\n", mapReturnCodeName(EC(&exceptBlock)) , EC(&exceptBlock), mapReasonCodeName(ERC(&exceptBlock)), ERC(&exceptBlock) ); } }
Following the removal of the message from the queue, you can stop and free the queue manager. You can also free the strings that were created. Finally, terminate the session:
(void)mqeQueueManager_stop(hQueueManager,&exceptBlock); (void)mqeQueueManager_free(hQueueManager,&exceptBlock); /* Lets do some clean up */ (void)mqeString_free(hFieldLabel,&exceptBlock); (void)mqeString_free(hLocalQMName,&exceptBlock); (void)mqeString_free(hLocalQueueName,&exceptBlock); (void)mqeString_free(hQueueStore,&exceptBlock); (void)mqeString_free(hRegistryDir,&exceptBlock); (void)mqeSession_terminate(&exceptBlock);
To simplify the process of compiling, the examples directory includes a makefile. This is the makefile exported from eMbedded Visual C (EVC). A batchfile runs this makefile. This batch file will setup the paths to the EVC directories, along with the paths to the WebSphere MQ Everyplace installation. You may need to edit the batch file, depending on how you want to install WebSphere MQ Everyplace.
Running the batch file will compile the example. By default, the batch file compiles for Debug PocketPC 2000 (either Emulator or ARM processor).