visual C++实现MFC动态库运用debugview输出日志2:获取当前模块的路径

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haimianjie2012/article/details/82843732

本文将当前模块的路径封装成类,具体包括:1.

   /************************************************************************
    * 功能描述: 获取当前模块句柄
    * 输入参数:
    * 输出参数:
    * 返回参数:
    * 其它说明:                                                                     
    ************************************************************************/
    inline HMODULE GetCurrentModule()

/************************************************************************
    * 功能描述: 获取当前模块路径
    * 输入参数:
    * 输出参数:
    * 返回参数:
    * 其它说明:                                                                     
    ************************************************************************/
    CString GetCurrentPath();

    /************************************************************************
    * 功能描述: 获取上一级目录
    * 输入参数:
    * 输出参数:
    * 返回参数:
    * 其它说明:                                                                     
    ************************************************************************/
    CString GetParentPath();

    /************************************************************************
    * 功能描述: 获取当前模块名称
    * 输入参数:
    * 输出参数:
    * 返回参数:
    * 其它说明:                                                                     
    ************************************************************************/
    CString GetModuleName();

下面是该类的详细代码:

#pragma once

/************************************************************************
* 功能描述: 路径通用函数类
* 创建日期:2014-09-29
* 作  者:tanmin
* 其它说明:                                                                     
************************************************************************/
class CMyDirectory
{
public:
	CMyDirectory(void);
	~CMyDirectory(void);
	public:
	/************************************************************************
	* 功能描述: 获取当前模块句柄
	* 输入参数:
	* 输出参数:
	* 返回参数:
	* 其它说明:                                                                     
	************************************************************************/
	inline HMODULE GetCurrentModule()
	{
#if _MSC_VER < 1300
		MEMORY_BASIC_INFORMATION mbi;
		static int dummy;
		VirtualQuery(&dummy, &mbi, sizeof(mbi));
		return reinterpret_cast<HMODULE>(mbi.AllocationBase);
#else
		return reinterpret_cast<HMODULE>(&__ImageBase);
#endif
	}
public:
	/************************************************************************
	* 功能描述: 获取当前模块路径
	* 输入参数:
	* 输出参数:
	* 返回参数:
	* 其它说明:                                                                     
	************************************************************************/
	CString GetCurrentPath();

	/************************************************************************
	* 功能描述: 获取上一级目录
	* 输入参数:
	* 输出参数:
	* 返回参数:
	* 其它说明:                                                                     
	************************************************************************/
	CString GetParentPath();

	/************************************************************************
	* 功能描述: 获取当前模块名称
	* 输入参数:
	* 输出参数:
	* 返回参数:
	* 其它说明:                                                                     
	************************************************************************/
	CString GetModuleName();
};

CMyDirectory.cpp实现

#include "StdAfx.h"
#include "MyDirectory.h"


CMyDirectory::CMyDirectory(void)
{
}


CMyDirectory::~CMyDirectory(void)
{
}
CString CMyDirectory::GetCurrentPath()
{
	TCHAR path[MAX_PATH] = {0};
	GetModuleFileName(GetCurrentModule(), path, MAX_PATH);
	CString str = path;
	int nlen = str.ReverseFind('\\');
	str = str.Left(nlen);
	if (str.Right(0) != _T("\\"))
		str = str + _T("\\");
	return str;
}

CString CMyDirectory::GetParentPath()
{
	TCHAR path[MAX_PATH] = {0};
	GetModuleFileName(GetCurrentModule(), path, MAX_PATH);
	PathRemoveFileSpec(path);
	CString str = path;
	int nlen = str.ReverseFind('\\');
	str = str.Left(nlen);
	if (str.Right(0) != _T("\\"))
		str = str + _T("\\");
	return str;
}



CString CMyDirectory::GetModuleName()
{
	TCHAR path[MAX_PATH] = {0};
	GetModuleFileName(GetCurrentModule(), path, MAX_PATH);
	CString str = path;
	int nlen = str.ReverseFind('\\');
	int nsize = str.GetLength();
	str = str.Right(nsize - nlen - 1);
	return str;
}

猜你喜欢

转载自blog.csdn.net/haimianjie2012/article/details/82843732