gpt4 book ai didi

c++ - 如何在 C++ 中的同一个 dll 中获取 dll 位置路径?

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:19 28 4
gpt4 key购买 nike

假设我有一个名为 MyDll.dll

的 dll
  • d:\MyWorks\MyDll.dll【是directshow dll】

  • 我想从 MyDll 代码中获取它的位置路径。

我为此使用了 boost: 文件系统

string path = "";
boost::filesystem::path full_path( boost::filesystem::current_path() );
path = full_path.string();

但这给了我它的执行路径,它是 C:\Windows\system32,而不是它的位置路径,它是 d:\MyWorks\MyDll.dll

如何在同一 dll 中获取 dll 的位置?

更新:通过获取模块:

TCHAR path[2048];
GetModuleFileName( NULL, path, 2048 );
ostringstream file;

file << path ;

string const pathString =file.str();

cout << "Path: " << pathString << endl;

只给我十六进制字符串:0049EA95....

最佳答案

在你的 DllMain 你收到一个HINSTANCE范围;那实际上是 HMODULE你的 dll,你可以使用 GetModuleFileName 检索您的 dll 的完全限定路径。要只获取包含它的目录,您只需删除文件名(您可以使用 boost::filesystem 、shell 路径函数以及 strrchr 来做到这一点)。

关于c++ - 如何在 C++ 中的同一个 dll 中获取 dll 位置路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8605644/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com