1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| STDAPI_(BOOL) GetVolumeCollection(BYTE byVolLetter, PVOLUMECOLLECTION pVolCll, LPWSTR pParentInstId) { WCHAR szParentInstanceId[MAX_DEVICE_ID_LEN] = {}; WCHAR szTmpInstanceId[MAX_DEVICE_ID_LEN] ={}; WCHAR szDriveLetters[26*2*4] ={}; BOOL bRet; DWORD cbBytesReturned; LPWSTR pszDriveRoot = NULL; size_t uStrlen;
pVolCll->dwCount = 0;
bRet = GetParentInstanceId(byVolLetter, szParentInstanceId, NULL, 0); if (!bRet) return FALSE; pszLogicalDrives);
cbBytesReturned = GetLogicalDriveStrings (ARRAYSIZE(szDriveLetters)*sizeof(WCHAR), szDriveLetters);
for (pszDriveRoot = szDriveLetters; *pszDriveRoot != TEXT('\0'); pszDriveRoot += uStrlen + 1) { StringCchLength(pszDriveRoot, 26*2*4, &uStrlen); if (((BYTE)(*pszDriveRoot) == 'A') || ((BYTE)(*pszDriveRoot) == 'a') || ((BYTE)(*pszDriveRoot) == 'B') || ((BYTE)(*pszDriveRoot) == 'b')) continue;
bRet = GetParentInstanceId((BYTE)(*pszDriveRoot), szTmpInstanceId, NULL, 0); if (bRet && (StrCmpI(szParentInstanceId,szTmpInstanceId) == 0)) { pVolCll->byVolume[pVolCll->dwCount] = (BYTE)(*pszDriveRoot); pVolCll->dwCount++; }
} if (pParentInstId) StringCchCopy(pParentInstId, MAX_DEVICE_ID_LEN, szParentInstanceId);
return TRUE; }
BOOL GetParentInstanceId(BYTE byVolLetter,LPWSTR pParentInstId, LPWSTR pParentDevName,DWORD dwBufSize) { HANDLE hDevice = INVALID_HANDLE_VALUE; WCHAR szDeviceName[8]; STORAGE_PROPERTY_QUERY spq; STORAGE_DEVICE_NUMBER sdn; STORAGE_DEVICE_NUMBER sdnTmp; BOOL bSuccess; DWORD cbBytesReturned; BYTE byBuffer[4096]; BOOL bRet = FALSE; GUID *pGuidInferface = NULL;
HDEVINFO hIntDevInfo = INVALID_HANDLE_VALUE; DWORD dwIndex; SP_DEVICE_INTERFACE_DATA interfaceData; SP_DEVINFO_DATA deviceInfoData; CONFIGRET configret; DEVINST dnDevInstParent; WCHAR szDeviceInstanceID[MAX_DEVICE_ID_LEN]={}; HANDLE hDev = INVALID_HANDLE_VALUE; PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = NULL;
StringCchPrintf(szDeviceName, ARRAYSIZE(szDeviceName), L"\\\\.\\%c:", byVolLetter); hDevice = CreateFile (szDeviceName, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hDevice == INVALID_HANDLE_VALUE) { return FALSE; }
spq.PropertyId = StorageDeviceProperty; spq.QueryType = PropertyStandardQuery; spq.AdditionalParameters[0] = 0; bSuccess = DeviceIoControl (hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &spq, sizeof(spq), &byBuffer, sizeof(byBuffer), &cbBytesReturned, (LPOVERLAPPED) NULL); STORAGE_DEVICE_DESCRIPTOR *psdp = (STORAGE_DEVICE_DESCRIPTOR *)byBuffer; if (!bSuccess || psdp->BusType != BusTypeUsb) { CloseHandle (hDevice); return FALSE; }
bSuccess = DeviceIoControl (hDevice, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, (LPVOID)&sdn, sizeof(sdn), &cbBytesReturned, (LPOVERLAPPED) NULL); if (!bSuccess) { CloseHandle (hDevice); return FALSE; }
if (sdn.DeviceType == FILE_DEVICE_CD_ROM || sdn.DeviceType == FILE_DEVICE_DVD) { pGuidInferface = (GUID*)&GUID_DEVINTERFACE_CDROM; } else { pGuidInferface = (GUID*)&GUID_DEVINTERFACE_DISK;
}
CloseHandle (hDevice); hDevice = INVALID_HANDLE_VALUE;
hIntDevInfo = SetupDiGetClassDevs (pGuidInferface, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (INVALID_HANDLE_VALUE == hIntDevInfo) return FALSE;
for (dwIndex = 0; ;dwIndex ++) { ZeroMemory(&interfaceData, sizeof(interfaceData)); interfaceData.cbSize = sizeof(interfaceData); bSuccess = SetupDiEnumDeviceInterfaces (hIntDevInfo, NULL, pGuidInferface, dwIndex, &interfaceData); if (!bSuccess) { DWORD dwErrorCode = GetLastError(); if (dwErrorCode == ERROR_NO_MORE_ITEMS) break; }
cbBytesReturned = 0; bSuccess = SetupDiGetDeviceInterfaceDetail (hIntDevInfo, &interfaceData, NULL, 0, &cbBytesReturned, NULL); if ((!bSuccess && GetLastError() != ERROR_INSUFFICIENT_BUFFER) || cbBytesReturned == 0) continue; if (pInterfaceDetailData) pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalFree (pInterfaceDetailData); pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc (LPTR, cbBytesReturned); pInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); ZeroMemory (&deviceInfoData, sizeof(deviceInfoData)); deviceInfoData.cbSize = sizeof(deviceInfoData); bSuccess = SetupDiGetDeviceInterfaceDetail (hIntDevInfo, &interfaceData, pInterfaceDetailData, cbBytesReturned, &cbBytesReturned, &deviceInfoData); if (!bSuccess) continue;
hDev = CreateFile (pInterfaceDetailData->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hDev == INVALID_HANDLE_VALUE) continue;
bSuccess = DeviceIoControl (hDev, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, (LPVOID)&sdnTmp, sizeof(sdnTmp), &cbBytesReturned, (LPOVERLAPPED) NULL); CloseHandle(hDev); if (!bSuccess) continue;
if ((sdn.DeviceType != sdnTmp.DeviceType) || (sdn.DeviceNumber != sdnTmp.DeviceNumber)) { continue; }
configret = CM_Get_Parent (&dnDevInstParent, deviceInfoData.DevInst, 0); if (CR_SUCCESS != configret) break; configret = CM_Get_Device_ID (dnDevInstParent, szDeviceInstanceID, ARRAYSIZE(szDeviceInstanceID), 0); if (CR_SUCCESS != configret) break;
if (pParentInstId) StringCchCopy(pParentInstId, MAX_DEVICE_ID_LEN, szDeviceInstanceID); if (pParentDevName && dwBufSize > 8) { LPWSTR pDevName = pInterfaceDetailData->DevicePath; LPWSTR pfind = StrStrI(pDevName, L"ven_"); if (pfind) { pDevName = pfind + 4; pfind = StrChr(pDevName, '&'); StringCbCopyN(pParentDevName, dwBufSize, pDevName, (pfind-pDevName)*sizeof(WCHAR)); StringCbCat(pParentDevName, dwBufSize, L" "); pDevName = pInterfaceDetailData->DevicePath; } pfind = StrStrI(pDevName, L"prod_"); if (pfind) { pDevName = pfind + 5; pfind = StrChr(pDevName, '&'); StringCbCatN(pParentDevName, dwBufSize, pDevName, (pfind-pDevName)*sizeof(WCHAR)); } pDevName = pParentDevName; do { pfind = StrChr(pDevName, '_'); if (!pfind) break; *pfind = ' '; pDevName = pfind +1; }while(1);
wcsupr(pParentDevName); }
bRet = TRUE; break; }
if (pInterfaceDetailData) LocalFree( pInterfaceDetailData); if (hIntDevInfo ) SetupDiDestroyDeviceInfoList(hIntDevInfo);
return bRet; }
|