Building the pbadd PowerBuilder extension

PowerBuilder Native Interface

Building the pbadd PowerBuilder extension

  • The class declaration is in a header file, pbadd.h

  • The standard functions that every PowerBuilder extension must expose are in main.cpp

  • The implementation of the class is in pbadd.cpp.

  1. Create the pbadd.h header file.

    #include "pbext.h"
    class pbadd: public IPBX_NonVisualObject
    {
    public:
       pbadd();
       virtual ~pbadd();
       PBXRESULT Invoke(
          IPB_Session   *session,
          pbobject      obj,
          pbmethodID    mid,
          PBCallInfo    *ci);

       int f_add(IPB_Session*, pbint, pbint);

    // Enum used to provide entry points for each
    // method in the class - the only one in this case
    // is mAdd
       enum MethodIDs
       {
          mAdd = 0
       };

    private:
       virtual void Destroy();
    };
  2. Create the main.cpp file, which includes pbadd.h and implements the standard functions, PBX_GetDescription and PBX_CreateNonvisualObject:.

    • PBX_GetDescription is used to pass the descriptions of classes in the extension to PowerBuilder.

    • The PBX_CreateNonVisualObject method creates the object instance. The PowerScript CREATE statement maps to this PBNI method.

    #include "pbadd.h"
    // initialize the PBX
    BOOL APIENTRY DllMain(HANDLE hModule,
                    DWORD ul_reason_for_all,
                     LPVOID lpReserved
                  )
    {
       switch(ul_reason_for_all)
       {
          case DLL_PROCESS_ATTACH:
          case DLL_THREAD_ATTACH:
          case DLL_THREAD_DETACH:
          case DLL_PROCESS_DETACH:
             break;
       }
       return TRUE;
    }

    // describe the pbadd class
    PBXEXPORT LPCTSTR PBXCALL PBX_GetDescription()
    {
       static const TCHAR desc[]={
          "class pbadd from nonvisualobject \n" \
          "function int f_add(int a,int b)\n" \
          "end class \n"
       };
    return desc;
    }

    // export the required PBX_CreateNonVisualObject
    // function so that the PBVM can
    // create an instance of the class
    PBXEXPORT PBXRESULT PBXCALL PBX_CreateNonVisualObject
    (
       IPB_Session*    pbSession,
       pbobject        pbobj,
       LPCSTR          xtraName,
       IPBX_NonVisualObject   **obj
    )
    {
       // if the calling function requests the pbadd
       // class, create an instance
       if (strcmp(xtraName,"pbadd")==0)
          {
             *obj=new pbadd;
       }
       return 0;
    };
  3. Create the pbadd.cpp file, which includes pbadd.h and contains the implementation of the pbadd class and its single method, f_add.

    #include "pbadd.h"

    // Implement the required Invoke method
    PBXRESULT pbadd:: Invoke(IPB_Session *Session,
       pbobject obj, pbmethodID mid, PBCallInfo *ci)
    {
       // if the method to call is f_add
       if (mid == mAdd)
       {
          int sum = f_add(Session, ci->pArgs->GetAt(0)->
             GetInt(), ci->pArgs->GetAt(1)->GetInt());
          ci->returnValue->SetInt(sum);
       }
       return PBX_OK;
    }

    // constructor and destructor
    pbadd:: pbadd()
    {
    }
    pbadd:: ~pbadd()
    {
    }

    // implement the class's f_add method
    int pbadd:: f_add(IPB_Session* session, pbint arg1,
       pbint arg2)
    {
       return arg1+arg2;
    }

    // Implement the required Destroy method
    void pbadd::Destroy()
    {
       delete this;
    }

  1. In your C++ development tool or on the command line, compile and link the PBX.

    Make sure the include directory in PowerBuilder 15.0\SDK\PBNI is in your include path. For this example, the generated DLL is called pbadd.pbx.