/* 功能:獲取pc所有物理磁盤總數(shù)(本地硬盤,、軟盤、移動磁盤等) 返回值: 物理磁盤數(shù) */ extern "C" DEVINFO_API int GetDiskCount() { int iret = 0; std::string strtmp,strLetter; char lpDrives[MAX_PATH]; char szSymLink[MAX_PATH]; DWORD dwLen = ::GetLogicalDriveStrings(MAX_PATH, lpDrives); char * p ; p = lpDrives; //g_DiskDevMap.clear(); g_DiskDevV.clear(); for(DWORD nIndex = 0; nIndex < dwLen / 4; nIndex++) { long itype = GetDriveType( p ); if(p[0]<'C') { p = p+4; continue ;;; } if ( ( itype!=DRIVE_CDROM) ||(itype!=DRIVE_REMOTE)||(itype!=DRIVE_UNKNOWN)||(itype!=DRIVE_NO_ROOT_DIR)) { ::ZeroMemory(szSymLink,MAX_PATH); if(GetPhysicaldriveNo(p[0],szSymLink)==0) { strtmp.assign(szSymLink); strLetter = p[0]; BOOL bHave = FALSE ; DriversInfo di; for(int i=0;i<g_DiskDevV.size();i++) { if(g_DiskDevV[i].szSymLink == strtmp) { g_DiskDevV[i].driverLetter.append(strLetter); bHave = TRUE ; break; } } if(!bHave) { di.driverLetter = strLetter; di.szSymLink.assign(szSymLink);; g_DiskDevV.push_back(di); } /*if(g_DiskDevMap.find(szSymLink)strLetter != g_DiskDevMap.end()) { g_DiskDevMap[strtmp].append(); } else g_DiskDevMap[strtmp] = strLetter ;*/ } p = p+4 ; } else { p = p+4; continue ;; } } iret = g_DiskDevV.size();//g_DiskDevMap.size(); return iret; } /* 功能:獲取指定盤符對應(yīng)物理設(shè)備DosDevice名 參數(shù): letter 盤符 szSymLink DosDevice名 返回值: 0 成功 其他 失敗 */ int GetPhysicaldriveNo(char letter,char * szSymLink) { //函數(shù) char buf[] = "\\\\.\\ :"; buf[4] = letter; HANDLE hFile = CreateFile(buf,GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); VOLUME_DISK_EXTENTS vde; DWORD readed; BOOL ret = DeviceIoControl(hFile,IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,NULL,0, &vde, sizeof(VOLUME_DISK_EXTENTS),&readed,NULL); CloseHandle(hFile); if (ret) { sprintf(szSymLink,"\\\\.\\PHYSICALDRIVE%d",vde.Extents[0].DiskNumber); return 0; } else { // PrintLastError(""); return -1; } } |
|