DllImport Unmanaged,非.NET Dll到.NET项目代表Char *和Void __StdCall

我有一个非.net的DLL和非托管的Borland C ++,我需要导入。 它返回void并在函数上具有标识符__stdcall。 它还需要传递char *。 当我尝试将其添加为VS 2005中的项目的引用时,它返回无效程序集的错误。

我怎么能在C#中做到这一点?

这是我目前拥有的,它不起作用:

[DllImport ("Project1.dll", CallingConvention=CallingConvention.StdCall)] public static extern IntPtr CustomerForm (String caption); 

对于非托管DLL,不要将其添加为引用。 确保二进制DLL与.NET EXE项目所在的构建位于同一文件夹中,通常是{project} \ bin \ debug。

此外,当您使用Borland C ++构建非托管DLL时,请确保您具有导出的.DEF文件。

编辑:

图书馆项目1
 EXPORTS
     CustomerForm

在您的Borland C ++源代码中,确保将该函数声明为export,例如:

 #ifdef __cplusplus
 __declspec(dllexport)void CustomerForm(char * s);
 #万一

使用它将确保该function可导出并可以链接!

确保DllImport属性的签名与您的本机C ++ Dll的签名相匹配,即

 [DllImport(“Project1.dll”,CallingConvention = CallingConvention.StdCall)]
     public static extern void CustomerForm(string caption);

希望这会有所帮助,最好的问候,汤姆。

要使用DllImport,您不需要添加dll的引用,只需将dll放在可执行文件旁边或system32中即可。