.net 无法添加对 dll 的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3456758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:35:57  来源:igfitidea点击:

A reference to the dll could not be added

.netwcfreference

提问by user20358

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

当我在 C# 应用程序中添加一个 .dll 文件作为参考时,它显示一个错误:

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.

无法添加对“....dll”的引用。请确保该文件可访问并且它是有效的程序集或 COM 组件。

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

ILDissassembler 说没有有效的 CLR 头,所以我尝试使用 regsvr32 注册它,这给了我另一个错误:

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

模块“”已加载,但对 DLLRegisterServer 的调用失败,错误代码为“0x80004005”

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

我在 64 位 Windows 7 机器上使用 VS2010 终极版。可能是什么问题呢?

Thanks for any hints/replies

感谢您的任何提示/回复

采纳答案by user20358

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

我使用dependency walker来检查dll所拥有的内部引用。原来它需要 VB 运行时 msvbvm60.dll 并且由于我的开发箱没有安装它,所以我无法使用 regsvr32 注册它

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

这似乎是我现在最初问题的答案。

回答by Memet Olsen

The following worked for me:

以下对我有用:

Short answer

简答

Run the following via command line (cmd):

通过命令行(cmd)运行以下命令:

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

And a valid dll will be created for you.

并且会为您创建一个有效的 dll。

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.
  • 打开cmd

  • 找到 TlbImp.exe。可能位于 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin。如果找不到它,请转到您的根文件夹(C:\ 或 D:)并运行:

    dir tlbimp.exe /s              //this will locate the file.
    
  • 运行 tlbimp.exe 并将您的 dll 放在它后面。示例:如果您的 dll 是 cvextern.dll。你可以运行:

    TlbImp.exe cvextern.dll
    
  • 在 tlbimp.exe 的同一文件夹中创建了一个新的 dll。您可以将其用作项目中的参考。

回答by Manfred

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.

仅当项目是 .NET 程序集时,才能将 DLL(或 EXE)添加到项目中。如果不是,您将看到此错误消息。

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.

regsvr32 还对 DLL 中的结构和导出函数进行了某些假设。我已经有一段时间没有使用它了,但它与注册 COM 服务器有关,因此某些入口点需要可用。如果 regsvr32 失败,则 DLL 不提供这些入口点,并且 DLL 不包含 COM 组件。

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.

您使用 DLL 的唯一机会是像任何其他非 .NET 二进制文件一样导入它,例如,当您使用某些 Win32 API 时。有一篇旧的 MSDN 杂志文章可能会有所帮助。有关从何处获取文章的信息,请参阅以下更新。

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

2018 年 3 月 12 日更新:MSDN 杂志的链接不再像 2010 年 8 月那样有效。 Jason Clark 的文章标题为“.NET 专栏:在 C# 中使用 P/Invoke 调用 Win32 DLL”。它发表在 2010 年 7 月的 MSDN 杂志上。在“返回器”有文章在这里的那一刻(格式是有限的)。整个MSDN杂志发行2010年7月,请点击这里(仅HCM格式,如何使用HCM文件的说明这里)。

回答by Robert Obergfoll

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

如果您尝试引用 x86 dll,请确保您的编译器设置为 x86...

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.

我遇到了类似的问题......如上所述,尝试使用 OLEDB 从 Visual Studio 2012 中的 C# 代码访问 Excel 文件。

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

我不断收到有关无法访问 Access 库的错误消息,但我知道我已经加载了它。

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.

在调试期间,我突然意识到我正在为 64 位编译但加载了 Office x86。即使我加载了 32 位的 Access 库,它也从未被应用程序使用过......因此,无法访问。

Here is what I was using in C#:

这是我在 C# 中使用的内容:

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

"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

一旦我将编译器切换到 x86,它就起作用了

回答by JeffB

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

我刚刚遇到了这个问题,在所有关于用命令提示符修复它的解释之后,我发现如果你直接将它添加到项目中,你可以简单地在需要的每个页面上包含库

回答by Sashus

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

我在我的项目中导入 WinSCard.dll 时遇到了同样的问题。我处理直接从 dll 导入的问题,如下所示:

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

您可以将其添加到单独的项目中,然后从主项目中添加引用。

回答by Rafal Cypcer

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.

在构建解决方案期间重新启动我的 PC 后,我遇到了这个问题。我的两个引用不见了,所以我不得不手动重建我的两个项目,然后我可以毫无错误地添加引用。

回答by Leon Wolf

You can not add a reference to a nativeDLL. 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

您不能添加对本机DLL的引用。但是,您可以将它们包含在解决方案中(右键单击解决方案,选择“添加现有文件”),但除非您声明类似的内容,否则它们不会被引用

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

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

也许有某种包装DLL,您实际上正在引用它并包含 DLL 导入。

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.

有时,您可能会引用包装 DLL 但仍然无法使您的程序运行,其中错误提示建议您确保文件存在且所有依赖项都可用。

This problem is because the assembly you are trying to add is targeted and compiled only for a x86or x64processor architecture.

此问题是因为您尝试添加的程序集仅针对x86x64处理器体系结构进行定位和编译。

Just try change the Target Platform to x86or x64in Build-> Configuration Manager.

只需尝试在Build-> Configuration Manager中将 Target Platform 更改为x86x64

回答by boateng

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.

我需要在配置管理器中将架构从 x64 更改为 x86,并将我的 32 位 dll(C 语言 - pcProxAPI.dll)复制到创建的新文件夹中。这是下面“Sashus”描述的步骤的基础。

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

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

回答by LoukMouk

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

当我尝试添加我刚刚编码的 dll 时,我遇到了同样的问题。原来我需要设置我的 dll 来自的项目的属性:

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

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

因为我想在其中使用这个 dll 的项目也是这样设置的(将相同的属性设置为/clr)。