You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.1 KiB
C++
108 lines
2.1 KiB
C++
|
1 year ago
|
#include "stdafx.h"
|
||
|
|
#include "ModulePath.h"
|
||
|
|
|
||
|
|
const char kEndChar = '\0';
|
||
|
|
const char kFilePathSeparators[] = "\\/";
|
||
|
|
|
||
|
|
HMODULE CModulePath::GetModuleHandleFromAddress(void *address)
|
||
|
|
{
|
||
|
|
MEMORY_BASIC_INFORMATION mbi;
|
||
|
|
DWORD result = ::VirtualQuery(address, &mbi, sizeof(mbi));
|
||
|
|
if (result != sizeof(mbi))
|
||
|
|
{
|
||
|
|
return static_cast<HMODULE>(0);
|
||
|
|
}
|
||
|
|
return static_cast<HMODULE>(mbi.AllocationBase);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool CModulePath::IsModuleHandleValid(HMODULE module_handle)
|
||
|
|
{
|
||
|
|
if (!module_handle)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return module_handle == GetModuleHandleFromAddress(module_handle);
|
||
|
|
}
|
||
|
|
|
||
|
|
HMODULE CModulePath::GetCurrentModuleHandle()
|
||
|
|
{
|
||
|
|
return GetModuleHandleFromAddress((void*)GetCurrentModuleHandle);
|
||
|
|
}
|
||
|
|
|
||
|
|
string CModulePath::GetModulePathName(HMODULE module_handle)
|
||
|
|
{
|
||
|
|
string mod_path;
|
||
|
|
if (IsModuleHandleValid(module_handle))
|
||
|
|
{
|
||
|
|
mod_path.resize(MAX_PATH);
|
||
|
|
mod_path.resize(::GetModuleFileName(module_handle, &mod_path[0], mod_path.size()));
|
||
|
|
}
|
||
|
|
|
||
|
|
return mod_path;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool CModulePath::IsFilePathSeparator(const char separator)
|
||
|
|
{
|
||
|
|
if (separator == kEndChar)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
size_t len = sizeof(kFilePathSeparators) / sizeof(wchar_t);
|
||
|
|
for (size_t i = 0; i < len; i++)
|
||
|
|
{
|
||
|
|
if (separator == kFilePathSeparators[i])
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool CModulePath::FilePathApartDirectory(const string &filepath_in, string &directory_out)
|
||
|
|
{
|
||
|
|
size_t index = filepath_in.size() - 1;
|
||
|
|
if (index <= 0 || filepath_in.size() == 0)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
for (; index != 0; index--)
|
||
|
|
{
|
||
|
|
if (IsFilePathSeparator(filepath_in[index]))
|
||
|
|
{
|
||
|
|
if (index == filepath_in.size() - 1)
|
||
|
|
{
|
||
|
|
directory_out = filepath_in;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
directory_out = filepath_in.substr(0, index + 1);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
string CModulePath::GetModuleDirectory(HMODULE module_handle)
|
||
|
|
{
|
||
|
|
string module_directory;
|
||
|
|
if (IsModuleHandleValid(module_handle))
|
||
|
|
{
|
||
|
|
if (FilePathApartDirectory(GetModulePathName(module_handle), module_directory))
|
||
|
|
{
|
||
|
|
return module_directory;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
|
||
|
|
string CModulePath::GetCurrentModuleDirectory()
|
||
|
|
{
|
||
|
|
return GetModuleDirectory(GetCurrentModuleHandle());
|
||
|
|
}
|
||
|
|
|