0

I am trying access methods of a C# DLL (created one) from PowerBuilder but getting Error: Error calling external function.

C# Code

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace AESKeyGen
{
    public static class AESKeyGen_
    {
        static string secretKey;

        private static byte[] generateAppKey()
        {
            Aes KEYGEN = Aes.Create();
            byte[] secretKey = KEYGEN.Key;
            return secretKey;
        }

        [DllExport("AESKey_generate", CallingConvention = CallingConvention.StdCall)]
        public static string AESKey_generate()
        {
            secretKey = Convert.ToBase64String(generateAppKey());
            return secretKey;
        }
    }
}

PowerBuilder Code

FUNCTION string AESKey_generate() LIBRARY "AESKeyGen.dll

ls_AESKey = AESKey_generate()

I could able to test this DLL using another C# app successfully by adding reference. tried to check methods exported by AESKeyGen DLL using depends.exe but could not any methods listed there. I did try few things mentioned on internet but no luck.

please suggested.

  • Does this help? https://stackoverflow.com/questions/34306591/use-c-sharp-created-dll-in-powerbuilder-12-5-2 – Ben Dec 16 '20 at 17:06
  • Yes, I able to call DLL functions from PB. Thank you Ben. Now I am facing another issue :) PB showing Chinese text. This may be because string is an object in C#. any suggestions on how to fix this? – user3541371 Dec 17 '20 at 17:28
  • It could be that C# uses unicode by default or that string is a reference type and PB doesnt handle it properly. You could call Encoding.UTF8.GetBytes (or encoding.ASCII etc) and return the byte array and this also allows you to define the encoding rather than relying on the default encoding of each. – Ben Dec 18 '20 at 10:20
  • I believe PB uses ANSI encoding by default from a quick google search but not 100% sure. – Ben Dec 18 '20 at 10:22
  • In this one, the asker was getting Chinese characters: https://stackoverflow.com/questions/65094693/how-to-get-the-host-name-and-ipv4-address-in-powerbuilder-2019 – Slapout Dec 22 '20 at 14:57
  • Thank you all for your suggestions! I could solve this issue by changing properties of project and adding return: MarshalAs. – user3541371 Dec 23 '20 at 15:29

0 Answers0