/***************************************************************************/ /* */ /* (c) Copyright IBM Corp. 2004 All rights reserved. */ /* */ /* This sample program is owned by International Business Machines */ /* Corporation or one of its subsidiaries ("IBM") and is copyrighted */ /* and licensed, not sold. */ /* */ /* You may copy, modify, and distribute this sample program in any */ /* form without payment to IBM, for any purpose including developing, */ /* using, marketing or distributing programs that include or are */ /* derivative works of the sample program. */ /* */ /* The sample program is provided to you on an "AS IS" basis, without */ /* warranty of any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, */ /* EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ /* Some jurisdictions do not allow for the exclusion or limitation of */ /* implied warranties, so the above limitations or exclusions may not */ /* apply to you. IBM shall not be liable for any damages you suffer as */ /* a result of using, modifying or distributing the sample program or */ /* its derivatives. */ /* */ /* Each copy of any portion of this sample program or any derivative */ /* work, must include the above copyright notice and disclaimer of */ /* warranty. */ /* */ /***************************************************************************/ /* */ /* Program name: ntloadmq */ /* */ /* Description: Sample C program that puts messages on a message queue */ /* and then gets them back by loading the WebSphere MQ DLL */ /* */ /***************************************************************************/ /* */ /* Function: */ /* */ /* This program is a based on the AMQSGET0.C and AMQSPUT0.C programs */ /* that come with the WebSphere MQ product. They has been modified to */ /* use the LoadLibrary and GetProcAddress APIs to load the WebSphere MQ */ /* library and its entry points. In addition, it shows the use of the */ /* _putenv API to change the environment that the program runs under. */ /* */ /* This program has been tested with the MSVC++ 6.0 compiler: */ /* */ /* cl -MT -Z7 -Od -c -W1 -D_X86=1 -DWIN32 ntloadmq.c */ /* link -OUT:ntloadmq.exe ntloadmq.obj */ /* */ /***************************************************************************/ /* */ /* For a detailed description of what this program does, please refer */ /* to the original source for AMQSGET0.C and AMQSPUT0.C. */ /* */ /***************************************************************************/ /* */ /* ntloadmq has 2 parameters - */ /* - the name of the message queue (required) */ /* - the queue manager name (optional) */ /* */ /* Note: This program overrides the MQSERVER environment variable that */ /* it is started with. In order for the program to work, the line */ /* of code that has: */ /* */ /* _putenv("MQSERVER=SYSTEM.DEF.SVRCONN/tcp/"); */ /* */ /* must be changed for your environment and the program then */ /* needs to be recompiled. */ /* */ /***************************************************************************/ #include #include #include #include #include void (*sconn)(char *, MQHCONN *, MQLONG *, MQLONG *); void (*sopen)(MQHCONN, MQOD *, MQLONG, MQHOBJ *, MQLONG *, MQLONG *); void (*sput) (MQHCONN, MQHOBJ, MQMD *, MQPMO *, MQLONG, MQBYTE *, MQLONG *, MQLONG *); void (*sget) (MQHCONN, MQHOBJ, MQMD *, MQGMO *, MQLONG, MQBYTE *, MQLONG *, MQLONG *, MQLONG *); void (*sclose)(MQHCONN, MQHOBJ *, MQLONG, MQLONG *, MQLONG *); void (*sdisc)(MQLONG *, MQLONG *, MQLONG *); int main(int argc, char **argv) { /* Declare file and character for sample input */ FILE *fp; /* Declare MQI structures needed */ MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */ MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */ MQPMO pmo = {MQPMO_DEFAULT}; /* put message options */ MQGMO gmo = {MQGMO_DEFAULT}; /* get message options */ /** note, sample uses defaults where it can **/ MQHCONN Hcon; /* connection handle */ MQHOBJ Hobj; /* object handle */ MQLONG O_options; /* MQOPEN options */ MQLONG C_options; /* MQCLOSE options */ MQLONG CompCode; /* completion code */ MQLONG OpenCode; /* MQOPEN completion code */ MQLONG Reason; /* reason code */ MQLONG CReason; /* reason code for MQCONN */ MQLONG buflen; /* buffer length */ MQLONG messlen; /* message length received */ char buffer[100]; /* message buffer */ char QMName[50]; /* queue manager name */ char *envvar; HINSTANCE hinstLib; BOOL fl; printf("Sample ntloadmq start\n"); if (argc < 2) { printf("Required parameter missing - queue name\n"); exit(99); } /*************************************************************/ /* Print the original environment variable */ /* Change the environment variable */ /* Print the changed environment variable to show it changed */ /*************************************************************/ envvar = getenv("MQSERVER"); if (envvar != NULL) { printf("Original MQSERVER is: %s\n", envvar); } else { printf("MQSERVER not found\n"); } /* endif */ /********************************************************/ /* Make sure to change to appropriate value! */ /********************************************************/ _putenv("MQSERVER=SYSTEM.DEF.SVRCONN/tcp/"); envvar = getenv("MQSERVER"); if (envvar != NULL) { printf("New MQSERVER is: %s\n", envvar); } /* endif */ /*****************************************************/ /* Load the WebSphere MQ Client DLL */ /*****************************************************/ hinstLib = LoadLibrary("MQIC32"); if (hinstLib == NULL) { printf("LoadLibrary failed"); } else { sconn = (void(*)(char *, MQHCONN *, MQLONG *, MQLONG *))GetProcAddress(hinstLib, "MQCONN"); if (!sconn) { printf("sconn call failed.\n"); exit(0); } else { printf("sconn symbol resolved\n"); } sopen = (void(*)(MQHCONN, MQOD *, MQLONG, MQHOBJ *, MQLONG *, MQLONG *))GetProcAddress(hinstLib, "MQOPEN"); if (!sopen) { printf("sopen call failed.\n"); exit(0); } else { printf("sopen symbol resolved\n"); } sput = (void(*)(MQHCONN, MQHOBJ, MQMD *, MQPMO *, MQLONG, MQBYTE *, MQLONG *, MQLONG *))GetProcAddress(hinstLib, "MQPUT"); if (!sput) { printf("sput call failed.\n"); exit(0); } else { printf("sput symbol resolved\n"); } sget = (void(*)(MQHCONN, MQHOBJ, MQMD *, MQGMO *, MQLONG, MQBYTE *, MQLONG *, MQLONG *, MQLONG *))GetProcAddress(hinstLib, "MQGET"); if (!sget) { printf("sget call failed.\n"); exit(0); } else { printf("sget symbol resolved\n"); } sclose = (void(*)(MQHCONN, MQHOBJ *, MQLONG, MQLONG *, MQLONG *))GetProcAddress(hinstLib, "MQCLOSE"); if (!sclose) { printf("sclose call failed.\n"); exit(0); } else { printf("sclose symbol resolved\n"); } sdisc = (void(*)(MQLONG *, MQLONG *, MQLONG *))GetProcAddress(hinstLib, "MQDISC"); if (!sdisc) { printf("sdisc call failed.\n"); exit(0); } else { printf("sdisc symbol resolved\n"); } } /******************************************************************/ /* Connect to queue manager */ /******************************************************************/ QMName[0] = 0; /* default */ if (argc > 2) { strcpy(QMName, argv[2]); } sconn(QMName, &Hcon, &CompCode, &CReason); /* report reason and stop if it failed */ if (CompCode == MQCC_FAILED) { printf("MQCONN ended with reason code %ld\n", CReason); printf(" > Has the environment variable defined in the program been changed?\n"); exit( (int)CReason ); } /******************************************************************/ /* Use user input parameter as the name of the queue */ /******************************************************************/ strncpy(od.ObjectName, argv[1], (size_t)MQ_Q_NAME_LENGTH); printf("target queue is %s\n", od.ObjectName); /******************************************************************/ /* Open the target message queue for input and output */ /******************************************************************/ O_options = MQOO_OUTPUT /* open queue for output */ + MQOO_INPUT_AS_Q_DEF /* open queue for input also */ + MQOO_FAIL_IF_QUIESCING; /* but not if MQM stopping */ sopen(Hcon, &od, O_options, &Hobj, &OpenCode, &Reason); /* report reason, if any; stop if failed */ if (Reason != MQRC_NONE) { printf("MQOPEN ended with reason code %ld\n", Reason); } if (OpenCode == MQCC_FAILED) { printf("unable to open queue for output\n"); } /*************************************************************************/ /* Read lines from the file (or stdin) and put them to the message */ /* queue. After the put, get the message back. */ /* Loop until null line or end of file, or there is a failure. */ /*************************************************************************/ CompCode = OpenCode; /* use MQOPEN result for initial test */ fp = stdin; memcpy(md.Format, /* character string format */ MQFMT_STRING, (size_t)MQ_FORMAT_LENGTH); while (CompCode != MQCC_FAILED) { if (fgets(buffer, sizeof(buffer), fp) != NULL) { buflen = strlen(buffer); /* length without null */ if (buffer[buflen-1] == '\n') { /* last char is a new-line */ buffer[buflen-1] = '\0'; /* replace new-line with null */ --buflen; /* reduce buffer length */ } } else { buflen = 0; /* treat EOF same as null line */ } /****************************************************************/ /* Put each buffer to the message queue */ /****************************************************************/ if (buflen > 0) { memcpy(md.MsgId, /* reset MsgId to get a new one */ MQMI_NONE, sizeof(md.MsgId) ); memcpy(md.CorrelId, /* reset CorrelId to get a new one */ MQCI_NONE, sizeof(md.CorrelId) ); /*********************************************/ /* put the message using the resolved symbol */ /*********************************************/ sput(Hcon, Hobj, &md, &pmo, buflen, buffer, &CompCode, &Reason); /* report reason, if any */ if (Reason != MQRC_NONE) { printf("MQPUT ended with reason code %ld\n", Reason); } else { /*********************************************************/ /* The message has been put. Now, get it back. */ /*********************************************************/ buflen = sizeof(buffer) - 1; /* buffer size available for GET */ gmo.Options = MQGMO_WAIT /* wait for new messages */ + MQGMO_CONVERT;/* convert if necessary */ gmo.WaitInterval = 15000; /* 15 second limit for waiting */ /**************************************************************/ /* The MsgId and CorrelId are reset to NULL for the get. */ /**************************************************************/ memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId)); memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId)); /**************************************************************/ /* MQGET sets Encoding and CodedCharSetId to the values in */ /* the message returned, so these fields should be reset to */ /* the default values before every call, as MQGMO_CONVERT is */ /* specified. */ /**************************************************************/ md.Encoding = MQENC_NATIVE; md.CodedCharSetId = MQCCSI_Q_MGR; sget(Hcon, Hobj, &md, &gmo, buflen, buffer, &messlen, &CompCode, &Reason); printf("sget buffer=%ld, messlen=%ld\n", buflen, messlen); /* report reason, if any */ if (Reason != MQRC_NONE) { if (Reason == MQRC_NO_MSG_AVAILABLE) { printf("no more messages\n"); } else { printf("MQGET ended with reason code %ld\n", Reason); if (Reason == MQRC_TRUNCATED_MSG_FAILED) { CompCode = MQCC_FAILED; } } } /**************************************************************/ /* Display each message received */ /**************************************************************/ if (CompCode != MQCC_FAILED) { buffer[messlen] = '\0'; /* add terminator */ printf("message <%s>\n", buffer); } } } else { /* satisfy end condition when empty line is read */ CompCode = MQCC_FAILED; } } /******************************************************************/ /* Close the target queue (if it was opened) */ /******************************************************************/ if (OpenCode != MQCC_FAILED) { C_options = 0; sclose(Hcon, &Hobj, C_options, &CompCode, &Reason); /* report reason, if any */ if (Reason != MQRC_NONE) { printf("MQCLOSE ended with reason code %ld\n", Reason); } } /******************************************************************/ /* Disconnect from MQM if not already connected */ /******************************************************************/ if (CReason != MQRC_ALREADY_CONNECTED) { sdisc(&Hcon, &CompCode, &Reason); /* report reason, if any */ if (Reason != MQRC_NONE) { printf("MQDISC ended with reason code %ld\n", Reason); } } /******************************************************************/ /* The program is done with the library, so it needs to be freed. */ /******************************************************************/ fl = FreeLibrary(hinstLib); if (fl) { printf("FreeLibrary returned TRUE (success)\n"); } else { printf("FreeLibrary returned FALSE (failure)\n"); } /* endif */ /******************************************************************/ /* End of ntloadmq */ /******************************************************************/ printf("Sample ntloadmq end\n"); return(0); }