本文将当前模块的路径封装成类,具体包括: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;
}