简体   繁体   English

在 Windows / C# 中卸载 DLL

[英]Unloading DLL in Windows / C#

I have a piece of hardware, for which the maker provides a DLL and an example Visual Studio project.我有一块硬件,制造商为此提供了一个 DLL 和一个示例 Visual Studio 项目。

I wrote a tool based on this project, using the newest version of the DLL, and it runs fine.我基于这个项目编写了一个工具,使用最新版本的 DLL,它运行良好。

Now, there are also other (older) tools, using other versions of the DLL.现在,还有其他(较旧的)工具,使用其他版本的 DLL。 Those tools run fine as well, on their own.这些工具本身也运行良好。

The problem occours when I run both tools (after another, not simoultaneously!) without rebooting Windows in between.当我运行这两个工具(一个接一个,而不是同时运行!)而没有在两者之间重新启动 Windows 时,就会出现问题。

Then I get an "Version mismatch" error.然后我收到“版本不匹配”错误。 I suspect that Windows still keeps the DLL loaded, although the program that ran first terminated correctly.我怀疑 Windows 仍然保持 DLL 加载,尽管首先运行的程序正确终止。

Since I do not have access to the source code of the "other tools", is there any way to由于我无法访问“其他工具”的源代码,有什么办法可以

  1. enforce reloading of the DLL with the correct version when my program starts and;在我的程序启动时强制使用正确版本的 DLL 重新加载;
  2. Force Windows to unload the DLL when my program exits当我的程序退出时强制 Windows 卸载 DLL

so there won't be any problems?所以不会有任何问题吗?

Since you don't have the source code of these other tools, you will have to create a manifest file for each application that you want to use that's using a version of this dll.由于您没有这些其他工具的源代码,因此您必须为要使用的每个使用此 dll 版本的应用程序创建一个清单文件。 This is going to be a file with the same name as the executable, with the ".manifest" extension.这将是一个与可执行文件同名的文件,扩展名为“.manifest”。 You can find more about application manifests here:您可以在此处找到有关应用程序清单的更多信息:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa374191%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa374191%28v=vs.85%29.aspx

Once you have the manifest file in place, you would specify the exact dll it needs to load with something like:一旦你有了清单文件,你就可以指定它需要加载的确切 dll,如下所示:

  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" 
                        name="Proseware.Research.SampleAssembly" 
                        version="6.0.0.0" 
                        processorArchitecture="X86" 
                        publicKeyToken="0000000000000000" 
                        language="*"
      />
    </dependentAssembly>
  </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM