libloaderapi GetModuleFileNameW

本文介绍Windows API函数GetModuleFileNameW的使用方法,该函数用于获取当前进程已加载模块的完整路径,包括如何处理路径长度超过缓冲区大小的情况,并提供了一个C++示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 简介

1 原型

DWORD GetModuleFileNameW(
HMODULE hModule,
LPWSTR  lpFilename,
DWORD   nSize
);

GetModuleFileNameW
获取当前进程已加载模块的文件的完整路径,该模块必须由当前进程加载。
如果想要获取其他进程已加载模块的文件路径,可以使用GetModuleFileNameEx函数。 

2 参数

hModule:

A handle to the loaded module whose path is being requested. If this parameter is NULL,GetModuleFileName retrieves the path of the executable file of the current process.

一个模块的句柄。可以是一个DLL模块,或者是一个应用程序的实例句柄。如果该参数为NULL,该函数返回该应用程序全路径。

lpFilename:

A pointer to a buffer that receives the fully qualified path of the module.If the length of the path is less than the size that the nSize parameter specifies,the function succeeds and the path is returned as a null-terminated string.

If the length of the path exceeds the size that the nSize parameter specifies,the function succeeds and the string is truncated to nSize characters including the terminating null character.

nSize:

The size of the lpFilename buffer, in TCHARs.

3 返回值

If the function succeeds, the return value is the length of the string that is copied to the buffer,in characters, not including the terminating null character.

If the buffer is too small to hold the module name, the string is truncated to nSize characters including the terminating null character, the function returns nSize,and the function sets the last error to ERROR_INSUFFICIENT_BUFFER.

二 举例

#include <Windows.h>
#include <iostream>
#include <string>

int main() {

  wchar_t szExeFile[MAX_PATH];
  GetModuleFileNameW(NULL, szExeFile, MAX_PATH);
  std::wstring path(szExeFile);

  getchar();
  return 0;
}

 szExeFile: L"F:\\Demo\\cppDemo\\Debug\\cppDemo.exe"

三 参考

GetModuleFileNameW

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值