简体   繁体   中英

DLLImport path C#

I would like to import my DLL inside my C# class but instead of this:

[DllImport(@"C:\Users\user\Documents\Visual Studio 2010\Projects\KeyDll\Debug\DLLWrap.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?DivSet@MyCall@MyFunc@@SAPADPAD@Z")]

I would like to locate the path within the project like so:

[DllImport(@"...\Debug\DLLWrap.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?DivSet@MyCall@MyFunc@@SAPADPAD@Z")]

So that it looks in the local folder the solution is in.

Any suggestions??

Regards

You can simply use relative urls like:

[DllImport("DLLWrap.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?DivSet@MyCall@MyFunc@@SAPADPAD@Z")]

Your application will search working directory (your local folder) for the file by default.

In Winforms, you can always ensure it uses your Working Directory by

[DllImport(Application.StartupPath + "\\DLLWrap.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?DivSet@MyCall@MyFunc@@SAPADPAD@Z")]

Edit the properties of the DLL to be 'Content' and 'Copy Always'

Reference the DLL in your P/Invoke statements as @"DLLWrap.dll"

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