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:1234567891011121314151617181920212223#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();}; -
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:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253#include "pbadd.h"// initialize the PBXBOOL 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 classPBXEXPORT LPCTSTR PBXCALL PBX_GetDescription(){static const TCHAR desc[]={"class pbadd from nonvisualobject""function int f_add(int a,int b)""end class"};return desc;}// export the required PBX_CreateNonVisualObject// function so that the PBVM can// create an instance of the classPBXEXPORT PBXRESULT PBXCALL PBX_CreateNonVisualObject(IPB_Session* pbSession,pbobject pbobj,LPCSTR xtraName,IPBX_NonVisualObject **obj){// if the calling function requests the pbadd// class, create an instanceif (strcmp(xtraName,"pbadd")==0){*obj=new pbadd;}return 0;}; -
-
Create the pbadd.cpp file, which includes pbadd.h and contains
the implementation of the pbadd class and its single method,
f_add.123456789101112131415161718192021222324252627282930313233343536#include "pbadd.h"// Implement the required Invoke methodPBXRESULT pbadd:: Invoke(IPB_Session *Session,pbobject obj, pbmethodID mid, PBCallInfo *ci){// if the method to call is f_addif (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 destructorpbadd:: pbadd(){}pbadd:: ~pbadd(){}// implement the class's f_add methodint pbadd:: f_add(IPB_Session* session, pbint arg1,pbint arg2){return arg1+arg2;}// Implement the required Destroy methodvoid pbadd::Destroy(){delete this;}
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 %AppeonInstallPath%PowerBuilder [version]SDKPBNI
is in your include path. For this example, the generated DLL is
called pbadd.pbx.