如何使用探测配置元素实现 Windows 并行程序集

问题描述

我正在尝试使用清单文件处理 Windows Side-by-side Assemblies 系统,以便能够将 dll 放在子文件夹中,同时继续链接到它们(不使用“运行时”加载依赖项)

我明白我必须在应用程序的配置文件中使用 probing element。但似乎被忽略了,或者我在某处做错了。

我在下面描述了我所做的一个完整的简单示例。

我有两个源文件HelloWorld.cpp 是 dll 的代码main.cpp 是主应用程序的代码

示例基础

#include <iostream>

__declspec(dllexport) void HelloWorld()
{
    std::cout << "Hello World!" << std::endl;
}
__declspec(dllimport) void HelloWorld();

int main()
{
    HelloWorld();
    return 0;
}

以下是手动编译的命令:

cl /c /EHsc HelloWorld.cpp
link /dll HelloWorld.cpp
cl /c main.cpp
link main.obj HelloWorld.lib
main.exe

输出Hello World!:它有效

现在我将 dll 移动到子文件bin

mkdir bin
move HelloWorld.dll bin\HelloWorld.dll
main.exe

没有输出,但我没想到它有效。

使用清单文件实现并行程序集

现在,为了实现 Windows Side-by-side Assemblies 系统,我添加了以下 3 个文件以获得此文件夹结构:

.\main.exe
.\main.exe.manifest
.\main.exe.config
.\bin\HelloWorld.dll
.\bin\HelloWorld.dll.manifest

main.exe.manifest

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level='asInvoker' uiAccess='false' />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type='win32' name='HelloWorld.dll' version='1.2.3.4' processorArchitecture='amd64' />
    </dependentAssembly>
  </dependency>
</assembly>

main.exe.config

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath="bin"/>
        </assemblyBinding>
    </runtime>
</configuration>  

HelloWorld.dll.manifest

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level='asInvoker' uiAccess='false' />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <assemblyIdentity name="HelloWorld.dll" version="1.2.3.4" processorArchitecture="amd64" type="win32"/>
  <file name="HelloWorld.dll"/>
</assembly>
mkdir bin
move HelloWorld.dll bin\HelloWorld.dll
main.exe

我没有输出:我希望它可以工作!

试着理解

我使用 sxstrace 来理解附加内容

sxstrace Trace -logfile:sxstrace.etl -nostop
main.exe
sxstrace Stoptrace
sxstrace Parse -logfile:sxstrace.etl -outfile:sxstrace.txt

日志如下:

=================
Begin Activation Context Generation.
Input Parameter:
    Flags = 0
    ProcessorArchitecture = AMD64
    CultureFallBacks = fr-FR;fr;en-US;en
    ManifestPath = C:\Users\azias\Documents\dev\dll\main.exe.Manifest
    AssemblyDirectory = C:\Users\azias\Documents\dev\dll\
    Application Config File = C:\Users\azias\Documents\dev\dll\main.exe.Config
-----------------
INFO: Parsing Application Config File C:\Users\azias\Documents\dev\dll\main.exe.Config.
INFO: Parsing Manifest File C:\Users\azias\Documents\dev\dll\main.exe.Manifest.
    INFO: Manifest DeFinition Identity is (null).
    INFO: Reference: HelloWorld.dll,processorArchitecture="amd64",type="win32",version="1.2.3.4"
INFO: Resolving reference HelloWorld.dll,version="1.2.3.4".
    INFO: Resolving reference for ProcessorArchitecture amd64.
        INFO: Resolving reference for culture Neutral.
            INFO: Applying Binding Policy.
                INFO: No binding policy redirect found.
            INFO: Begin assembly probing.
                INFO: Did not find the assembly in WinSxS.
                INFO: Attempt to probe manifest at C:\Users\azias\Documents\dev\dll\HelloWorld.dll.DLL.
                INFO: Attempt to probe manifest at C:\Users\azias\Documents\dev\dll\HelloWorld.dll.MANIFEST.
                INFO: Attempt to probe manifest at C:\Users\azias\Documents\dev\dll\HelloWorld.dll\HelloWorld.dll.DLL.
                INFO: Attempt to probe manifest at C:\Users\azias\Documents\dev\dll\HelloWorld.dll\HelloWorld.dll.MANIFEST.
                INFO: Did not find manifest for culture Neutral.
            INFO: End assembly probing.
    ERROR: Cannot resolve reference HelloWorld.dll,version="1.2.3.4".
ERROR: Activation Context generation Failed.
End Activation Context Generation.

我们可以看到它已经解析了 main.exe.config 但根本没有在 bin 文件夹中搜索

我的问题

所以我的问题是:我错在哪里以及如何使它起作用?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)