PreviousNext

Writing a C Client for C++ Servers

If you wish, you can develop C language clients that use interfaces written with C++ features. Whenever the interface definition is compiled for C++, C structures, macros, and function prototypes are automatically built into the header file and stubs to give this capability.

For example, the following get( )operation is defined in the Matrix interface definition:

long get(
long row,
long col
);

The macros generated by the IDL compiler are formed by combining the name of the interface and the name of the operation with an underscore between. For example, to allow a C client to invoke the get operation on the interface, the IDL compiler generates the following macro in the header file:

Matrix_get(obj, row, col)

Since member functions cannot be called in C with an implied object (the C++ this object), each member function for the C macros has an additional object argument as the first parameter. The remaining arguments are the same as those specified in the IDL input file.

To obtain the interface pointer using the C mapping, use one of the bind routines generated by the IDL compiler for the C interface. These are also generated in the header file. For example, the Matrix interface supports the following C macros for binding to a remote object:

Matrix *Matrix_bind_by_name(unsigned_char_t *name);
Matrix *Matrix_bind_by_uuid(uuid_t * u);
Matrix *Matrix_bind_by_hndl(rpc_binding_handle_t bh);

All static member functions of an interface are also supported for C. The macros are formed in a manner similar to the normal member functions (by joining the interface name and the operation name with an underscore), except there is no need for an additional argument to represent a current object. For example, if the Matrix interface supports the createMatrix( ) static operation, the following example C code invokes the operation:

/* code fragment showing the use of C macros */
Matrix *m; /* a C structure to represent an interface */
/*
** invoke a static member function to get an interface
** pointer and invoke operations on it.
*/
m = Matrix_createMatrix(1, 2, 3, 4);
if (!m) {
/* handle error */
}
else {
printf("[%d, %d]\\n", Matrix_get(m, 0, 0), Matrix_get(m, 0, 1));
printf("[%d, %d]\\n", Matrix_get(m, 1, 0), Matrix_get(m, 1, 1));
}