简体   繁体   中英

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?

[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.

You may need a post-build step to ensure that the DLL is copied to your build output folder when you recompile the application. 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).

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.

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)

If it isn't a managed DLL then you need to use the DllImport attribute. Assuming the DLL has exported functions. You can check the exported function names using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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