c++中的GetModuleFileName函数的用法以及作用

参考:

1. http://blog.sina.com.cn/s/blog_b078a1cb0101fw48.html

2. https://www.cnblogs.com/Satu/p/8203936.html

函数原型:
DWORD GetModuleFileName(
HMODULE hModule,
LPTSTR lpFilename,
DWORD nSize
);
函数参数说明:
hModule HMODULE 装载一个程序实例的句柄。如果该参数为NULL,该函数返回该当前应用程序全路径。
lpFileName LPTSTR 是你存放返回的名字的内存块的指针,是一个输出参数
nSize DWORD ,装载到缓冲区lpFileName的最大值
函数返回值:
如果返回为成功,将在lpFileName的缓冲区当中返回相应模块的路径,如果所设的nSize过小,那么返回仅按所设置缓冲区大小返回相应字符串内容。
如果函数失败,返回值将为0,利用GetLastError可获得异常代码。
需要的头文件为:
windows.h
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
关于GetModuleFileName function
参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx
 
 
 
以下代码摘自Installing a Service(https://msdn.microsoft.com/en-us/library/windows/desktop/ms683500(v=vs.85).aspx)。

IDE: Microsoft Visual Studio Community 2017 15.5.2

操作系统:Windows 7 x64

#include "stdafx.h"    /* IDE自行创建的 */

#include <windows.h>

int main(int argc, char **argv)
{
    TCHAR szPath[MAX_PATH];

    if (!GetModuleFileName(NULL, szPath, MAX_PATH))
    {
        printf("Cannot get the module file name, error: (%d) ", GetLastError());
        return 1;
    }
    else {
        printf("Module file name: %ls ", szPath);
    }

    getchar();

    return 0;
}

关于MAX_PATH,在头文件minwindef.h中定义,但没有给出具体的描述。

#define MAX_PATH          260
原文地址:https://www.cnblogs.com/MCSFX/p/12973961.html