GetModuleFileName函数得到当前应用程序路径
来源:互联网 发布:域名备案注销流程 编辑:程序博客网 时间:2024/05/02 00:15
在开发软件的过程里,经常需要把数据保存到当前执行文件路径下面,或者读取当前执行文件路径下的一些配置信息。这时就需要从当前模块里获取所在的目录路径,以便进行固定的位置操作文件。要解决这个需求,就需要调用API函数GetModuleFileName来获取模块所在的路径。
函数GetModuleFileName声明如下:
WINBASEAPI
DWORD
WINAPI
GetModuleFileNameA(
__in_opt HMODULE hModule,
__out_ecount_part(nSize, return + 1) LPCH lpFilename,
__in DWORD nSize
);
WINBASEAPI
DWORD
WINAPI
GetModuleFileNameW(
__in_opt HMODULE hModule,
__out_ecount_part(nSize, return + 1) LPWCH lpFilename,
__in DWORD nSize
);
#ifdef UNICODE
#define GetModuleFileName GetModuleFileNameW
#else
#define GetModuleFileName GetModuleFileNameA
#endif // !UNICODE
hModule是模块的句柄,或者设置为NULL表示当前模块。
lpFilename是保存路径的缓冲区。
nSize是缓冲区的大小。
调用函数的例子如下:
#001 //获取当前程序所在路径。
#002 //蔡军生 2007/12/05 QQ:9073204 深圳
#003 void TestGetExePath(void)
#004 {
#005 //
#006 const int nBufSize = 512;
#007 TCHAR chBuf[nBufSize];
#008 ZeroMemory(chBuf,nBufSize);
#009
#010 //获取当前执行文件的路径。
#011 if (GetModuleFileName(NULL,chBuf,nBufSize))
#012 {
#013 //输出带文件名称路径。
#014 OutputDebugString(chBuf);
#015 OutputDebugString(_T("/r/n"));
#016
#017 //获取文件路径。
#018 TCHAR* lpStrPath = chBuf;
#019 PathRemoveFileSpec(lpStrPath);
#020 OutputDebugString(lpStrPath);
#021 OutputDebugString(_T("/r/n"));
#022 }
#023
#024 }
输出的结果如下:
g:/work/windows_api/wincpp2/debug/WinCpp.exe
g:/work/windows_api/wincpp2/debug
函数GetModuleFileName声明如下:
WINBASEAPI
DWORD
WINAPI
GetModuleFileNameA(
__in_opt HMODULE hModule,
__out_ecount_part(nSize, return + 1) LPCH lpFilename,
__in DWORD nSize
);
WINBASEAPI
DWORD
WINAPI
GetModuleFileNameW(
__in_opt HMODULE hModule,
__out_ecount_part(nSize, return + 1) LPWCH lpFilename,
__in DWORD nSize
);
#ifdef UNICODE
#define GetModuleFileName GetModuleFileNameW
#else
#define GetModuleFileName GetModuleFileNameA
#endif // !UNICODE
hModule是模块的句柄,或者设置为NULL表示当前模块。
lpFilename是保存路径的缓冲区。
nSize是缓冲区的大小。
调用函数的例子如下:
#001 //获取当前程序所在路径。
#002 //蔡军生 2007/12/05 QQ:9073204 深圳
#003 void TestGetExePath(void)
#004 {
#005 //
#006 const int nBufSize = 512;
#007 TCHAR chBuf[nBufSize];
#008 ZeroMemory(chBuf,nBufSize);
#009
#010 //获取当前执行文件的路径。
#011 if (GetModuleFileName(NULL,chBuf,nBufSize))
#012 {
#013 //输出带文件名称路径。
#014 OutputDebugString(chBuf);
#015 OutputDebugString(_T("/r/n"));
#016
#017 //获取文件路径。
#018 TCHAR* lpStrPath = chBuf;
#019 PathRemoveFileSpec(lpStrPath);
#020 OutputDebugString(lpStrPath);
#021 OutputDebugString(_T("/r/n"));
#022 }
#023
#024 }
输出的结果如下:
g:/work/windows_api/wincpp2/debug/WinCpp.exe
g:/work/windows_api/wincpp2/debug
- GetModuleFileName函数得到当前应用程序路径
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- MFC获得当前应用程序目录GetCurrentDirectory()和GetModuleFileName()函数
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- 获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数 .
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- GetModuleFileName 得到的路径有\??\
- GetModuleFileName函数当前运行exe或dll的路径
- MAC XCode 得到当前应用程序的路径
- VC新建目录CreateDirectory、得到exe存放路径GetModuleFileName、获取当前目录GetCurrentDirectory、CFileDialog设置打开默认路径
- VC新建目录CreateDirectory、得到exe存放路径GetModuleFileName、获取当前目录GetCurrentDirectory、CFileDialog设置打开默认路径
- 用GetModuleFileName获取程序当前执行路径
- GetModuleFileName获取当前进程完整路径
- VC中使用GetModuleFileName获取应用程序路径
- 利用元数据和规则搜索多个列表中指定范围的内容
- 价值3000元的代码作者利用一月赚10万(只做研究不可用来干坏事)
- java.lang.IllegalArgumentException
- 构建省份城市级联
- 安装Nginx
- GetModuleFileName函数得到当前应用程序路径
- MoteLab 使用说明
- nginx安装与配置
- struts2 中 action 跟 actionsupport 的区别
- 关于DISCUZNT上GOOGLE ADSENSE代码
- 转:240多个jQuery插件
- Gentoo安装手记
- 飞鸽遇上即时通讯
- Linux下Makefile的automake生成全攻略