1. Peter Piechutzki
  2. PowerBuilder
  3. Wednesday, 11 November 2020 11:49 AM UTC

Hi, as relative newbe into C#integration  with PB (2919 R2) I thought I'd give it a try. So I started with a  .net dll function as simple as it could be created with snapdevelop in powerscript.

- Snapdevelop ClassLibrary 1 c# Code:

using System;
namespace tester
{
    public class Utility 
    {
        public string ReadEnv()
        {
            string user = Environment.UserName;
            return user;
        }
    }
}

- Compiled, Built and Published fine with all depedenys to directory D:\Temp\pp\

- Imported into Powerbuilder with .NET DLL Importer as nvo_utility,  PB Source code follows

forward
global type nvo_utility from dotnetobject
end type
end forward

global type nvo_utility from dotnetobject
end type
global nvo_utility nvo_utility

type variables

PUBLIC:
String is_assemblypath = "D:\Temp\pp\ClassLibrary1.dll"
String is_classname = "tester.Utility"
end variables

forward prototypes
public function string of_readenv ()
end prototypes

public function string of_readenv ();
//*-----------------------------------------------------------------*/
//*  .NET function : ReadEnv
//*   Return : String
//*-----------------------------------------------------------------*/

Return This.readenv()
end function

on nvo_utility.create
call super::create
TriggerEvent( this, "constructor" )
end on

on nvo_utility.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

- Trying to use it in PB results in Error "Object instance does not exist " - cb_1.clicked() event

string ls_username
nvo_utility lnvo_utility
lnvo_utility = create nvo_utility
ls_username = lnvo_utility.of_readenv( )  // Exits with error
messagebox("Gotcha",ls_username)
destroy lnvo_utility

I tried chaging the "Return This.readenv()" to  "Return This:ReadEnv()" since c# is case sensitive but run into same runtime error. Then i tried Bruce's .Net Importer Demo from CodeExchange resulting in the same Error as soon as I call the windows function of_sendmail from w.main.

Any Idea's what I'm missing?

regards

Peter

P.S. I Sure look forward to the elevate conference!

 

 

 

 

 

Accepted Answer
Mark Lee @Appeon Accepted Answer Pending Moderation
  1. Thursday, 12 November 2020 07:33 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi Peter,

 

I tested your code and the issue is caused by that there's no Create Instance for your DotNetObject object.

The two methods below can solve the issue:

Method 1: Use .NET DLL Importer module to import the dll again as nvo_utility. Please remember when you import this object, you need to expand "Advance Settings" and select "Encapsulate a DotNetAssembly object in each DotNetObject object".

For instructions related to .NET DLL Importer module, please refer to the link below:

https://docs.appeon.com/pb2019r2/application_techniques/ch20s02.html 

And then run your PB end case again. Execute this cb_1.clicked() event to test it again.

 

Method 2: Modify the cb_1.clicked() event code in your PB case as below and then test again:

string ls_username
nvo_utility lnvo_utility

lnvo_utility = create nvo_utility

long	ll_status
DotNetAssembly lnv_assembly
lnv_assembly = Create DotNetAssembly
ll_status = lnv_assembly.LoadWithDotNetFramework(lnvo_utility.is_assemblypath )
ll_status = lnv_assembly.CreateInstance(lnvo_utility.is_classname, lnvo_utility)

ls_username = lnvo_utility.of_readenv( )  // Exits with error
messagebox("Gotcha",ls_username)
destroy lnvo_utility
//destroy lnv_assembly

 

Regards,

Attachments (1)
Comment
  1. Peter Piechutzki
  2. Thursday, 12 November 2020 08:23 AM UTC
Thank you very ver much Mark, you saved my day !



True, Yesterday i actually missed the Method 1 of your suggestion. Today, before I got your answer i came up with just about the same as your Method 2 after i follwed the powerbuilder help "Importing .NET assembly" by using scripted Loadwithdotnetcore() and Createinstance() :-)



I must say method 1 is alot easier and I just missed it in the Application techniques part of Help. So I'd like to emphasisze appeon to maybe note this more clearly in the PB Help or even make 'Encapsulate a DonenetAssembly object in each DotNetObject object' a default setting for Import.

  1. Helpful
  1. Mark Lee @Appeon
  2. Thursday, 12 November 2020 09:00 AM UTC
Hi Peter,

I think your advice is very reasonable.

I will record it as a new enhancement/requirement request and will transfer it to our product team for consideration.
  1. Helpful
There are no comments made yet.
Aleš Vojáček Accepted Answer Pending Moderation
  1. Tuesday, 16 February 2021 09:03 AM UTC
  2. PowerBuilder
  3. # 1

Is there any way to use method 1 with relative path to assembly? 

I mean I will copy assebly in lib folder inside Powebuilder workspace directory.

When I import that assembly it imports it and set location in absolute path. If I set assemblky locationa to ".\libs\asmbl.dll"

Method 1 does not work.

Should I use Method 2 in this scenario or there is something what I missed?

 

Thank you Ales

Comment
There are no comments made yet.
Peter Piechutzki Accepted Answer Pending Moderation
  1. Wednesday, 11 November 2020 11:51 AM UTC
  2. PowerBuilder
  3. # 2

Forgot to add, if anyone has a complete sample in source code for Snap Develop and PB 2019R2 to share it would be realy great!

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.