VC++ HeapAlloc 内部函数提供空指针
VC++ HeapAlloc inside function gives null pointer
我正在尝试使用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)
相关文章:
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 在函数内初始化无符号字符指针将返回空指针
- 正确的方法是什么?调用指针到指针到指针内部的函数?
- 空指针传递给lambda函数不再为空
- 我多么应该将空指针传递给c ++函数,就像C#中的IntPter.Zero一样
- VC++ HeapAlloc 内部函数提供空指针
- 在函数中分配后的空指针
- C++ 通过函数指针调用函数内部的向量
- 函数参数中的空指针与对象
- 传递给函数的 C++ 空指针
- 传递给函数调用后的空指针sqlite3句柄
- 如何在函数C++中返回空指针
- 为什么Boost.Cconcept通过空指针调用析构函数
- 如何使函数不使用STL容器参数(类似于空指针)
- 当超出函数时,指针设置为空指针
- 为什么在空指针上调用解构函数?
- 当通过函数指针使用内部函数时,链接器错误
- 将对象的空指针传递给函数
- 什么更快:检查空指针或调用空函数
- 将向量作为空指针传递给函数