11.07.2015 Views

in PowerBuilder - sys-con.com's archive of magazines - SYS-CON ...

in PowerBuilder - sys-con.com's archive of magazines - SYS-CON ...

in PowerBuilder - sys-con.com's archive of magazines - SYS-CON ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

List<strong>in</strong>g 1// Def<strong>in</strong>e the function po<strong>in</strong>ter for PB_GetVMtypedef PBXEXPORT PBXRESULT (*P_PB_GetVM)(IPB_VM** vm);// <strong>con</strong>structorCPBInvoker::CPBInvoker(LPCTSTR LibList[], <strong>in</strong>t libNum, LPCT-STR appName){_h<strong>in</strong>st = NULL; // a member variable to hold the DLL handleIPB_VM* pbvm;_h<strong>in</strong>st = LoadLibrary("pbvm100.dll"); // step2: load PBVMif ( _h<strong>in</strong>st == NULL ) {throw "Loaded PBVM successfully";}// step 3: Get PBVM referenceP_PB_GetVM getvm =(P_PB_GetVM)GetProcAddress(_h<strong>in</strong>st,"PB_GetVM");if (getvm == NULL) {throw "PB_GetVM failed";}getvm(&pbvm);if (pbvm == NULL) {throw "getvm failed";}// step 4: Create IPB_Session <strong>in</strong>stance, LibList is anarray holds the pbl file namesif ( pbvm->CreateSession(appName, LibList, libNum, &_session)!= PBX_OK ) {throw "Error <strong>in</strong> CreateSession";}}// destructorCPBInvoker::~CPBInvoker(){// step 13: Release IPB_Sessionif( NULL != _session ) {_session->Release();}// unload PBVM DLLif( NULL != _h<strong>in</strong>st ) {FreeLibrary(_h<strong>in</strong>st);}}// Call the NVO methodvoid CPBInvoker::barcode(LPCTSTR text, LPCTSTR format,char* code){// step 5: F<strong>in</strong>d the group, n_automation is the NVO’snamepbgroup group = _session->F<strong>in</strong>dGroup("n_automation",pbgroup_userobject);if (group == NULL) throw "n_automation is not found!";// step 6: F<strong>in</strong>d the class, n_automation is the NVO’snamepbclass cls = _session->F<strong>in</strong>dClass(group,"n_automation");if (cls == NULL) throw "n_automation class notfound!";objectpbobject pbobj = _session->NewObject(cls);// PBCallInfo <strong>con</strong>ta<strong>in</strong>s arguments and return valuePBCallInfo ci;// step 8: GetMethodID to get the method. “SSS” is thesignature str<strong>in</strong>g, fn_barcode is// the function namepbmethodID mid = _session->GetMethodID(cls, "fn_barcode",PBRT_FUNCTION, "SSS");// step 9: Initialize call <strong>in</strong>fo structure based onmethod ID_session->InitCallInfo(cls, mid, &ci);// step 10: Fill <strong>in</strong> parameters.ci.pArgs-> GetAt(0)->SetStr<strong>in</strong>g(text);ci.pArgs-> GetAt(1)->SetStr<strong>in</strong>g(format);// step 11: <strong>in</strong>voke the method_session->InvokeObjectFunction(pbobj, mid, &ci);// step 12: Retrieve the return value, code is a validmemory// buffer passed <strong>in</strong> by the callerpbstr<strong>in</strong>g ret = ci.returnValue->GetStr<strong>in</strong>g();strcpy( code, _session->GetStr<strong>in</strong>g(ret) );}// step 13: Release the PBCallInfo structure_session->FreeCallInfo( &ci );List<strong>in</strong>g 2#def<strong>in</strong>e PBINVOKER_API __declspec(dllexport)PBINVOKER_API void barcode(LPCTSTR specimen_formatted,LPCTSTR spcimen_id, char *code){// _<strong>in</strong>voker is a global variable holds the reference <strong>of</strong>the PBInvoker objectif( NULL != _<strong>in</strong>voker ) {// call the NVO method_<strong>in</strong>voker->barcode(specimen_formatted, spcimen_id, code);}}PBINVOKER_API void <strong>in</strong>itPBSession(LPCTSTR libList[], <strong>in</strong>tlibNum, LPCTSTR appName){// <strong>in</strong>it the IPB_Sessionif( NULL == _<strong>in</strong>voker ) {// <strong>in</strong>stantiate the CPBInvoker wrapper class_<strong>in</strong>voker = new CPBInvoker(libList, libNum, appName);}}PBINVOKER_API void closePBSession(){// clean upif( NULL != _<strong>in</strong>voker ) {// delete the wrapper objectdelete _<strong>in</strong>voker;_<strong>in</strong>voker = NULL;}}// step 7: Create an <strong>in</strong>stance <strong>of</strong> the <strong>PowerBuilder</strong>pbdj.<strong>sys</strong>-<strong>con</strong>.comPBDJ volume12 issue1129

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!