2

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

user2577497
  • 497
  • 9
  • 17
  • Add appropriate tags, Winforms or WPF? – Chibueze Opata Jul 17 '13 at 22:21
  • Does this answer your question? [How can I specify a \[DllImport\] path at runtime?](https://stackoverflow.com/questions/8836093/how-can-i-specify-a-dllimport-path-at-runtime) – GSerg Jan 07 '22 at 12:11

2 Answers2

1

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")]
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
0

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

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

Pogrindis
  • 7,755
  • 5
  • 31
  • 44