Example: Callable Library message channels

Demonstrates control of message channels in the C API.

This example shows you how to use the CPLEX message handler from the Callable Library. It captures all messages generated by CPLEX and displays them on screen along with a label indicating which channel sent the message. It also creates a user channel to receive output generated by the program itself. The user channel accepts user-generated messages, displays them on screen with a label, and records them in a file without the label.

The complete program lpex5.c appears online in the standard distribution at yourCPLEXinstallation /examples/src. This example derives from lpex1.c, a program in Getting Started. There are a few differences between the two examples:

  • In this example, the function ourmsgfunc manages all output. The program itself, or rather CPXXmsgstr and CPXmsgstr from the CPLEX Callable Library, calls ourmsgfunc. In fact, CPXmsgstr is a replacement for printf, allowing a message to appear in more than one place, for example, both on screen and in a file.

    Only after you initialize the CPLEX environment by calling CPXopenCPLEX can you call CPXmsgstr. And only after you call CPXgetchannels can you use the default CPLEX channels. Therefore, calls to ourmsgfunc print directly any messages that occur before the program gets the address of cpxerror (a channel). After a call to CPXgetchannels gets the address of cpxerror, and after a call to CPXaddfuncdest associates the message function ourmsgfunc with cpxerror, then error messages are generated by calls to CPXmsgstr.

    After the TERMINATE: label, any error must be generated with care in case the error message function has not been set up properly. Thus, ourmsgfunc is also called directly to generate any error messages there.

  • System function fopen opens the file lpex5.msg to accept solution information. Solution information is also displayed on screen since ourmsgfunc is associated with that new channel, too. Thus in the loops near the end of main , when the solution is printed, only one call to CPXmsgstr suffices to put the output both on screen and into the file.

  • Although CPXcloseCPLEX will automatically delete file- and function-destinations for channels, it is a good practice to call CPXXdelfuncdest and CPXdelfuncdest at the end of your programs.