怎么列出设备名
如何列出设备名
我想查出电脑中的所有设备名,以便能用CreateFile打开(只是想测试下),可是我用QueryDosDevice不行的,请看:
char buf[10000];
if(0==QueryDosDevice(NULL,buf,10000))
{
cout<<"Fail:"<< GetLastError()<<endl;
return;
}
cout<<buf<<endl;
它只输出个Gllobal,我想问错在哪?怎么解决
------解决方案--------------------
我想查出电脑中的所有设备名,以便能用CreateFile打开(只是想测试下),可是我用QueryDosDevice不行的,请看:
char buf[10000];
if(0==QueryDosDevice(NULL,buf,10000))
{
cout<<"Fail:"<< GetLastError()<<endl;
return;
}
cout<<buf<<endl;
它只输出个Gllobal,我想问错在哪?怎么解决
------解决方案--------------------
- C/C++ code
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 ; } // 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 // to Alloc the buffer. Keep calling until // success or an unknown failure. while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, &DataT, (PBYTE)buffer, buffersize, &buffersize)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { //改变缓冲区大小 if (buffer) LocalFree(buffer); buffer = (char*)LocalAlloc(LPTR,buffersize); } else { //在这里放错误处理代码 break; } } // buffer,设备名称 if (buffer) LocalFree(buffer); } if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS ) { //在这里放错误处理代码 return ; } // 清理 SetupDiDestroyDeviceInfoList(hDevInfo);