Skip to content

Instantly share code, notes, and snippets.

@pwm1234
Last active May 9, 2024 14:51
Show Gist options
  • Save pwm1234/05280cf2e462853e183d to your computer and use it in GitHub Desktop.
Save pwm1234/05280cf2e462853e183d to your computer and use it in GitHub Desktop.
path to dll containing a function
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;
}
namespace {
void foo()
{}
}
std::string path_to_my_dll()
{
get_module_path(foo);
}
@pwm1234
Copy link
Author

pwm1234 commented May 31, 2015

the function `path_to_my_dll' returns the absolute path to the dll containing it. I use this to load a companion .properties file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment