VC++ HeapAlloc 内部函数提供空指针

VC++ HeapAlloc inside function gives null pointer

本文关键字:空指针 内部函数 HeapAlloc VC++      更新时间:2023-10-16

我正在尝试使用HeapAlloc()来分配SetupDiGetDeviceRegistryProperty()使用的缓冲区。

GetDeviceInformation()里面,我有:

HANDLE hHeap = GetProcessHeap();
while (SetupDiEnumDeviceInfo(DeviceInfoSet, MemberIndex++, DeviceInfoData)) 
{
    DWORD DataT;
    LPTSTR buffer = NULL;
    DWORD buffersize = 0;
    // port of device
    DWORD portnum = 0;
    GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_FRIENDLYNAME, 
         &DataT, buffer, &buffersize, &buffersize);
    if (!buffer) 
    {
        cerr << "Null Ptr!" << endl;
        exit(1);
    }
    // Do stuff, uninstall device

    if (buffer) HeapFree(hHeap, NULL, buffer); 
    }
}

GetRegistryProperty()里面,我有:

void GetRegistryProperty(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, 
    DWORD Property, PDWORD DataT, LPTSTR buffer, PDWORD buffersize, PDWORD size)
{

    HANDLE hHeap = GetProcessHeap();
    while (!SetupDiGetDeviceRegistryProperty(
        DeviceInfoSet,
        DeviceInfoData,
        Property, //SPDRP_FRIENDLYNAME or SPDRP_CLASS
        DataT, //&DataT
        (PBYTE)buffer,
        *buffersize,
        size)) //&buffersize
    {
        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            // Change the buffer size.
            if (buffer) HeapFree(hHeap, NULL, buffer); 
            // Double the size to avoid problems on 
            // W2k MBCS systems per KB 888609. 
            buffer = (LPTSTR)HeapAlloc(hHeap, HEAP_ZERO_MEMORY |
                HEAP_GENERATE_EXCEPTIONS, *buffersize * 2);
        }
        else
        {
            // error handling
            break;
        }
    }
}

HeapAlloc()按预期工作(缓冲区填充属性),直到GetRegistryProperty()返回。 此时,缓冲区始终为 NULL。 这也是意料之中的吗? 如何返回一个 char *,指向一个数组,该数组的寿命超过了创建它的函数的生命周期? 我假设我不明白 HeapAlloc() 是如何工作的。

我将其放在一个单独的函数中的原因是我想用不同的DWORD Property多次调用GetRegistryProperty()。 在我将代码移动到单独的函数之前,它运行良好。

通过引用传递buffer(注意 LPTSTR&):

void GetRegistryProperty(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, 
    DWORD Property, PDWORD DataT, LPTSTR& buffer, PDWORD buffersize, PDWORD size)

您正在按值传递buffer,因此在 GetRegistryProperty 中,当您重新分配它时,您只需覆盖 GetRegistryProperty 中的指针副本。

将 GetRegistryProperty 的签名更改为:

void GetRegistryProperty(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, DWORD Property, PDWORD DataT, LPTSTR& buffer, PDWORD buffersize, PDWORD size)