C++ as client

In C++, you use COM library functions to create an instance of a PowerBuilder COM object. You also need to use C/C++ definitions of the PowerBuilder COM objects when you build the client. The Microsoft IDL (MIDL) compiler generates these definitions from the IDL file created by the PowerBuilder COM generator.

For example, using the IDL file generated for the Employee PowerBuilder COM object:

midl.exe employee.idl

The MIDL compiler generates a header file (employee.h) containing definitions of the interfaces of all the objects in the IDL file and a C file (employee_i.c) that defines the CLSIDs and Interface IDs (IIDs) associated with the object.

NoteAdditional files The MIDL compiler also generates proxy/stub code (in employee_p.c and dlldata.c), but you do not need to use the proxy/stub code to build the C++ client executable or access the PowerBuilder COM object.

Building a client To build a C++ client executable that can access methods in a PowerBuilder COM object, you create a C++ source file that includes the generated header file, compile both the C++ client source file and the C file generated by the MIDL compiler, then link the resulting object files to create the executable.

For the Employee example:

  1. Create a C++ source file called client.cpp (shown below).

  2. Compile client.cpp.

  3. Compile employee_i.c.

  4. Link client.obj and employee_i.obj to create an executable—for example, employee_ecl.exe.

Employee.h The following code fragments from the employee.h header file generated by the MIDL compiler show the definitions to be used by C++ clients:

typedef interface DIEmployee DIEmployee;
EXTERN_C const IID IID_DIEmployee;

interface DECLSPEC_UUID("A2F59F71-D5FB-11D1-92B9-00A0247712F1")
    DIEmployee : public IDispatch
    {
    public: 
    virtual /* [id] */ HRESULT STDMETHODCALLTYPE
       f_calcdayavg( 
      /* [in] */ long units,
      /* [in] */ long time,
      /* [retval][out] */ double __RPC_FAR 
        *retval) = 0;

    virtual /* [id] */ HRESULT 
        STDMETHODCALLTYPE f_teststring( 
        /* [retval][out] */ BSTR __RPC_FAR
           *retval) = 0;
    };

EXTERN_C const CLSID CLSID_CoEmployee;

Client.cpp The following sample client file uses the MIDL-generated C/C++ definitions of PowerBuilder COM objects. For further information on the COM API calls shown in client.cpp, see the Microsoft Software Development Kit documentation.

#include <windows.h>
// employee.h I( from MIDL.EXE)
#include "employee.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
    HRESULT hr;
    DIEmployee      *pDIEmployee = 0;

    //  Initialize COM
    CoInitialize(0);

    hr = CoCreateInstance(CLSID_CoEmployee,NULL,
       CLSCTX_INPROC_SERVER, IID_DIEmployee,
      (void **)&pDIEmployee);
    if (FAILED(hr))
    ErrorMessage("CoCreateInstance", hr);

    // variables for methods
    long units, time;
    double dReturn;
    BSTR  strReturn = NULL;
    
    // call methods
    hr = pDIEmployee->f_calcdayavg(units,
                   time,&dReturn);
    if (FAILED(hr))
    ErrorMessage("f_calcdayavg",hr);  

    hr = pDIEmployee->f_teststring(&strReturn);
    if (FAILED(hr))
    ErrorMessage("f_teststring",hr);

    // release the interface ptr
    pDIEmployee->Release();

    CoFreeUnusedLibraries();
    //  all done!
    
CoUninitialize();
    return 0;
}