[.net] A reference to the dll could not be added

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

This question is related to .net wcf reference

The answer is


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


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).


  1. start cmd.exe and type:
  2. Regsvr32 %dllpath%
  3. "%dllpath%" replace to your dll path

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.


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.


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.


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.


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.

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


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.


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.


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


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).


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.


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.


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).


Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to wcf

Create a asmx web service in C# using visual studio 2013 WCF Exception: Could not find a base address that matches scheme http for the endpoint WCF Service, the type provided as the service attribute values…could not be found WCF error - There was no endpoint listening at How can I pass a username/password in the header to a SOAP WCF Service The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'NTLM' Content Type application/soap+xml; charset=utf-8 was not supported by service The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) maxReceivedMessageSize and maxBufferSize in app.config how to generate a unique token which expires after 24 hours?

Examples related to reference

Method Call Chaining; returning a pointer vs a reference? When to create variables (memory management) Reference to non-static member function must be called Cannot find reference 'xxx' in __init__.py - Python / Pycharm c++ "Incomplete type not allowed" error accessing class reference information (Circular dependency with forward declaration) C++ initial value of reference to non-const must be an lvalue Dependent DLL is not getting copied to the build output folder in Visual Studio How to write to error log file in PHP How to reference Microsoft.Office.Interop.Excel dll? Linker Error C++ "undefined reference "