⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 枚举windows操作系统中物理磁盘和逻辑磁盘的信息
💻 CPP
字号:
#pragma warning(disable:4786)
#include <windows.h>
#include <cfgmgr32.h>
#include <Setupapi.h>
#include <cfgmgr32.h>
#include <initguid.h>
#include <iostream>
#include <string>
#include <vector>
#include "scsicmd.h"
#include "DeviceList.h"
using namespace std;
DEFINE_GUID(HARDDISK_GUID, 0x53f56307, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
DEFINE_GUID(VOLUME_GUID, 0x53f5630d, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
//DEFINE_GUID(xxqaz,0x36FC9E60, 0xC465, 0x11CF, 0x80, 0x56, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00);
struct DISKGEOMETRY{
	DWORD Cylinders,Heads,Sectors,BytesPerSector;
	DISKGEOMETRY():Cylinders(0),Heads(0),Sectors(0){}
};
struct DEVICEINFO{
	wstring Ring3Interface,FriendlyName,DeviceDesc,PhysicalDeviceObjectName,Service;
	wstring ParentInstanceID,ParentDeviceDesc;
	wstring StorageManagerName;
	DWORD StorageDeviceNumber;
	DWORD Sectors,BytesPerSector;
	DISKGEOMETRY DiskGeometry;
	DEVICEINFO():StorageDeviceNumber(-1),Sectors(0),BytesPerSector(0){}
};
DWORD GetDeviceInfo(GUID const*const guid,vector<DEVICEINFO>&DeviceVect,bool const QuestSCSIInfo=false);
void OutputMsg(wstring const msg,bool const end=true);
void main(){
	vector<DEVICEINFO>PhysicalDrive,Volume;
	GetDeviceInfo(&HARDDISK_GUID,PhysicalDrive,true);
	GetDeviceInfo(&VOLUME_GUID,Volume);
	vector<DEVICEINFO>::const_iterator pcx=Volume.begin();
	for(;pcx!=Volume.end();++pcx){
		wcout<<L"FriendlyName:"<<pcx->FriendlyName<<endl;
		wcout<<L"PhysicalDeviceObjectName:"<<pcx->PhysicalDeviceObjectName<<endl;
		wcout<<L"Service:"<<pcx->Service<<endl;
		wcout<<L"SerialNo."<<pcx->ParentInstanceID<<endl;
		wcout<<L"Device Type:"<<pcx->ParentDeviceDesc<<endl;
		wcout<<L"StorageManagerNumber:0x"<<hex<<pcx->StorageDeviceNumber<<dec<<L"  StorageManagerName:"<<pcx->StorageManagerName<<endl;
		vector<wstring>const AliasNames=GetAliasNames(pcx->PhysicalDeviceObjectName);
		vector<wstring>::const_iterator pcy=AliasNames.begin();
		for(;pcy!=AliasNames.end();++pcy){
			wcout<<L"  AliasName:"<<pcy->c_str()<<endl;
		}
		wcout<<endl;
	}
	vector<DEVICEINFO>::const_iterator pc=PhysicalDrive.begin();
	for(;pc!=PhysicalDrive.end();++pc){
		wcout<<L"FriendlyName:"<<pc->FriendlyName<<endl;
		wcout<<L"PhysicalDeviceObjectName:"<<pc->PhysicalDeviceObjectName<<endl;
		wcout<<L"Service:"<<pc->Service<<endl;
		wcout<<L"SerialNo."<<pc->ParentInstanceID<<endl;
		wcout<<L"Device Type:"<<pc->ParentDeviceDesc<<endl;
		wcout<<L"Capacity:"<<double(pc->Sectors)/2048<<L" MegaBytes  SectorSize:"
			<<pc->BytesPerSector<<L"(0x"<<hex<<pc->BytesPerSector<<L")"<<endl;
		wcout<<L"Cylinders:"<<dec<<pc->DiskGeometry.Cylinders
			<<L"(0x"<<hex<<pc->DiskGeometry.Cylinders;
		wcout<<L"),Heads:"<<dec<<pc->DiskGeometry.Heads
			<<L"(0x"<<hex<<pc->DiskGeometry.Heads;
		wcout<<L"),Sectors:"<<dec<<pc->DiskGeometry.Sectors
			<<L"(0x"<<hex<<pc->DiskGeometry.Sectors;
		wcout<<L"),SectorSize:"<<dec<<pc->DiskGeometry.BytesPerSector
			<<L"(0x"<<hex<<pc->DiskGeometry.BytesPerSector<<dec<<L")"<<endl;
		if(pc->BytesPerSector!=pc->DiskGeometry.BytesPerSector)
			wcout<<L"Warning Sector Sizes are Conflicted."<<endl;
		DWORD const tmpSectors=pc->DiskGeometry.Cylinders*pc->DiskGeometry.Heads*pc->DiskGeometry.Sectors;
		if(pc->Sectors!=tmpSectors)
			wcout<<L"Warning Total Sectors are Conflicted:"<<dec<<pc->Sectors<<hex<<L"(0x"<<pc->Sectors
			<<L"):"<<dec<<tmpSectors<<hex<<L"(0x"<<tmpSectors<<L")"<<endl;
		vector<DEVICEINFO>::const_iterator pcx=Volume.begin();
		for(;pcx!=Volume.end();++pcx){
			if((pc->StorageDeviceNumber==pcx->StorageDeviceNumber)&&(pc->StorageDeviceNumber==pcx->StorageDeviceNumber)){
				OutputMsg(L"  "+pcx->DeviceDesc+L"  "+pcx->PhysicalDeviceObjectName,false);
				vector<wstring>const AliasNames=GetAliasNames(pcx->PhysicalDeviceObjectName);
				vector<wstring>::const_iterator pcy=AliasNames.begin();
				for(;pcy!=AliasNames.end();++pcy){
					if(2==pcy->size())
						if((pcy->at(1)==L':')&&(((pcy->at(0)>=L'A')&&(pcy->at(0)<=L'Z'))||((pcy->at(0)>=L'a')&&(pcy->at(0)<=L'z')))){
							wcout<<L"  DriveLetter "<<pcy->c_str();
						}
				}
				wcout<<endl;
			}
		}
		wcout<<endl;
	}
}
wstring GetRegistryPropertyW(HDEVINFO const info,SP_DEVINFO_DATA const&did,DWORD const Property);
bool GetParentID(DEVINST const DevInst,DEVINST&dnDevInst,wstring&IDName,wstring&ParentDeviceDesc);
bool GetStorageInfo(wstring const Ring3Interface,DWORD&StorageDeviceNumber,wstring&StorageManagerName);
DWORD GetDeviceInfo(GUID const*const guid,vector<DEVICEINFO>&DeviceVect,bool const QuestSCSIInfo){
	HDEVINFO info=SetupDiGetClassDevsW(guid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
	if(INVALID_HANDLE_VALUE==info){
		return 0;
	}
	DWORD ErrorCount=0;
	SP_INTERFACE_DEVICE_DATA ifdata;
	ifdata.cbSize=sizeof(ifdata);
	DWORD devindex;
	for(devindex=0;SetupDiEnumDeviceInterfaces(info,NULL,guid,devindex,&ifdata);++devindex){
		DEVICEINFO tmpinfo;
		DWORD needed=0;
		SetupDiGetDeviceInterfaceDetailW(info,&ifdata,NULL,0,&needed,NULL);
		PSP_INTERFACE_DEVICE_DETAIL_DATA_W detail=(PSP_INTERFACE_DEVICE_DETAIL_DATA_W)new char[needed];
		detail->cbSize=sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA_W);
		SP_DEVINFO_DATA did={sizeof(SP_DEVINFO_DATA)};
		if(!SetupDiGetDeviceInterfaceDetailW(info,&ifdata,detail,needed,&needed,&did)){
			ErrorCount++;
			delete[](char*)detail;detail=NULL;needed=0;
			continue;
		}
		tmpinfo.Ring3Interface=detail->DevicePath;
		delete[](char*)detail;detail=NULL;needed=0;
		tmpinfo.FriendlyName=GetRegistryPropertyW(info,did,SPDRP_FRIENDLYNAME);
		tmpinfo.DeviceDesc=GetRegistryPropertyW(info,did,SPDRP_DEVICEDESC);
		tmpinfo.PhysicalDeviceObjectName=GetRegistryPropertyW(info,did,SPDRP_PHYSICAL_DEVICE_OBJECT_NAME);
		tmpinfo.Service=GetRegistryPropertyW(info,did,SPDRP_SERVICE);
		DEVINST ParentInst;
		GetParentID(did.DevInst,ParentInst,tmpinfo.ParentInstanceID,tmpinfo.ParentDeviceDesc);
		GetStorageInfo(tmpinfo.Ring3Interface,tmpinfo.StorageDeviceNumber,tmpinfo.StorageManagerName);
		if(QuestSCSIInfo){
			GetCapacity(tmpinfo.Ring3Interface,tmpinfo.Sectors,tmpinfo.BytesPerSector);
			GetGeometry(tmpinfo.Ring3Interface,tmpinfo.DiskGeometry.Cylinders,tmpinfo.DiskGeometry.Heads,tmpinfo.DiskGeometry.Sectors,tmpinfo.DiskGeometry.BytesPerSector);
		}
		DeviceVect.push_back(tmpinfo);
	}
	SetupDiDestroyDeviceInfoList(info);
	return ErrorCount;
}
wstring GetRegistryPropertyW(HDEVINFO const info,SP_DEVINFO_DATA const&did,DWORD const Property){
	DWORD needed=0;
	SetupDiGetDeviceRegistryPropertyW(info,(PSP_DEVINFO_DATA)&did,Property,NULL,NULL,0,&needed);
	PBYTE pBuf=new BYTE[needed];
	if(SetupDiGetDeviceRegistryPropertyW(info,(PSP_DEVINFO_DATA)&did,Property,NULL,pBuf,needed,&needed)){
		wstring const tmp=wstring::pointer(pBuf);
		delete[]pBuf;
		return tmp;
	}else{
		delete[]pBuf;
		return wstring();
	}
}
bool GetParentID(DEVINST const DevInst,DEVINST&dnDevInst,wstring&IDName,wstring&ParentDeviceDesc){
	bool success=false;
	dnDevInst=0;
	IDName.erase();
	if(CR_SUCCESS==CM_Get_Parent(&dnDevInst,DevInst,0)){
		DWORD needed=0;
		if(CR_SUCCESS==CM_Get_Device_ID_Size(&needed,dnDevInst,0)){
			IDName.resize(needed);
			if(CR_SUCCESS==CM_Get_Device_IDW(dnDevInst,wstring::pointer(IDName.c_str()),needed,0)){
				HDEVINFO const info=SetupDiCreateDeviceInfoList(NULL,NULL);
				if(INVALID_HANDLE_VALUE!=info){
					SP_DEVINFO_DATA did={sizeof(SP_DEVINFO_DATA)};
					if(SetupDiOpenDeviceInfoW(info,IDName.c_str(),NULL,0,&did)){
						ParentDeviceDesc=GetRegistryPropertyW(info,did,SPDRP_DEVICEDESC);
						success=true;
					}
					SetupDiDestroyDeviceInfoList(info);
				}
			}else
				IDName.erase();
		}
	}
	return success;
}
bool GetStorageInfo(wstring const Ring3Interface,DWORD&StorageDeviceNumber,wstring&StorageManagerName){
	StorageDeviceNumber=-1;
	StorageManagerName.erase();
	DISK_PERFORMANCE DiskPerf;
	DWORD needed;
	bool success=false;
	HANDLE const hDevice=CreateFileW(Ring3Interface.c_str(),0,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
	if(DeviceIoControl(hDevice,IOCTL_DISK_PERFORMANCE,NULL,0,(void*)&DiskPerf,sizeof(DISK_PERFORMANCE),&needed,NULL)){
		StorageDeviceNumber=DiskPerf.StorageDeviceNumber;
		StorageManagerName.assign(DiskPerf.StorageManagerName,sizeof(DiskPerf.StorageManagerName));
		success=true;
	}
	return success;
}
void OutputMsg(wstring const msg,bool const end){
	DWORD tmp;
	WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),msg.c_str(),msg.size(),&tmp,NULL);
	if(end)
		wcout<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -