简体   繁体   English

在C#类库中包含DLL

[英]Including DLL in C# Class Library

I want to reference/include and C++ dll file in to my C# class libary, with a normal C# windows form I just put the dll in the working directory, this does not seem to work for class libraries, how do I get it to find the .dll? 我想引用/包含和C ++ dll文件到我的C#类库中,使用普通的C#Windows窗体,我只是将dll放在工作目录中,这似乎不适用于类库,我如何找到它.dll?

[System.Runtime.InteropServices.DllImportAttribute("ve.dll", EntryPoint=<MethodName>, CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]

That is the call to the methods and the DLL I am including is in the build folder. 那是对方法的调用,而我包含的DLL在构建文件夹中。

You may need a post-build step to ensure that the DLL is copied to your build output folder when you recompile the application. 您可能需要一个后生成步骤,以确保在重新编译应用程序时将DLL复制到您的生成输出文件夹中。 Once that's done, the DLLImport attribute should be able to find the DLL using the short name of the file, without any path information (since it will be local to the executing assembly). 完成此操作后, DLLImport属性应该能够使用文件的短名称查找DLL,而无需任何路径信息(因为它对于执行程序集而言是本地的)。

The c++ dll needs to be either local to the hosting exe (if the hosting exe references the c# dll it will copy that local to itself on build) or in a location in the system PATH environment variable. C ++ dll必须位于托管exe本地(如果托管exe引用c#dll,它将在构建时将该本地复制到自身)或位于系统PATH环境变量中的某个位置。

You can add the c++ dll to the c# project (Add ->Existing Item -> All Files) and set Copy to Output Directory to Copy if newer or Copy Always and set Build Action to None (IIRC the default option) 您可以将c ++ dll添加到c#项目(添加->现有项->所有文件),并将Copy to Output Directory设置为Copy if newerCopy Always ,并将Build Action设置为None (IIRC为默认选项)

If it isn't a managed DLL then you need to use the DllImport attribute. 如果不是托管DLL,则需要使用DllImport属性。 Assuming the DLL has exported functions. 假设DLL具有导出功能。 You can check the exported function names using dumpbin /exports 您可以使用dumpbin / exports检查导出的函数名称

private const string DLLPATH = "MyDLL.dll";

[DllImport(DLLPATH, EntryPoint = "GetDLLStatus")]
public static extern int GetDLLStatus();

[DllImport(DLLPATH, EntryPoint = "SomeOtherFunction")]
public static extern float SomeOtherFunction();

DllImport will first look for the DLL in the application directory then look in your path DllImport将首先在应用程序目录中查找DLL,然后在您的路径中查找

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM