在C ++ Windows应用程序中引用本机C ++ DLL仅在作为参考添加时才起作用 [英] Referencing native C++ DLL in C++ windows app only works when added as reference

查看:249
本文介绍了在C ++ Windows应用程序中引用本机C ++ DLL仅在作为参考添加时才起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2013/2015:

Visual Studio 2013/2015:

我有一个空白的C ++ Windows Store App,同样的解决方案中最简单的DLL只导出 void foo(){}

I have a blank C++ Windows Store App and the simplest possible DLL in the same solution only exporting void foo() {}.

一个Win32控制台应用程序的工作原理包括DLL的头文件和* .lib文件添加到链接器依赖。

Using this DLL in e.g. a Win32 console application works as intended with the DLL's header included and the *.lib file added to the linkers dependencies.

但是尝试在Windows Store应用程序中使用此DLL在程序启动时给我一个异常,消息框 App5.exe中0x77F0E052(ntdll.dll)处理的未处理的异常:0xC0000135:无法找到DLL。和以下输出调试控制台:

However trying to use this DLL in the Windows Store App gives me an exception at program start, the message box Unhandled exception at 0x77F0E052 (ntdll.dll) in App5.exe: 0xC0000135: Unable to Locate DLL. and the following output in the Debug console:

'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 App的引用添加(通过属性 - > Common属性 - >引用)然后它的工作。

Now, if I simply add the DLL as reference to the Windows Store App (via Properties -> Common Properties -> References) then it works.

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

I compared the command lines for the compiler and the linker and they were identical.

我缺少什么?网络上有许多例子,但除了一个简单的添加* .lib文件到链接器的依赖项之外,所有这些都是我想做的。

What am I missing? There are a number of examples on the web but all but one simply added the *.lib file to the linker's dependencies and that is what I would prefer to do too.

推荐答案

您需要将DLL作为内容包含在appxpackage中。将其添加为引用将自动执行,但也可以将其显式设置为内容。将DLL添加到项目中,以显示在解决方案资源管理器中。在解决方案资源管理器中点击DLL,然后选择属性。在属性窗格中,将DLL的内容设置为True:

You need to include the DLL as content in the appxpackage. Adding it as a reference will do so automatically, but you can also explicitly set it as content. Add the DLL to the project so it shows up in Solution Explorer. Write click on the DLL in solution explorer and select properties. In the properties pane set the DLL's Content to "True":

这是部署设置,而不是构建或链接设置,因此它不会影响编译器或连接。项目文件将被更新为包含以下内容:

This is a deployment setting not a build or link setting, so it won't affect the arguments to the compiler or the linker. The project file will be updated to include something like:

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

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

And that will cause the DLL to be included in the deployment appxpackage.

这篇关于在C ++ Windows应用程序中引用本机C ++ DLL仅在作为参考添加时才起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