Building the pbadd PowerBuilder extension
In this example, the C++ code is in three
files:
-
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.
To implement the pbadd extension:
-
Create the pbadd.h header
file.The pbadd.h header file declares the
pbadd class. The file includes pbext.h, which
must be included in all PowerBuilder extensions because it declares
the ancestor classes for native classes and the standard functions that
the extension must expose. Here is the code for pbadd.h:1#include "pbext.h"<br>class pbadd: public IPBX_NonVisualObject<br>{<br>public:<br>   pbadd();<br>   virtual ~pbadd();<br>   PBXRESULT Invoke(<br>      IPB_Session   *session,<br>      pbobject      obj,<br>      pbmethodID    mid,<br>      PBCallInfo    *ci);<br> <br>   int f_add(IPB_Session*, pbint, pbint);1<br>// Enum used to provide entry points for each<br>// method in the class - the only one in this case<br>// is mAdd<br>   enum MethodIDs<br>   {<br>      mAdd = 0 <br>   };<br> <br>private:<br>   virtual void Destroy();<br>}; -
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.
The following is the code for main.cpp:
1#include "pbadd.h"<br>// initialize the PBX<br>BOOL APIENTRY DllMain(HANDLE hModule,<br>                DWORD ul_reason_for_all,<br>                 LPVOID lpReserved<br>              )<br>{<br>   switch(ul_reason_for_all)<br>   {<br>      case DLL_PROCESS_ATTACH:<br>      case DLL_THREAD_ATTACH:<br>      case DLL_THREAD_DETACH:<br>      case DLL_PROCESS_DETACH:<br>         break;<br>   }<br>   return TRUE;<br>}<br> <br>1234// describe the pbadd class<br>PBXEXPORT LPCTSTR PBXCALL PBX_GetDescription()<br>{<br>   static const TCHAR desc[]={<br>      "class pbadd from nonvisualobject" <br>      "function int f_add(int a,int b)" <br>      "end class"<br>   };<br>return desc;<br>}<br> <br>1// export the required PBX_CreateNonVisualObject<br>// function so that the PBVM can<br>// create an instance of the class<br>PBXEXPORT PBXRESULT PBXCALL PBX_CreateNonVisualObject<br>(<br>   IPB_Session*    pbSession,<br>   pbobject        pbobj,<br>   LPCSTR          xtraName,<br>   IPBX_NonVisualObject   **obj<br>)<br>{<br>   // if the calling function requests the pbadd<br>   // class, create an instance<br>   if (strcmp(xtraName,"pbadd")==0)<br>      {<br>         *obj=new pbadd;<br>   }<br>   return 0;<br>}; -
-
Create the pbadd.cpp file,
which includes pbadd.h and contains the implementation
of the pbadd class and its single method, f_add.1#include "pbadd.h"<br> <br>// Implement the required Invoke method<br>PBXRESULT pbadd:: Invoke(IPB_Session *Session,<br>   pbobject obj, pbmethodID mid, PBCallInfo *ci)<br>{<br>   // if the method to call is f_add<br>   if (mid == mAdd)1   {<br>      int sum = f_add(Session, ci->pArgs->GetAt(0)-><br>         GetInt(), ci->pArgs->GetAt(1)->GetInt());<br>      ci->returnValue->SetInt(sum);<br>   }<br>   return PBX_OK;<br>}<br> <br>// constructor and destructor<br>pbadd:: pbadd()<br>{<br>}<br>pbadd:: ~pbadd()<br>{<br>}<br> <br>// implement the class's f_add method<br>int pbadd:: f_add(IPB_Session* session, pbint arg1,<br>   pbint arg2)<br>{<br>   return arg1+arg2;<br>}<br> <br>// Implement the required Destroy method<br>void pbadd::Destroy()<br>{<br>   delete this;<br>}
To compile and link the PBX:
-
In your C++ development
tool or on the command line, compile and link the PBX.Make sure the include directory in PowerBuilder
12.6SDKPBNI is in your include path.
For this example, the generated DLL is called pbadd.pbx.