windows 获取当前模块所在文件夹路径
2017-06-07 10:11
447 查看
1.getmodulepath.h
#ifndef GETMODULEPATH
#define GETMODULEPATH
#include <wtypes.h>
#include <string>
/**
* @class getmodulepath
* @brief 这个类用来获取当前模块的绝对路径
*/
class GetModulePath
{
public:
static HMODULE GetModuleHandleFromAddress(void* address);
static bool IsModuleHandleValid(HMODULE module_handle);
static HMODULE GetCurrentModuleHandle();
static std::wstring GetModulePathName(HMODULE module_handle);
static bool IsFilePathSeparator(const wchar_t separator);
static bool FilePathApartDirectory(const std::wstring &filepath_in, std::wstring &directory_out);
static std::wstring GetModuleDirectory(HMODULE module_handle);
static std::wstring GetCurrentModuleDirectory();
};
#endif // GETMODULEPATH
2.getmodulepath.cpp
#include "getmodulepath.h"
const wchar_t kEndChar = L'\0';
const wchar_t kFilePathSeparators[] = L"\\/";
HMODULE GetModulePath::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 GetModulePath::IsModuleHandleValid(HMODULE module_handle)
{
if (!module_handle)
{
return false;
}
return module_handle == GetModuleHandleFromAddress(module_handle);
}
HMODULE GetModulePath::GetCurrentModuleHandle()
{
return GetModuleHandleFromAddress((void*)GetCurrentModuleHandle);
}
std::wstring GetModulePath::GetModulePathName(HMODULE module_handle)
{
std::wstring mod_path;
if (IsModuleHandleValid(module_handle))
{
mod_path.resize(MAX_PATH);
mod_path.resize(::GetModuleFileNameW(module_handle, &mod_path[0], mod_path.size()));
}
return mod_path;
}
bool GetModulePath::IsFilePathSeparator(const wchar_t 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 GetModulePath::FilePathApartDirectory(const std::wstring &filepath_in, std::wstring &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;
}
std::wstring GetModulePath::GetModuleDirectory(HMODULE module_handle)
{
std::wstring module_directory;
if (IsModuleHandleValid(module_handle))
{
if (FilePathApartDirectory(GetModulePathName(module_handle), module_directory))
{
return module_directory;
}
}
return L"";
}
std::wstring GetModulePath::GetCurrentModuleDirectory()
{
return GetModuleDirectory(GetCurrentModuleHandle());
}
3.使用示例
std::wstring dir = GetModulePath::GetCurrentModuleDirectory();
#ifndef GETMODULEPATH
#define GETMODULEPATH
#include <wtypes.h>
#include <string>
/**
* @class getmodulepath
* @brief 这个类用来获取当前模块的绝对路径
*/
class GetModulePath
{
public:
static HMODULE GetModuleHandleFromAddress(void* address);
static bool IsModuleHandleValid(HMODULE module_handle);
static HMODULE GetCurrentModuleHandle();
static std::wstring GetModulePathName(HMODULE module_handle);
static bool IsFilePathSeparator(const wchar_t separator);
static bool FilePathApartDirectory(const std::wstring &filepath_in, std::wstring &directory_out);
static std::wstring GetModuleDirectory(HMODULE module_handle);
static std::wstring GetCurrentModuleDirectory();
};
#endif // GETMODULEPATH
2.getmodulepath.cpp
#include "getmodulepath.h"
const wchar_t kEndChar = L'\0';
const wchar_t kFilePathSeparators[] = L"\\/";
HMODULE GetModulePath::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 GetModulePath::IsModuleHandleValid(HMODULE module_handle)
{
if (!module_handle)
{
return false;
}
return module_handle == GetModuleHandleFromAddress(module_handle);
}
HMODULE GetModulePath::GetCurrentModuleHandle()
{
return GetModuleHandleFromAddress((void*)GetCurrentModuleHandle);
}
std::wstring GetModulePath::GetModulePathName(HMODULE module_handle)
{
std::wstring mod_path;
if (IsModuleHandleValid(module_handle))
{
mod_path.resize(MAX_PATH);
mod_path.resize(::GetModuleFileNameW(module_handle, &mod_path[0], mod_path.size()));
}
return mod_path;
}
bool GetModulePath::IsFilePathSeparator(const wchar_t 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 GetModulePath::FilePathApartDirectory(const std::wstring &filepath_in, std::wstring &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;
}
std::wstring GetModulePath::GetModuleDirectory(HMODULE module_handle)
{
std::wstring module_directory;
if (IsModuleHandleValid(module_handle))
{
if (FilePathApartDirectory(GetModulePathName(module_handle), module_directory))
{
return module_directory;
}
}
return L"";
}
std::wstring GetModulePath::GetCurrentModuleDirectory()
{
return GetModuleDirectory(GetCurrentModuleHandle());
}
3.使用示例
std::wstring dir = GetModulePath::GetCurrentModuleDirectory();
相关文章推荐
- 黄聪:wordpress获取当前插件文件所在目录文件夹路径
- JS获取当前文件所在的文件夹全路径
- node.js认识学习二:__dirname获取当前模块文件所在目录的完整绝对路径
- 获取当前exe文件所在文件夹路径接口(c语言)
- [WinAPI] API 12 [获取程序所在的目录、程序模块路径,获取和设置当前目录]
- JS获取当前文件所在的文件夹全路径
- JS获取当前文件所在的文件夹全路径
- SDK获取程序所在的目录,程序模块路径,获取和设置当前目录
- 获取当前模块所在路径
- JS获取当前文件所在的文件夹全路径
- java:windows下获取当前虚拟机(java.exe)所在路径
- C#获取本执行程序所在的当前路径
- c#获取当前应用程序所在路径
- [转]c#获取当前应用程序所在路径
- C#获取执行程序所在的当前路径的方法
- 在Windows Mobile和Wince(Windows Embedded CE)下进行Native C++开发,如何取出当前执行文件的路径和调用模块的路径
- 批处理文件中获取当前所在路径的几种方法
- 获取当前文件所在路径
- C#获取当前应用程序所在路径的方法
- 【转】c#获取当前应用程序所在路径