windows 获取当前模块所在文件夹路径

本文介绍了一个C++类GetModulePath,该类提供了获取当前模块绝对路径的方法。包括从地址获取模块句柄、验证模块句柄的有效性、获取模块文件名及目录等实用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值