Index 0 is special, it is a field with an empty name (fd_namelen is '0') that contains the encoded type name (MQE_TYPE_ASCII) of the MQeFields object. It is provided primarily to support the debugging of communication problems with a peer WebSphere MQ Everyplace system. Programs that are trying to enumerate the fields in an MQeFields object would usually start with index 1. The number of fields returned by MQeFieldsFields includes this special field.
#include <hmq.h> MQEINT32 MQeFieldsGetByIndex( MQEHSESS hSess, MQEHFIELDS hFlds, MQEINT32 startIndex, MQEFIELD pFds[], MQEINT32 nFlds, MQEINT32 * pCompCode, MQEINT32 * pReason);
On output, each descriptor is modified to reflect the field's actual values:
static MQECHAR const * FieldsType = "com.ibm.mqe.MQeFields"; static const MQECHAR * textVal = "The Owl and the Pussy Cat went to sea."; /* template for fields */ static const MQEFIELD PFDS[] = { {MQE_TYPE_BYTE, 0, 7, "fooByte", (MQEBYTE *)0, 0, (MQEBYTE *)0}, {MQE_TYPE_SHORT, 0, 8, "fooShort", (MQEBYTE *)0, 0, (MQEBYTE *)0}, {MQE_TYPE_LONG, 0, 7, "fooLong", (MQEBYTE *)0, 0, (MQEBYTE *)0}, {MQE_TYPE_ASCII, 0, 7, "fooText", (MQEBYTE *)0, 0, (MQEBYTE *)0}, }; #define NFDS 4 MQEHSESS hSess; MQEINT32 compcode; MQEINT32 reason; MQEHFIELDS hFlds; MQEBYTE byteVal; MQEINT16 int16Val; MQEINT32 int32Val; MQEFIELD Fds[NFDS], fd; MQEINT32 rc, nFlds, i; hSess = MQeInitialize("MyAppsName", &compcode, &reason); hFlds = MQeFieldsAlloc( hSess, FieldsType, &compcode, &reason); /* Put some fields in the fields object using MQeFieldsPutByArrayOfFd() */ byteVal = 0xAE; int16Val = 0x9876; int32Val = 0x12345678; /* Copy template */ memcpy(Fds,PFDS,sizeof(Fds)); Fds[0].fd_data = &byteVal; Fds[0].fd_datalen = 1; Fds[1].fd_data = &int16Val; Fds[1].fd_datalen = 2; Fds[2].fd_data = &int32Val; Fds[2].fd_datalen = 4; Fds[3].fd_data = (void *) &textVal[0]; Fds[3].fd_datalen = strlen(textVal); rc = MQeFieldsPutByArrayOfFd( hSess, hFlds, Fds, NFDS, &compcode, &reason); /* Get the fields out by index*/ nFlds = MQeFieldsFields( hSess, hFlds, &compcode, &reason); /* Get the fields one by one (without knowing the field names) */ /* Start at 1 - ignore index 0 (field object identifier) */ for (i=1; i<nFlds; i++) { fd.fd_name = NULL; fd.fd_namelen = 0; fd.fd_datatype = MQE_TYPE_BYTE; fd.fd_data = NULL; fd.fd_datalen = 0; fd.fd_base = 0; /* Use get by index to get datatype, namelen and datalen */ MQeFieldsGetByIndex(hSess, hFlds, i, &fd, 1, &compcode, &reason); /* Allocate space for the field name */ fd.fd_name = malloc( fd.fd_namelen+1 ); /* Allocate space for the data */ fd.fd_data = malloc( fd.fd_datalen * MQE_SIZEOF(fd.fd_datatype)); /* Get all the data and the name, now we have allocated space */ MQeFieldsGetByIndex(hSess, hFlds, i, &fd, 1, &compcode, &reason); /* Null terminate the name */ fd.fd_name[fd.fd_namelen] = '\0'; free( fd.fd_data ); free( fd.fd_name ); }