获取计算机移动设备的信息

//遍历系统盘,找到移动设备
void GETUsbList()
{
	HANDLE hDevice;               // handle to the drive to be examined
	BOOL result;                 // results flag
	DWORD readed;                   // discard results

	STORAGE_DESCRIPTOR_HEADER *pDevDescHeader;
	STORAGE_DEVICE_DESCRIPTOR *pDevDesc;
	DWORD devDescLength;
	STORAGE_PROPERTY_QUERY query;

	std::wstring wsSrcPath;
	for (wchar_t d = 'A'; d <= 'Z'; d++)
	{
		TCHAR szTemp[2] = { d ,':'};
		wsSrcPath = TChar2ws(szTemp);

		if(wsSrcPath.empty())
		{
			return;
		}
		wsSrcPath = wsSrcPath.substr(0,wsSrcPath.find(L":"));

		std::map<std::wstring, std::wstring>::iterator iter = mUsbNameList.find(wsSrcPath);  
		if(iter != mUsbNameList.end()) 
		{
			return;
		}

		wsSrcPath = wsSrcPath.substr(0,wsSrcPath.find(L":\\"));
		std::wstring szPath = wsformat(L"\\\\.\\%s:",wsSrcPath.c_str());

		hDevice = CreateFile(
			szPath.c_str(), // drive to open
			GENERIC_READ | GENERIC_WRITE,     // access to the drive
			FILE_SHARE_READ | FILE_SHARE_WRITE, //share mode
			NULL,             // default security attributes
			OPEN_EXISTING,    // disposition
			0,                // file attributes
			NULL            // do not copy file attribute
			);
		if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
		{
			fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError());
			continue;
		}

		query.PropertyId = StorageDeviceProperty;
		query.QueryType = PropertyStandardQuery;

		pDevDescHeader = (STORAGE_DESCRIPTOR_HEADER *)malloc(sizeof(STORAGE_DESCRIPTOR_HEADER));
		if (NULL == pDevDescHeader)
		{
			continue;
		}

		result = DeviceIoControl(
			hDevice,     // device to be queried
			IOCTL_STORAGE_QUERY_PROPERTY,     // operation to perform
			&query,
			sizeof query,               // no input buffer
			pDevDescHeader,
			sizeof(STORAGE_DESCRIPTOR_HEADER),     // output buffer
			&readed,                 // # bytes returned
			NULL);      // synchronous I/O
		if (!result)        //fail
		{
			fprintf(stderr, "IOCTL_STORAGE_QUERY_PROPERTY Error: %ld\n", GetLastError());
			free(pDevDescHeader);
			(void)CloseHandle(hDevice);
			continue ;
		}

		devDescLength = pDevDescHeader->Size;
		pDevDesc = (STORAGE_DEVICE_DESCRIPTOR *)malloc(devDescLength);
		if (NULL == pDevDesc)
		{
			free(pDevDescHeader);
			continue;
		}

		result = DeviceIoControl(
			hDevice,     // device to be queried
			IOCTL_STORAGE_QUERY_PROPERTY,     // operation to perform
			&query,
			sizeof query,               // no input buffer
			pDevDesc,
			devDescLength,     // output buffer
			&readed,                 // # bytes returned
			NULL);      // synchronous I/O
		if (!result)        //fail
		{
			fprintf(stderr, "IOCTL_STORAGE_QUERY_PROPERTY Error: %ld\n", GetLastError());
			free(pDevDescHeader);
			free(pDevDesc);
			(void)CloseHandle(hDevice);
			continue ;
		}

		if(BusTypeUsb == (WORD)pDevDesc->BusType)
		{
			//类型收移动设备的处理
		}
		else
		{
			continue;
		}
	}
	return ;
}

//获取移动设备硬件信息
void GetUsbInfo(std::wstring wsSrcPath)
{
	if(wsSrcPath.empty())
	{
		return;
	}
	wsSrcPath = wsSrcPath.substr(0,wsSrcPath.find(L":\\"));
	std::wstring szPath = wsformat(L"\\\\.\\%s:",wsSrcPath.c_str());
	
	HANDLE  hDevice = CreateFile(szPath.c_str(), GENERIC_READ,
		FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);

	if (hDevice != INVALID_HANDLE_VALUE)
	{

		PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];

		pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;

		if(GetDisksProperty(hDevice, pDevDesc))
		{
			char *p= (char*)pDevDesc;
			VendorId = (pDevDesc->VendorIdOffset ? &p[pDevDesc->VendorIdOffset]:"(NULL)");
			ProductId=(pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset]:"(NULL)");
			ProductRevision = (pDevDesc->ProductRevisionOffset ? &p[pDevDesc->ProductRevisionOffset] : "(NULL)");
			SerialNumber = (pDevDesc->SerialNumberOffset ? &p[pDevDesc->SerialNumberOffset] : "(NULL)");

		}
		delete pDevDesc;
		CloseHandle(hDevice);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值