This is an article I wrote long time ago. The Powerbuilder version was 9.0, .NET was 1.1
Calling .NET class/method from Powerbuilder
- Create .Net class and add a test method
- Compile the solution.
- Create a key pair for signing the assembly with a strong name. See Creating a key pair for detail.
For example, c:\yourpath>sn –k DotNetIdeas.snk
- Add the key file to your project
- Modify AssemblyInfo.cs
- Add the assembly to GAC
You may go to control panel or use command line(gacutil /i DotNetIdeas.Interop.dll)
- Register the assembly using regasm tool.
regasm DotNetIdeas.Interop.dll
The following message is copied from MSDN
The Assembly Registration tool reads the metadata within an assembly and adds the necessary entries to the registry, which allows COM clients to create .NET Framework classes transparently. Once a class is registered, any COM client can use it as though the class were a COM class. The class is registered only once, when the assembly is installed. Instances of classes within the assembly cannot be created from COM until they are actually registered.
To unregister, call regasm /u yourdll.dll
- Create a Powerbuilder application with a simple window. The window has a static text control and a button.
- Add following code in the click event of the button
- Now compile and run the application. Click on the “Hello .NET” button, you will receive the response from the .NET class we created above.
Calling Powerbuilder object/method from .NET
- Create a non-visual user object with a method uf_hello
- Create a project object using the COM/MTS/COM+ Component Wizard. You may change the name and you will need to choose which user object to be deployed to the COM component server. You don’t have to change anything other than that. Just click Next all the way through.
- Deploy the project. You will see something similar to the following screen. This will generate a COM dll. Here is testpb.dll
- Use the Type Library Importer converts the type definitions found within a COM type library into equivalent definitions in a common language runtime assembly
>tlbimp testpb.dll /keyfile:DotNetIdeas.snk /out:NetTestPB.dll
- Register both COM dll and .NET dll
>regsvr32 testpb.dll
>regasm NetTestPB.dll
- Create a .NET windows application with a form looks like the form below. It has a label and a button. (Here I re-used the project I created in previous section.)
- Add reference to the DLL we created above
- Add the following code to the click event of the button. You may notice that the Powerbuilder class has weird long name. That was generated when you use the COM component wizard. You can give it more readable name.
- Build and run the application. Click on the button. This is what you will see.
Caution: How to call Powerbuilder COM component from .NET web page/web services?
If you want to use the same code we showed above to call Powerbuilder COM component from .NET web application, you may receive “System.NullReferenceException: Object reference not set to an instance of an object.” exception. That is because the Powerbuilder COM component we created above is single tread. To make .NET initialize an STA, you have to explicitly indicate that this is your intention. Here is how you would do it from a web service