Calling a delphi DLL method from C# Code

前端 未结 2 1618
忘掉有多难
忘掉有多难 2021-01-13 07:51

I am trying to call a Delphi function from C# ASP.NET code. The function\'s declaration looks like this:

function SomeFunction(const someString, SomeOtherStr         


        
相关标签:
2条回答
  • 2021-01-13 08:02

    Try

    [DLLImport(MyDLL.dll, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    public static extern IntPtr SomeFunction(string someString, string SomeOtherString);
    

    and use Marshal.GetObjectForNativeVariant with the return value to get a .NET object.

    0 讨论(0)
  • 2021-01-13 08:20

    You cannot call that DLL function because it uses the Delphi-specific string data type, which has no equivalent in non-Embarcadero products. (Even if your C# code can match the structure of Delphi's string type, you would also need to allocate the memory using the DLL's memory manager, which it almost certainly doesn't export.)

    If you have the ability to change the DLL, then make the parameters have type PAnsiChar or PWideChar. (From your C# declaration, it looks like you want PAnsiChar.) That's what the DLL should have used all along.

    If you can't change the DLL, then write a wrapper DLL in Delphi or C++ Builder that uses PAnsiChar or PWideChar and then forwards those parameters to the original Delphi DLL. Or complain loudly to the DLL vendor and request a new version that uses types that are more friendly to other languages.

    0 讨论(0)
提交回复
热议问题