Last active
May 9, 2024 14:51
-
-
Save pwm1234/05280cf2e462853e183d to your computer and use it in GitHub Desktop.
path to dll containing a function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::string get_module_path(void* address) | |
{ | |
char path[FILENAME_MAX]; | |
HMODULE hm = NULL; | |
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | | |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | |
(LPCSTR)address, | |
&hm)) | |
{ | |
std::ostringstream oss; | |
oss << "GetModuleHandle returned " << GetLastError(); | |
throw std::runtime_error(oss.str()); | |
} | |
GetModuleFileNameA(hm, path, sizeof(path)); | |
std::string p = path; | |
return p; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace { | |
void foo() | |
{} | |
} | |
std::string path_to_my_dll() | |
{ | |
get_module_path(foo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the function `path_to_my_dll' returns the absolute path to the dll containing it. I use this to load a companion .properties file