在 C++ windows 应用程序中引用本机 C++ DLL 仅在添加为引用时有效

Referencing native C++ DLL in C++ windows app only works when added as reference

Visual Studio 2013/2015:

我有一个空白的 C++ Windows 商店应用程序和同一解决方案中可能最简单的 DLL,仅导出 void foo() {}

例如在Win32 控制台应用程序按预期工作,包含 DLL header 并将 *.lib 文件添加到链接器依赖项中。

然而,尝试在 Windows Store App 中使用此 DLL 在程序启动时出现异常,消息框 Unhandled exception at 0x77F0E052 (ntdll.dll) in App5.exe: 0xC0000135: Unable to Locate DLL. 和调试控制台中的以下输出:

'App5.exe' (Win32): Loaded 'C:\Users\naitsirhc\Documents\Visual Studio 2015\Projects\App5\Debug\App5\AppX\App5.exe'. Symbols loaded.
'App5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'App5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'App5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
First-chance exception at 0x77F0E052 (ntdll.dll) in App5.exe: 0xC0000135: Unable to Locate DLL.
Unhandled exception at 0x77F0E052 (ntdll.dll) in App5.exe: 0xC0000135: Unable to Locate DLL.
The program '[4168] App5.exe' has exited with code 0 (0x0).

现在,如果我简单地将 DLL 添加为对 Windows Store 应用程序的引用(通过属性 -> 通用属性 -> 引用),它就可以工作了。

我比较了编译器和链接器的命令行,它们是相同的。

我错过了什么?网络上有许多示例,但除了一个之外,所有示例都只是将 *.lib 文件添加到链接器的依赖项中,这也是我更愿意做的。

您需要将 DLL 作为内容包含在 appx 包中。将其添加为引用会自动执行此操作,但您也可以将其显式设置为内容。将 DLL 添加到项目中,使其显示在解决方案资源管理器中。在解决方案资源管理器和 select 属性中右键单击 DLL。在属性窗格中将 DLL 的内容设置为 "True":

这是一个部署设置,而不是构建或 link 设置,因此它不会影响编译器或 linker 的参数。项目文件将更新为包含如下内容:

<ItemGroup>
    <None Include="Foo.dll">
        <DeploymentContent>true</DeploymentContent>
    </None>
</ItemGroup>

这将导致 DLL 包含在部署 appx 包中。