c# - 从 C# 调用 Delphi 函数

标签 c# delphi pinvoke delphi-xe2 marshalling

我有下面的 DLL 源代码。

library Project1;

uses
  System.SysUtils,
  System.Classes;

type

  IStringFunctions = interface
    ['{240B567B-E619-48E4-8CDA-F6A722F44A71}']
    function GetMethodValueAsString():PAnsiChar; stdcall;
  end;

  TStringFunctions = class(TInterfacedObject, IStringFunctions)
  public
    function GetMethodValueAsString():PAnsiChar; stdcall;
  end;

{$R *.res}

function  TStringFunctions.GetMethodValueAsString():PAnsiChar; stdcall;
begin
  Result := 'test';
end;


procedure GetImplementation(out instance:IStringFunctions); stdcall; export;
begin
  instance := TStringFunctions.Create;
end;

exports GetImplementation;

begin
end.

我想像这样在 C# 中使用

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    [ComVisible(true)]
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("240B567B-E619-48E4-8CDA-F6A722F44A71")]
    public interface IStringFunctions
    {
        [MethodImplAttribute(MethodImplOptions.PreserveSig)]
        [return: MarshalAs(UnmanagedType.AnsiBStr)]
        string GetMethodValueAsString();
    }

    class Program
    {
        [DllImport("kernel32.dll", EntryPoint = "LoadLibrary", CallingConvention = CallingConvention.StdCall)]
        static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

        [DllImport("kernel32.dll", EntryPoint = "GetProcAddress", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

        [DllImport("kernel32.dll", EntryPoint = "FreeLibrary", CallingConvention = CallingConvention.StdCall)]
        static extern bool FreeLibrary(int hModule);

        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        delegate void GetImplementation([MarshalAs(UnmanagedType.Interface)] out IStringFunctions instance);

        static void Main(string[] args)
        {
            const string dllName = "Project1.dll";
            const string functionName = "GetImplementation";

            int libHandle = LoadLibrary(dllName);
            if (libHandle == 0) throw new Exception(string.Format("Could not load library \"{0}\"", dllName));

            var delphiFunctionAddress = GetProcAddress(libHandle, functionName);
            if (delphiFunctionAddress == IntPtr.Zero) throw new Exception(string.Format("Can't find function \"{0}\" in library \"{1}\"", functionName, dllName));

            GetImplementation getImplementation = (GetImplementation)Marshal.GetDelegateForFunctionPointer(delphiFunctionAddress, typeof(GetImplementation));

            if (getImplementation != null)
            {
                IStringFunctions instance = null;
                getImplementation(out instance);

                if (instance != null)
                {
                    //!!! don't return value !!!!
                    String result = instance.GetMethodValueAsString();
                    Console.WriteLine(result);
                }
            }
            Console.ReadLine();
        }
    }
}

但是 instance.GetMethodValueAsString 方法不起作用。和退出代码。

我想在 c# 中使用 dll 函数 (GetMethodValueAsString) 的返回值。

我不明白。

我的错在哪里?

非常感谢

最佳答案

[return: MarshalAs(UnmanagedType.AnsiBStr)]

这是错误的。您不会返回在 COM 堆上分配的 ANSI 编码字符串。您将返回一个普通的 C 字符串,一个指向以 null 结尾的 ANSI 字符数组的指针。

你的接口(interface)声明应该是:

[MethodImplAttribute(MethodImplOptions.PreserveSig)]
IntPtr GetMethodValueAsString();

必须像这样调用方法:

IntPtr ptr = instance.GetMethodValueAsString();
string result = Marshal.PtrToStringAnsi(ptr);

当然,当您需要返回一个动态分配的字符串时,您的界面设计变得相当不切实际。您还需要导出一个解除分配器。处理这个问题的简洁方法是使用 BSTR。像这样:

德尔福

IStringFunctions = interface
  ['{240B567B-E619-48E4-8CDA-F6A722F44A71}']
  procedure GetMethodValueAsString(out value: WideString); stdcall;
end;

C#

[MethodImplAttribute(MethodImplOptions.PreserveSig)]
void GetMethodValueAsString([MarshalAs(UnmanagedType.BStr)] out string result);

关于c# - 从 C# 调用 Delphi 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30041480/

相关文章:

c# - 使用 Unity 创建一个实例并在任何地方使用它

delphi - 与 Delphi 交换 Web 服务

C# - 将指向 sockaddr 结构的 IntPtr 转换为 IPAddress

c# - 从 .net 4 c# 中取消阻止文件

C# - 从 SetWindowText 获取事件

c# - 如何创建空字符串?

c# - 为什么不在我的 DynamicObject 上调用 TryGetMember?

delphi - 在 Windows Server 2008 (sp1) 上运行的 Delphi 应用程序是否不会回收内存?

c# - WPF/Windows 8 应用程序 DataTemplate 数据绑定(bind)事件

xml - Delphi 将子元素添加到 XPath 结果