简体   繁体   English

使用 GetProcessImageFileNameW 时出现未初始化的 LPWSTR 错误

[英]Uninitialized LPWSTR Error While Using GetProcessImageFileNameW

I am trying to use GetProcessImageFileNameW in a Windows kernel driver.我正在尝试在 Windows 内核驱动程序中使用 GetProcessImageFileNameW。

    LPWSTR path[MAX_PATH];
    if(GetProcessImageFileNameW(hProcess, path, MAX_PATH) == 0)
    {
        DbgPrint("Can't get the process image name");
        return;
    }

But when I build there is a compiler error "Using uninitialized memory 'path'"但是当我构建时出现编译器错误“使用未初始化的内存‘路径’”

How can I solve it?我该如何解决?

LPWSTR is a single wchar_t* pointer. LPWSTR是单个wchar_t*指针。 So LPWSTR path[MAX_PATH];所以LPWSTR path[MAX_PATH]; is creating an array of wchar_t* pointers.正在创建一个wchar_t*指针数组。

However, GetProcessImageFileNameW() takes an LPWSTR parameter, where the documentation says:但是, GetProcessImageFileNameW()需要一个LPWSTR参数,其中文档说:

lpImageFileName图像文件名

A pointer to a buffer that receives the full path to the executable file.指向接收可执行文件完整路径的缓冲区指针

That means GetProcessImageFileNameW() wants a pointer to an array of wchar_t characters, which it will then fill as needed.这意味着GetProcessImageFileNameW()一个指向wchar_t字符数组的指针,然后它将根据需要填充。

An array decays into a pointer to its 1st element.数组衰减为指向其第一个元素的指针。 So, you are passing a wchar_t** where a wchar_t* is expected.因此,您正在传递wchar_t** ,其中需要wchar_t* I'm surprised you are not getting a compiler error about a type mismatch, rather than an error about uninitialized memory.我很惊讶您没有收到关于类型不匹配的编译器错误,而不是关于未初始化内存的错误。

Try this instead:试试这个:

WCHAR path[MAX_PATH] = {};
if (!GetProcessImageFileNameW(hProcess, path, MAX_PATH))
{
    DbgPrint("Can't get the process image name");
    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM