windowsapi函数:Windows API一日一练(83)GetModuleFileName函数



  在开发软件Software过程里经常需要把数据保存到当前执行文件路径下面或者读取当前执行文件路径下些配置信息这时就需要从当前模块里获取所在目录路径以便进行固定位置操作文件要解决这个需求就需要APIGetModuleFileName来获取模块所在路径

  GetModuleFileName声明如下:

WINBASEAPI
DWORD
WINAPI
GetModuleFileNameA(
    __in_opt HMODULE hModule,
    __out_ecount_part(nSize,  + 1) LPCH lpFilename,
    __in     DWORD nSize
    );
WINBASEAPI
DWORD
WINAPI
GetModuleFileNameW(
    __in_opt HMODULE hModule,
    __out_ecount_part(nSize,  + 1) LPWCH lpFilename,
    __in     DWORD nSize
    );
#def UNICODE
# GetModuleFileName GetModuleFileNameW
#
# GetModuleFileName GetModuleFileNameA
#end // !UNICODE


  hModule是模块句柄或者设置为NULL表示当前模块

  lpFilename是保存路径缓冲区

  nSize是缓冲区大小

  例子如下:

#001 //获取当前所在路径
#002  //蔡军生 2007/12/05 qq:9073204 深圳
#003  void TestGetExePath(void)
#004  {
#005         //
#006         const  nBufSize = 512;
#007         TCHAR chBuf[nBufSize];
#008         ZeroMemory(chBuf,nBufSize);
#009 
#010         //获取当前执行文件路径
#011         (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


Tags:  api函数查询工具 api函数大全 api函数 windowsapi函数

延伸阅读

最新评论

发表评论