Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ VC++;函数内部的HeapAlloc提供空指针_C++_C_Visual C++_Malloc_Heapalloc - Fatal编程技术网

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

C++ VC++;函数内部的HeapAlloc提供空指针,c++,c,visual-c++,malloc,heapalloc,C++,C,Visual C++,Malloc,Heapalloc,我正在尝试使用HeapAlloc()分配SetupDiGetDeviceRegistryProperty()使用的缓冲区 在GetDeviceInformation()中我有: HANDLE hHeap = GetProcessHeap(); while (SetupDiEnumDeviceInfo(DeviceInfoSet, MemberIndex++, DeviceInfoData)) { DWORD DataT; LPTSTR buffer = NULL; D

我正在尝试使用
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); 

    }
}
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()
返回。此时,缓冲区总是空的。这也是预期的吗?如何返回指向数组的char*,该数组的生命周期超过了创建它的函数的生命周期?我想我不明白HeapAlloc()是如何工作的


我之所以将它放在一个单独的函数中,是因为我想用不同的
DWORD属性多次调用
GetRegistryProperty()
。在我将代码移动到一个单独的函数之前,它工作得非常好。

通过引用传递
缓冲区(注意LPTSTR&):


通过引用传递
缓冲区
(注意LPTSTR&):


您通过值传递
缓冲区
,因此在GetRegistryProperty中,重新分配缓冲区时,只需覆盖GetRegistryProperty中指针的副本

将GetRegistryProperty的签名更改为:

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

您通过值传递
缓冲区
,因此在GetRegistryProperty中,重新分配缓冲区时,只需覆盖GetRegistryProperty中指针的副本

将GetRegistryProperty的签名更改为:

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