Dll Delphi Using on C#

Asked

Viewed 59 times

0

Hello, I’m trying to integrate a dll made in Delphi 10.1 and use its functions in C# however it even accesses the dll but the application CLOSES without giving any error, I will pass as I am doing.

[DllImport("Func.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]

public static extern string TestePAnsiChar();

This test function returns me a string "Dll Loaded!"

This function in Delphi :

function TestePAnsiChar: PAnsiChar; stdcall; begin try Result := 'Dll Carregada!'; except on e: exception do Result := PAnsiChar(AnsiString(e.Message)); end; end;

I believe the problem is the type in c# because I changed this function to return an INT and there it is right, just not with this type Pansichar. Thanks in advance.

1 answer

1

Thanks for the help but I managed to solve the problem as follows in the code c :

[DllImport("Func.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    public static extern IntPtr Configura([MarshalAs(UnmanagedType.AnsiBStr)] string cEmp);

The function in Delphi did not change anything and the c# part was like this :

                IntPtr str = Configura("01");
            string teste = Marshal.PtrToStringAnsi(str);

Thank you and even more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.