john
john

Reputation: 1319

Relative Path to DLL in Platform Invoke Statement

I am using VS 2008 to develop an application that uses a .dll with P/Invoke. I can successfully use the dll when I use an absolute path. For example, this DOES work:

[DllImport("C:\\myDLL.dll")] internal static extern bool isReady();

this DOES NOT work:

[DllImport("myDLL.dll")] internal static extern bool isReady();

I have tried adding a reference folder and adding the dll to the project folder but neither work. I need to deploy this application on other computers and need to make sure this .dll is included. Thanks for the help!


I forgot to mention when I try to do that I get this error:

Microsoft Visual Studio A reference to 'C:\Users\dlugokja\Documents\Visual Studio 2008\Projects\DinamapN.sln(1)\DinamapN\DinamapN\DinaWin.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component. OK

Upvotes: 2

Views: 2767

Answers (2)

user7116
user7116

Reputation: 64088

The solution we use at work is the following:

  1. Add the DLL to your project
  2. Edit the properties of the DLL to be 'Content' and 'Copy Always'
  3. Reference the DLL in your P/Invoke statements as @"mydll.dll"

Upvotes: 4

Jacob Adams
Jacob Adams

Reputation: 3994

Why not just right click project file, "Add reference" and browse to the dll?

Upvotes: -2

Related Questions