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

本文介绍了一个用于封装当前模块路径的类CMyDirectory,包括获取当前模块句柄、模块路径、上一级目录及模块名称的方法。同时,提供了修改文件路径中斜杠格式的实用功能。

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

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

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

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

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

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

下面是该类的详细代码:


#pragma once
#include <string>
using namespace std;


/************************************************************************

* 功能描述: 路径通用函数类

************************************************************************/

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

	* 功能描述: 修改文件路径中的"\"改为"\\"

	* 输入参数:

	* 输出参数:

	* 返回参数:

	* 其它说明:

	************************************************************************/

	string  ChangeFilePath(string path);


};


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;

}

string  CMyDirectory::ChangeFilePath(string path)
{
	std::string str = "F:\工具类\Alarm.wav";

	std::string preStr, nextStr;
	int pos = str.find("\\");
	while (pos>0)
	{
		preStr = str.substr(0, pos);
		preStr += "\\";
		nextStr = str.substr(pos + 1, str.length() - pos - 1);
		str = nextStr;
		pos = str.find("\\");
	}
	if (preStr.empty())
	{
		preStr = str;
	}
	else
	{
		preStr += str;
	}

	return preStr;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haimianjie2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值