晚上每个人,我想知道如果有人能回答我2个简单的问题。Visual Studio错误并获取设备GUID和路径名称?
我做了一个应用程序与手臂设备进行通信工作正常,但是当我移动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。如果您指出哪条线路导致了错误,而不是让我们猜测或试图找出问题,那么它可能会非常有帮助,您不觉得吗?你可以编辑你的问题并添加某种评论,或者至少在你的问题文本中提到,以表明这一点?谢谢。 – 2012-07-25 01:51:53
嗨肯对不起,我削减了行号62行我已经标记在上面。完全错误的值是类型“HLOCAL”不能分配给类型为“LPTSTR”的实体 错误C2440:'=':无法从'HLOCAL'转换为'LPTSTR' 谢谢 – user1241548 2012-07-25 02:00:06