简体   繁体   English

通过在 Visual Studio 中调试期间忽略的注册表项链接到 DLL

[英]Linking to DLL via registry key ignored during debugging in Visual Studio

I'm working in VS 2019 on a C++ program in which I want to link into a bunch of 3rd party dlls, which are located in a folder separate from my executable.我正在 VS 2019 中使用 C++ 程序,我想在该程序中链接到一组 3rd 方 dll,这些 dll 位于与我的可执行文件不同的文件夹中。 I don't want to copy all the dlls into the executable folder and have found the following solution .我不想将所有 dll 复制到可执行文件夹中,并找到了以下解决方案

Just like in the solution, I create a registry entry to define the keys with the information about the executable and the required path information to link to the DLLs:就像在解决方案中一样,我创建了一个注册表项来定义键,其中包含有关可执行文件的信息以及链接到 DLL 所需的路径信息:

  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MyExe.exe HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MyExe.exe
    • %Path2MyExe%\MyExe.exe (default key) %Path2MyExe%\MyExe.exe(默认键)
    • Path = %Path2DLLs%路径 = %Path2DLLs%

The compilation from within VS works fine and when I start the generated executables from outside VS by (a) double clicking or (b) by using the command line, the 3rd party DLLs are found and the program is executed as expected.从 VS 内部编译工作正常,当我通过(a)双击或(b)使用命令行从 VS 外部启动生成的可执行文件时,找到了第 3 方 DLL 并按预期执行程序。

However , when I invoke the debugger from within Visual Studio (c), execution is halted right away complaining that DLLs cannot be found.但是,当我从 Visual Studio (c) 中调用调试器时,执行立即停止并抱怨找不到 DLL。

I have checked the present working directory from within the code using _getcwd and for all options (ac) it indicates the same directory.我已经使用_getcwd从代码中检查了当前的工作目录,并且对于所有选项 (ac),它表示相同的目录。

So the question is: why does option (c) - debugging from within VS - not work?!所以问题是:为什么选项 (c) - 从 VS 中调试 - 不起作用?!

Cheers, Sebastian干杯,塞巴斯蒂安

It's probably because the executable that's launched when debugging is a vshost executable, not the generated executable for the project, and so it needs a separate entry in the registry.这可能是因为调试时启动的可执行文件是 vshost 可执行文件,而不是项目生成的可执行文件,因此它需要在注册表中单独条目。

If you want to stick with this kind of a build (highly not recommended), the easiest way to debug is to disable the hosting process:如果您想坚持使用这种构建(强烈不推荐),最简单的调试方法是禁用托管进程:

在此处输入图像描述

Following the suggestion by drescherjm I tried to prepend the DLL directory to the PATH variable in "Configuration Properties -> Debugging -> Environment" following this solution .按照 drescherjm 的建议,我尝试在此解决方案之后将 DLL 目录添加到“配置属性 - > 调试 - > 环境”中的 PATH 变量中。

However, this did not work for me.但是,这对我不起作用。 I also added the path directly in the system environment dialogue without success.我也直接在系统环境对话中添加了路径,没有成功。

What did work in the end is setting the working directory under "Configuration Properties -> Debugging -> Working Directory" to point to the DLL folder.最后的工作是将“配置属性->调试->工作目录”下的工作目录设置为指向 DLL 文件夹。 Now all three calling routes (ac) work as expected.现在所有三个调用路由 (ac) 都按预期工作。

Thanks to everyone for taking the time to help me out on this.感谢大家花时间帮助我解决这个问题。

Sebastian塞巴斯蒂安

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

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