gpt4 book ai didi

c - GetModuleFileNameW 在当前模块上返回 Null

转载 作者:行者123 更新时间:2023-11-30 20:57:26 29 4
gpt4 key购买 nike

我正在尝试获取当前运行的可执行文件的路径,如下所示:

#include <Windows.h>
#include <stdio.h>

int main()
{
LPWSTR current_filepath = (LPWSTR)malloc(MAX_PATH * sizeof(LPWSTR));
DWORD path_size = 0;
DWORD buffer_size = GetModuleFileNameW(0, current_filepath, path_size);
if (!buffer_size)
{
DWORD err = GetLastError();
perror("Error: ");
}
else
{
wprintf(L"%ls \n", current_filepath);
}
}

但是当我在 if 上设置断点运行应用程序时,我可以看到 buffer_size 为 0 并且 current_filepath 未填充应有的路径。

我不确定我做错了什么,所以如果有人能指出正确的方向,我将不胜感激。

最佳答案

I'm trying to get the path of my currently running executable like so:

获取当前运行的 EXE 的文件路径基本上涉及将 NULLnullptr 传递给 GetModuleFileName

这就是您真正想要的:

LPCWSTR GetPathToRunningExe()
{
wchar_t szPath[MAX_PATH] = { 0 };
DWORD dwSize = GetModuleFileNameW(NULL, szPath, ARRAYSIZE(szPath));
wchar_t* result = NULL;

if (dwSize > 0)
{
size_t count = wcslen(szPath) + 1;
result = (wchar_t*)malloc(count * sizeof(szPath[0]));
wcscpy_s(result, count, szPath);
}
return result;
}

关于c - GetModuleFileNameW 在当前模块上返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59506719/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com