Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot add a reference .NET

I have a DLL which I would like to add as a reference to my project, but everytime I try to do it a dialog pops up telling me:

The reference could not be added. Please make sure that the file is accesible, and that it is a valid assembly or COM component.

I have researched a little and found out that error is because the assembly is unmanaged by .NET and that I should use DLLImport Attribute, however, I have the same exact solution in another computer, and everythig just works fine.

The difference is that the new computer I'm trying to add the reference is x64 and the old one is x86.

I have both the x64 and x86 DLL's, and can't add any. Why is this happening?

like image 319
Daniel Avatar asked Mar 08 '12 15:03

Daniel


People also ask

How do I enable references in Visual Studio?

You can also right-click the project node and select Add > Project Reference. If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference.


2 Answers

You can not add unmanaged DLLs as references in Visual Studio, regardless of the 32/64 "bittyness". And I doubt that it worked on your x86 machine.

There's a difference between "normal" DLLs and COM-DLLs.

You can add a reference to a COM-DLL after it was registered with the system (actually you reference the exposed COM object and a reference to the DLL is added automatically). This can be done on the "COM"-Tab of the "Add reference" dialog (here you have to make sure that your project is built as a x86 target in most cases).

"normal" DLLs can - as I said - not be added at all. You can include them in the solution (right click solution, select "Add existing file"), but they will not be referenced unless you declare something like

[DllImport("...")]
public static extern void MyFunction();

I suspect that in your other solution, there's some kind of wrapper DLL, which you are actually referencing and which contains the DLL imports.

like image 200
Thorsten Dittmar Avatar answered Sep 28 '22 06:09

Thorsten Dittmar


There are multiple answeres to your question:

  1. you may face this problem because the assembly you are trying to add is targeted and compiled for a x86 processor architecture. Just try change the Target Platform from x64 to x86 and even if that doent work, try change it to AnyCPU. AnyCPU Platform target makes your application executable on both types of architecture because it is architecture-free.

  2. If the assembly happens to be a DLL, and it cannot be added as a reference, then it is not a COM as well as .NET assembly. It will be a native assembly like others (for example, shell32.dll, user32.dl etc). You have to use them via DllImport attribute, but you must first check the documentation of that dll to get the list of functions implemented in that dll.

like image 21
Uday0119 Avatar answered Sep 28 '22 05:09

Uday0119