A reference to the dll could not be added

95

When I add a .dll file as a reference in C# application it shows an error :

A reference to the "....dll" could not be added.Please make sure that the file is accessible and that it is a valid assembly or COM component.

ILDissassembler says there is no valid CLR header so I try to register it using regsvr32 and that gives me another error:

The module "" was loaded but the call to DLLRegisterServer failed with error code '0x80004005'

I am using VS2010 ultimate version on a 64bit Windows 7 machine. What could be the problem?

Thanks for any hints/replies

.net
wcf
reference
asked on Stack Overflow Aug 11, 2010 by user20358 • edited Dec 28, 2013 by osvein

17 Answers

53

The following worked for me:

Short answer

Run the following via command line (cmd):

TlbImp.exe cvextern.dll        //where cvextern.dll is your dll you want to fix.

And a valid dll will be created for you.

Longer answer

  • Open cmd

  • Find TlbImp.exe. Probably located in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin. If you can't find it go to your root folder (C:\ or D:) and run:

    dir tlbimp.exe /s              //this will locate the file.
    
  • Run tlbimp.exe and put your dll behind it. Example: If your dll is cvextern.dll. You can run:

    TlbImp.exe cvextern.dll
    
  • A new dll has been created in the same folder of tlbimp.exe. You can use that as reference in your project.
answered on Stack Overflow Sep 28, 2012 by Memet Olsen • edited May 13, 2017 by Manfred
20

You can add a DLL (or EXE) to a project only if it is a .NET assembly. If it's not you will see this error message.

regsvr32 also makes certain assumptions about the structure and exported function in the DLL. It has been a while since I used it but it has to do with registering COM servers so certain entry points need to be available. If regsvr32 fails the DLL doesn't provide those entry points and the DLL does not contain a COM component.

You only chance for using the DLL is to import it like any other non-.NET binary, e.g. when you use certain Win32 APIs. There is an old MSDN Magazine Article that might be helpful. See the following update for info where to get the article.

Update 12 March 2018: The link to the MSDN Magazine no longer works as it used to in August 2010. The article by Jason Clark is titled ".NET Column: Calling Win32 DLLs in C# with P/Invoke". It was published in the July 2010 issue of MSDN Magazine. The "Wayback Machine" has the article here at the moment (formatting is limited). The entire MSDN Magazine issue July 2010 is available here (HCM format only, instructions for how to use HCM files here).

answered on Stack Overflow Aug 11, 2010 by Manfred • edited Mar 12, 2018 by Manfred
12

I used dependency walker to check out the internal references the dll was having. Turns out it was in need of the VB runtime msvbvm60.dll and since my dev box doesnt have that installed I was unable to register it using regsvr32

That seems to be the answer to my original question for now.

answered on Stack Overflow Aug 11, 2010 by user20358
7

Make sure your compiler is set to x86 if you are trying to reference an x86 dll...

I was having similar issues... as mentioned above, trying to use OLEDB to access an Excel file from my C# code in Visual Studio 2012.

I kept getting errors about the Access library not being accessible but I knew that I had loaded it.

During Debug, it dawned on me that I am compiling for 64 bit but have Office x86 loaded. Even though I loaded the Access library for 32 bit, it was never being used by the app... and was, therefore, not accessible.

Here is what I was using in C#:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFilePath + ";Extended Properties='Excel 12.0 Xml;HDR=Yes'";

...I was getting an error

As soon as I switched the compiler to x86 it worked

answered on Stack Overflow Jan 27, 2014 by Robert Obergfoll • edited Jan 7, 2015 by Olivier Jacot-Descombes
2

I just ran into that issue and after all the explanations about fixing it with command prompt I found that if you add it directly to the project you can then simply include the library on each page that it's needed

answered on Stack Overflow Mar 3, 2015 by JeffB
2

I have the same problem with importing WinSCard.dll in my project. I deal with that importing directly from dll like this:

[DllImport("winscard.dll")]
public static extern int SCardEstablishContext(int dwScope, int pvReserved1, int pvReserved2, ref int phContext);

[DllImport("winscard.dll")]
public static extern int SCardReleaseContext(int phContext);

You could add this to separate project and then add a reference from your main project.

answered on Stack Overflow Apr 12, 2016 by Sashus
1

