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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8605644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:43:23  来源:igfitidea点击:

How can I get dll location path inside the same dll in C++?

c++windowsvisual-c++dllboost

提问by Novalis

Suppose that I have a dll called it MyDll.dll

假设我有一个名为MyDll.dll的 dll

  • It is in the d:\MyWorks\MyDll.dll[it is directshow dll]

  • I want to get path of its location from inside the MyDll code.

  • 它在d:\MyWorks\MyDll.dll[它是 directshow dll]

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

I used boost for this:FileSystem

我用升压此:文件系统

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

But this give me its execution path, which is C:\Windows\system32, and not its location path which is d:\MyWorks\MyDll.dll.

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

How can I get a dll's location inside the same dll?

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

Update: By Get Module:

更新:通过获取模块:

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

  file << path ;

  string const pathString =file.str();

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

Gives me just hex-like string : 0049EA95....

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

回答by Matteo Italia

In your DllMainyou receive an HINSTANCEparameter; that is actually the HMODULEof your dll, that you can use with GetModuleFileNameto retrieve the fully-qualified path of your dll. To get just the directory that contains it you just have to remove the file name (you can do that with boost::filesystem, with shell path functions as well as just with a strrchr).

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

回答by J. Calleja

You can use GetModuleFileNamein order to get the full path of a module.

您可以使用GetModuleFileName来获取模块的完整路径。

The first argument is a handle to the required module. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process.

第一个参数是所需模块的句柄。如果此参数为 NULL,则 GetModuleFileName 检索当前进程的可执行文件的路径。

If you want the path to other module, you may use GetModuleHandleto get a handle. For example:

如果你想要其他模块的路径,你可以使用GetModuleHandle来获取句柄。例如:

  TCHAR path[_MAX_PATH+1]; 
  GetModuleFileName(GetModuleHandle(_T("MyDll.dll")), path, sizeof(path)/sizeof(path[0])); 

回答by Emir Akayd?n

Your problem is trying to see a Unicodestring on an Ansiconsole output window. If you really want to see the result, you need to cast your strings to Ansi(with some loss of course) or you can directly use;

您的问题是试图UnicodeAnsi控制台输出窗口上查看字符串。如果你真的想看到结果,你需要将你的字符串强制转换为Ansi(当然有一些损失)或者你可以直接使用;

char path[2048];
GetModuleFileNameA(NULL, path, 2048);
cout << path;

If you want to use Unicode, use TCHARand GetModuleFileNameW(or GetModuleFileNamesince your application is in unicode mode) but don't try to output to console window without casting to Ansi.

如果您想使用Unicode、 使用TCHARGetModuleFileNameW(或GetModuleFileName因为您的应用程序处于 unicode 模式)但不要尝试输出到控制台窗口而不强制转换为Ansi.

回答by Seva Alekseyev

TCHAR s[MAX_PATH+1];
GetModuleFileName(hInstance, s, _countof(s));

where hInstanceis a parameter of DllMain. Despite the name, it returns the full path.

其中hInstance是 DllMain 的参数。尽管有名称,但它返回完整路径。