Skip to main content
 首页 » 编程设计

c#之在 C# 中使用 Delphi native dll(DllImport 问题)

2024年02月20日12Free-Thinker

我在将 Delphi native dll 导入 .net 时遇到问题。 我收到 System.EntryPointNotFoundException。

这是我的 delphi dll:

procedure ProcedimientoEncriptar(texto,clave,resultado:PChar); export stdcall; 
    var ... 
    begin 
    .... 
    .... 
    end; 
 
    exports 
      ProcedimientoEncriptar ; 

这是我在 .Net (C#) 上的 DllImport 代码:

[DllImport("CryptoDLL.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)] 
        public static extern void ProcedimientoEncriptar([MarshalAs(UnmanagedType.LPStr)]string texto, [MarshalAs(UnmanagedType.LPStr)]string clave, [MarshalAs(UnmanagedType.LPStr)] StringBuilder resultado); 

任何帮助将不胜感激, 迭戈。

请您参考如下方法:

System.EntryPointNotFoundException 的唯一解释是 C# 代码找到的 DLL 不是您显示的从 Delphi 代码生成的 DLL。因此,也许 C# 代码正在获取 DLL 的过时版本。或者它可能正在选择一个完全不同的 DLL。

例如,我的系统在system32目录下有一个名为cryptdll.dll的DLL。这很可能是您的 C# 代码正在查找的 DLL。

为了确保找到正确的 DLL,您需要将 Delphi DLL 的副本放在与 C# 可执行文件相同的目录中。