I had this issue after my PC has been restarted during building the solution. My two references gone, so I had to rebuild my two projects manually and then I could add references without error.

answered on Stack Overflow Oct 20, 2016 by Rafal Cypcer
1

You can not add a reference to a native DLL. However 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();

Maybe there's some kind of wrapper DLL, which you are actually referencing and which contains the DLL imports.

Sometimes, You may reference the wrapper DLL but still can not make your program running, where error prompt suggests you to ensure the file exists and all dependencies are available.

This problem is because the assembly you are trying to add is targeted and compiled only for a x86 or x64 processor architecture.

Just try change the Target Platform to x86 or x64 in Build -> Configuration Manager.

answered on Stack Overflow Jul 21, 2017 by Leon Wolf • edited Jul 21, 2017 by Leon Wolf
1

I needed to change architecture to x86 from x64 in configuration manager and copy my 32 bit dll (C language - pcProxAPI.dll) into new folder this created.. This is on top of the steps described by "Sashus" below.

C:\Projects..\bin\x86\Debug

answered on Stack Overflow Sep 26, 2017 by boateng
1

I had the same problem when I tried to add a dll I just coded in C++ to my new C# project. Turned out I needed to set properties of the C++ project my dll is from:

  • Configuration Properties\General\Common Language Runtime Support: /clr
  • Configuration Properties\C/C++\General\Common Language RunTime Support: /clr

Because the C# project in which I wanted to use this dll was also set like that (had the same properties set to /clr).

answered on Stack Overflow Jul 30, 2018 by LoukMouk • edited Jun 11, 2020 by LoukMouk
0

I faced a similar problem. I was trying to add the reference of a .net 2.0 dll to a .Net 1.1 project. When I tried adding a previous version of the .dll which was complied in .Net 1.1. it worked for me.

answered on Stack Overflow Jul 26, 2013 by user2623151
0

For anyone else looking for help on this matter, or experiencing a FileNotFoundException or a FirstChanceException, check out my answer here:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll - windows phone

In general you must be absolutely certain that you are meeting all of the requirements for making the reference - I know it's the obvious answer, but you're probably overlooking a relatively simple requirement.

answered on Stack Overflow Jan 15, 2015 by JHaps • edited May 23, 2017 by Community
0

I had this error while writing a Windows Service. I was running Visual Studio as Administrator so that my post build commands would automatically install my service. I noticed that when I closed everything and ran VS normally (Not as Administrator) it let me add the references just fine with no error.

Hope this solution works for you.

answered on Stack Overflow Dec 16, 2016 by F├╝temire
0

Normally in Visual Studio 2015 you should create the dll project as a C++ -> CLR project from Visual Studio's templates, but you can technically enable it after the fact:

The critical property is called Common Language Runtime Support set in your project's configuration. It's found under Configuration Properties > General > Common Language Runtime Support.

When doing this, VS will probably not update the 'Target .NET Framework' option (like it should). You can manually add this by unloading your project, editing the your_project.xxproj file, and adding/updating the Target .NET framework Version XML tag.

For a sample, I suggest creating a new solution as a C++ CLR project and examining the XML there, perhaps even diffing it to make sure there's nothing very important that's out of the ordinary.

answered on Stack Overflow Jan 17, 2017 by Aaron Hull • edited Jan 20, 2017 by Aaron Hull
0

My answer is a bit late, but as a quick test, make sure you are using the latest version of libraries.

In my case after updating a nuget library that was referencing another library causing the problem the problem disappeared.

answered on Stack Overflow Mar 17, 2018 by Martin Staufcik
0

You can add .dll file manually. For example if you want to add a dll file in your WPF application, and you are unable to refer it in your project

(Getting error :A reference to the "....dll" could not be added.Please make sure that the file is accessible and that it is a valid assembly or COM component) ,

then COPY that dll file and PASTE in the INSTALLER PROJECT (in application folder).

answered on Stack Overflow Mar 31, 2020 by ARUN JOHN
0
  1. start cmd.exe and type:
  2. Regsvr32 %dllpath%
  3. "%dllpath%" replace to your dll path
answered on Stack Overflow Apr 22, 2020 by charles chen

User contributions licensed under CC BY-SA 3.0