• GetModuleFileName


    原文:http://www.cnblogs.com/dongzhiquan/archive/2009/07/28/1994776.html

    GetModuleFileName

    HMODULE hModule = ::GetModuleHandle(NULL);
    if (hModule != NULL)
    {
        ::GetModuleFileName(hModule, strRet.GetBuffer(MAX_PATH), MAX_PATH);
        strRet.ReleaseBuffer();
    }

    GetModuleFileName函数

    在 开发软件的过程里,经常需要把数据保存到当前执行文件路径下面,或者读取当前执行文件路径下的一些配置信息。这时就需要从当前模块里获取所在的目录路径, 以便进行固定的位置操作文件。要解决这个需求,就需要调用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(" "));
    #016 
    #017              //获取文件路径。
    #018              TCHAR* lpStrPath = chBuf;
    #019              PathRemoveFileSpec(lpStrPath);
    #020              OutputDebugString(lpStrPath);
    #021              OutputDebugString(_T(" "));
    #022        }
    #023 
    #024  }

    输出的结果如下:
    g:workwindows_apiwincpp2debugWinCpp.exe
    g:workwindows_apiwincpp2debug
  • 相关阅读:
    全字母短句
    java 遍历map的方法
    实现num1、num2交换,无中间变量
    N多条短信,用什么算法从中找出相似内容的来?
    Linux基础_磁盘分区
    Linux基础_软链接,硬链接
    Linux基础_系统启动流程
    Linux基础_合并,归档,压缩,dump,编辑器
    Linux基础_Linux操作系统简介
    计算机基础_操作系统
  • 原文地址:https://www.cnblogs.com/lizhigang/p/7261643.html
Copyright © 2020-2023  润新知