|
Comments
|
Today's Top SOA Links
Techniques Calling PowerBuilder NVOs in C#
PBNI answers the call
By: David Huo
Jan. 28, 2006 04:00 PM
How can we reuse PowerBuilder NVOs in .NET applications? Before PowerBuilder 9.0, the answer was PowerBuilder automation or a three-tier solution.
Introducing PBNI (PowerBuilder Native Interface) PBNI is a standard programming interface that enables developers to extend the functionality of PowerBuilder. Using PBNI, you can create visual and nonvisual extensions to PowerBuilder (including marshaler extensions) and also embed the PowerBuilder virtual machine (PBVM) into C++ applications. Compared with PowerBuilder automation, PBNI has many advantages:
PBNI provides predefined C++ interfaces to allow the PBVM interact to with other languages (see Figure 1). Table 1 describes some important interfaces.
Calling PowerBuilder NVO from Unmanaged Code 1. Identify the required PowerBuilder library files and the method signature string in the NVO. This can be done using the PowerBuilder library painter. Right-click the method in the NVO and select "Properties". The signature string is shown in the "Signature" text field (see Figure 2). Another way to do it is using pbsig100.exe, which is a command-line tool shipped with PBNI SDK:
pbsig100 <pbl file name>. PBNI uses the NVO's name, method name, and the signature string to find the method to call. 2. Call LoadLibrary() to load the PBVM DLL. In version 10, the PBVM DLL is called pbvm100.dll. 3. Use GetProcAddress() to get the address of the "PB_GetVM" method and then call the PB_GetVM() method to get a reference to the PBVM. 4. Call CreateSession() to create a IPB_Session instance. You can store the IPB_Session reference in a class member or a global variable to reuse the session. Because creating the session is a heavy process, it should be done only once. 5. Call the FindGroup() method of the IPB_Session instance to find the NVO group. The group name should be the same as the NVO's name. 6. Call the FindClass() method of the IPB_Session instance with the group ID and class name. The class name should be the NVO's name. You may feel confused here; what is the point of the group definition? It makes more sense when you try to find a window and controls in the pbl. For example, in a window definition w_1, w_1 is a group, and w_1 and the controls contained in it are all classes of group w_1. 7. Call the NewObject() method of the IPB_Session instance to create an instance of the class returned by FindClass(). 8. Call the GetMethodID() method of the IPB_Session instance to get a reference of the method you want to call. Please make sure to give the correct function name and signature string - (see step 1) 9. Call the InitCallInfo() method of the IPB_Session instance to initialize a PBCallInfo structure. 10. Fill in the parameters and call InvokeObjectFunction() to invoke the method. 11. Retrieve the return value using the PBCallInfo structure. 12. Release the PBCallInfo reference. 13. Release the IPB_Session instance and call FreeLibrary() to unload the PBVM DLL. This step should be done in the destructor of the class or at the moment when you are done with PBNI calls. It sounds like a lot steps, but it is much easier when you see the sample code in Listing 1. In the following example, the PBNI calls are wrapped in a C++ class called CPBInvoker. The DLL loading and IPB_Session initialization are done in the constructor and the clean up is done in the destructor of the class. For the sample, the method we want to call is in an NVO named n_automation and the method name is fn_barcode with a method signature string of "SSS", meaning it returns a string and takes two string type parameters.
How to Pass Parameters
It's a little tricky to get the return value. If the method returns a simple type, such as int, you can use PBCallInfo returnValue->GetInt() to retrieve the value. If the return value requires a memory block, such as a string, you must do this:
pbstring ret = ci.returnValue-> Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||