1
votes

I need to call a 'classic' DLL C# (not COM) from Powerbuilder. The creation of the DLL in C# I based on this example: RGiesecke dll Export template.

And I managed to call the DLL from within Powerbuilder.

BUT I want to pass the string 'as reference': so I added 'ref' to the function declaration:

[DllExport("ExpTest", CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static string ExpTest([MarshalAs(UnmanagedType.LPWStr)] ref string sText, out int length)
{
    MessageBox.Show(sText, "ExpTest");
    length = sText.Length;
    //sText = "def";
    return sText;
}

The code from Powerbuilder calling this function:

String ls_arg, ls_ret
ls_arg = "abc"
long ll_len
ls_ret = ExpTest(ls_arg, ll_len)
messagebox(ls_arg, ls_ret)

When calling the original functio (without 'ref' to the 'string sTest' declaration), it returns "abc". When I add 'ref' to the 'string sTest' declaration, it returns some 'chinese characters').

Can anyone help? Even better: how to pass an array of strings (by ref) from and to Powerbuilder?

Thanks for your help!!

Msc.

Tried to define the External functions in Powerbuilder like this: - FUNCTION String ExpTest(REF String value, REF long len) LIBRARY "Classicdll.dll"
- FUNCTION String ExpTest(String value, REF long len) LIBRARY "Classicdll.dll"
- and both with ALIAS FOR "ExpTest;Ansi"...

1
In PoweBuilder. How is it done in "Local External Functions"?Eduardo G.
Hi Eduardo, I've added the declaration(s) I used in the post....MSC

1 Answers

1
votes

Do not use ref. You have to use ref only if you want to change what the object is not what it contains! Strings in .NET are special objects. They're acting like a value type but they're a reference type. More information can be found here:

I think your chinese characters are the reference, as a string, to the String object and not a String which contains chinese characters.

EDIT: It looks like that using unmanged exports is a ugly hack. So creating a C# COM *.dll would be the better (supported) way of writing extensions for PowerBuilder.