102

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

osvein
  • 625
  • 2
  • 10
  • 31
user20358
  • 14,182
  • 36
  • 114
  • 186

18 Answers18

54

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.
Manfred
  • 5,320
  • 3
  • 35
  • 29
Memet Olsen
  • 4,578
  • 5
  • 40
  • 50
  • 5
    Another way to access TlbImp.exe is to open up a Visual Studio command prompt. – Scott Aug 21 '13 at 14:03
  • 79
    @Memet Afer trying this I got, `TlbImp : error TI0000 : The input file 'c:\Program Files\Microsoft SDKs\Windows\ v7.0A\bin\MyLibrary.dll' is not a valid type library.` Any Suggestions ? – Dineshkumar Feb 21 '14 at 13:24
  • @user34567890 I was stuck there for a couple of days now. I am trying to wrap the .dll in a VB6.0 wrapper and use it in .Net. But still no luck and I have idea if I am on the right track... – bonCodigo Feb 25 '15 at 08:39
  • guess what no such file in my win 8.1 system – Furkan Gözükara Apr 02 '15 at 23:25
  • 1
    The input file C:\Windows\SYSTEM32\SqlServerSpatial.dll' is not a valid type library – Christine Oct 27 '15 at 22:06
  • 7
    Having same error as stated by Dineshkumar Ponnusamy. Please help me out – deathrace Nov 24 '15 at 11:29
  • 1
    When would you need to use TLBMP? Normally in VS2015 if you just add the reference to the COM DLL from the references window it takes care of all this for you. – StayOnTarget Jun 28 '17 at 18:56
  • @DaveInCaz Not always. I just ran into this issue with a C-built COM API and this answer resolved my problem. – Maximilian Burszley Feb 20 '18 at 19:51
  • #theincorrigible when you added that C-built DLL as a reference through the IDE, what did it do? How did it go wrong? – StayOnTarget Feb 21 '18 at 17:35
  • 1
    It's so strange. On Windows 7, I could easily simply add the original DLL, but now I need to do it as you suggested. However, your solution works for me. Thank you. – tmighty May 03 '18 at 01:25
  • TlbImp : error TI1002 : The input file 'D:\oleaut32.dll' is not a valid type library. – JeeShen Lee Jun 28 '22 at 02:02
25

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

Manfred
  • 5,320
  • 3
  • 35
  • 29
  • 1
    Is there a way to make sure the compiler puts a .dll in the output folder even if it's not a reference? – Kyle Delaney May 11 '17 at 21:14
  • 2
    @KyleDelaney I don't think the compiler can do that. I'd consider putting a copy or xcopy command into the pre-build/post-build event of the project/solution. It's manual, though, but generally references don't change too often and a proper test suite should catch the cases where you forgot to update the commands executed in the pre-build/post-build event. – Manfred May 13 '17 at 23:30
  • Thanks for the reply! – Kyle Delaney May 14 '17 at 18:21
  • Link to the article is dead. – Roald Mar 07 '18 at 15:06
  • @Roald Thank you. Yes, the link is indeed broken. Microsoft only provides HCM format of the entire MSDN Magazine issue at a different URL. The Wayback Machine also has a snapshot of just the article in HTML format with some simplified appearance. I've updated the answer to reflect this and I also removed the broken link from my answer. Hope this helps. – Manfred Mar 12 '18 at 02:32
14

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.

user20358
  • 14,182
  • 36
  • 114
  • 186
  • In my case, I had to delete the obj folder for the project I was trying to reference and then I rebuilt the project. – Tolu May 08 '15 at 10:40
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

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
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

JeffB
  • 17
  • 1
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.

Sashus
  • 411
  • 7
  • 8
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.

Rafal Cypcer
  • 557
  • 1
  • 5
  • 17
1

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.

Aaron Hull
  • 422
  • 4
  • 16
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.

Leon Wolf
  • 119
  • 1
  • 4
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

boateng
  • 910
  • 11
  • 21
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).

LoukMouk
  • 503
  • 9
  • 29
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.

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.

Community
  • 1
  • 1
JHaps
  • 41
  • 3
  • Look into differences between framework versions in relation to the reference - a lot of the time using a newer framework version should not make a difference. – JHaps Jan 15 '15 at 15:28
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.

Fütemire
  • 1,705
  • 1
  • 26
  • 21
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.

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
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).

0
  1. start cmd.exe and type:
  2. Regsvr32 %dllpath%
  3. "%dllpath%" replace to your dll path
charles chen
  • 82
  • 1
  • 9
0

I had the same problem when I tried to add my own c++ dll to a c# (4.7.2) project

c++ project was compiled as x86 (win32) c# was complied as x86

the solution was to compile the c++ as CLR

Amir Dar
  • 35
  • 5