Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 315
user20358 Avatar asked Aug 11 '10 08:08

user20358


People also ask

How do I add a reference to a DLL in Visual Studio C ++?

On the Project menu, click Add References. In Visual C++, click References on the Project menu, and then click Add New Reference. In the Add References dialog box, click the tab that corresponds with the category that you want to add a reference to. In Visual C++, click the Browse tab in the Add References dialog box.

How do I add a DLL reference in Visual Studio NET core?

One of the things I did miss was a clear direction on how to reference a . NET dll directly (and of course still enjoy the benefits of intellisense), basically the same thing as doing Project > Add Reference > Browse in Visual Studio. And that's it!

How do I add a reference to System Web DLL?

1 right click on References in Solution Explorer and press add reference... 2 choose the browse tab and go to C:\Windows\assembly\GAC_32\System. Web\System.


11 Answers

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.
like image 132
Memet Olsen Avatar answered Oct 14 '22 14:10

Memet Olsen


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

like image 39
Manfred Avatar answered Oct 14 '22 15:10

Manfred


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.

like image 29
user20358 Avatar answered Oct 14 '22 14:10

user20358


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

like image 20
Robert Obergfoll Avatar answered Oct 14 '22 16:10

Robert Obergfoll


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

like image 39
JeffB Avatar answered Oct 14 '22 16:10

JeffB


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.

like image 20
Sashus Avatar answered Oct 14 '22 15:10

Sashus


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.

like image 35
Rafal Cypcer Avatar answered Oct 14 '22 14:10

Rafal Cypcer


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.

like image 42
Aaron Hull Avatar answered Oct 14 '22 14:10

Aaron Hull


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.

like image 35
Leon Wolf Avatar answered Oct 14 '22 16:10

Leon Wolf


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

like image 33
boateng Avatar answered Oct 14 '22 14:10

boateng


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

like image 24
LoukMouk Avatar answered Oct 14 '22 14:10

LoukMouk