【C/C++】 Win下获取程序运行地址

来源:互联网 发布:数据库部署方案 编辑:程序博客网 时间:2024/06/03 21:46

函数原型:

DWORD WINAPI GetModuleFileName(  _In_opt_ HMODULE hModule,  _Out_    LPTSTR  lpFilename,  _In_     DWORD   nSize);

使用要求:

  • 头文件: Windows.h
  • 相近函数:
    • GetModuleFileNameW (Unicode) 返回Unicode编码型
    • GetModuleFileNameA (ANSI) 返回Ansi编码型

代码:

获取ANSI编码型

string GetExePath(void)  {      char szFilePath[MAX_PATH + 1]={0};      string s;    GetModuleFileNameA(NULL, szFilePath, MAX_PATH);    //编码方式ANSI    s = szFilePath;    int idx = s.find_last_of('\\');    return s.substr(0,idx);  }  
原创粉丝点击