【发布时间】:2012-07-25 01:41:41
【问题描述】:
晚上大家我想知道是否有人可以回答我 2 个快速问题。
我制作了一个与 arm 设备通信的应用程序,它工作正常,但是当我移动 PC 等时,我需要重新配置设备路径。如下图所示。
路径:\\?\usb#vid_045e&pid_0040#6&ff454f2&0&3#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
我做了一些阅读,发现这是我需要的两个功能 SetupDiGetClassDevs 和 SetupDiGetDeviceInstanceId。我的问题是我找对地方了吗,即这两个函数会返回上面的路径。另外这条路径的技术名称是什么?
我在下面的微软网站上找到了我认为是一个很好的例子(总是从例子中学习更好),但这会引发错误C2440: '=' : cannot convert from 'HLOCAL' to 'LPTSTR'
哪个是我的新指针错误?
这是代码
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size (doubled)
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
// Double the returned buffersize to correct
// for underlying legacy CM functions that
// return an incorrect buffersize value on
// DBCS/MBCS systems.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = LocalAlloc(LPTR,buffersize * 2); // ERROR LINE
}
else
{
// Insert error handling here.
break;
}
}
printf("Result:[%s]\n",buffer);
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
希望它是一个简单的,谢谢。
【问题讨论】:
-
欢迎来到 StackOverflow。如果您指出是哪一行导致了错误,而不是让我们猜测或尝试弄清楚,这可能会非常有帮助,您不觉得吗?您可以编辑您的问题并添加某种评论,或者至少在您的问题文本中提及,以表明这一点吗?谢谢。
-
嗨,Ken,对不起,我把行号从盒子里剪了出来,它的第 62 行我在上面做了标记。完整的错误是“HLOCAL”类型的值无法分配给“LPTSTR”类型的实体错误 C2440:“=”:无法从“HLOCAL”转换为“LPTSTR”谢谢
标签: c++ winapi hardware enumerate