Saturday, November 4, 2006

How to communicate between .NET and Powerbuilder application?

 

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

  1. Create .Net class and add a test method

clip_image002

  1. Compile the solution.
  2. 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

  1. Add the key file to your project

clip_image004

  1. Modify AssemblyInfo.cs

      clip_image006

  1. Add the assembly to GAC

You may go to control panel or use command line(gacutil /i DotNetIdeas.Interop.dll)

  1. 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

  1. Create a Powerbuilder application with a simple window. The window has a static text control and a button.

clip_image008

  1. Add following code in the click event of the button

clip_image010

  1. Now compile and run the application. Click on the “Hello .NET” button, you will receive the response from the .NET class we created above.

clip_image012

 

 

Calling Powerbuilder object/method from .NET

  1. Create a non-visual user object with a method uf_hello

clip_image014

  1. 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.

clip_image016

 

  1. Deploy the project. You will see something similar to the following screen. This will generate a COM dll. Here is testpb.dll

clip_image018

  1. 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

  1. Register both COM dll and .NET dll

>regsvr32 testpb.dll

>regasm NetTestPB.dll

  1. 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.)

clip_image020

  1. Add reference to the DLL we created above

clip_image022

  1. 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.

clip_image024

  1. Build and run the application. Click on the button. This is what you will see.

clip_image026

 

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

 

No comments:

Post a Comment