从C#导入非托管代码导入到delphi

时间:2012-04-19 17:34:21

标签: c# delphi marshalling unmanaged dllimport

我有一个连接到运行良好的dll的c#类。

现在我在Delphi中需要相同的功能。最好的方法是什么?我不想在Delphi中再次编写所有导入。有没有办法在delphi中导入c#类或任何其他方式快速简单地进行...

这是我的c#类中的一个函数:

    /// Return Type: ABS_STATUS->ABS_LONG->int
    ///pszDsn: ABS_CHAR*
    ///phConnection: ABS_CONNECTION*
    [System.Runtime.InteropServices.DllImportAttribute("bsapi.dll", EntryPoint = "ABSOpen", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    public static extern int ABSOpen([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string pszDsn, ref uint phConnection);

1 个答案:

答案 0 :(得分:1)

在Delphi中这很简单:

function ABSOpen(pszDsn: PAnsiChar; var phConnection: Cardinal): Integer; 
    stdcall; external 'bsapi.dll';

毫无疑问,你有更多的功能,但它们应该都很容易。如果我是你,我会回到最初的C ++头文件,它比你的p / invokes更容易翻译。

我认为p / invoke到Delphi导入单元没有任何好的转换工具。即使对于Delphi的C ++标题,我也不相信有很好的通用工具。您可以查看JEDI project which has some useful resources,但在我看来,是时候卷起袖子了!