这一系列文章是根据cutebunny 的BLOG “windows的磁盘操作” 写成的,主要是部分修改原作中的代码,使之兼容Unicode和Windows 7 64bit. 原文可以在下面的网址找到
http://cutebunny.blog.51cto.com 。 本文是参考 “windows的磁盘操作之九——区分本地磁盘与移动硬盘”写成。
程序实现了区分当前存储设备是硬盘还是移动硬盘的功能。
// gettype.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" /****************************************************************************** * Function: get the bus type of an disk * input: drive name (c:) * output: bus type * return: Succeed, 0 * Fail, -1 ******************************************************************************/ DWORD GetDriveTypeByBus(const TCHAR *drive, WORD *type) { 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; hDevice = CreateFile( drive, // 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()); return DWORD(-1); } query.PropertyId = StorageDeviceProperty; query.QueryType = PropertyStandardQuery; pDevDescHeader = (STORAGE_DESCRIPTOR_HEADER *)malloc(sizeof(STORAGE_DESCRIPTOR_HEADER)); if (NULL == pDevDescHeader) { return (DWORD)-1; } 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); return DWORD(-1); } devDescLength = pDevDescHeader->Size; pDevDesc = (STORAGE_DEVICE_DESCRIPTOR *)malloc(devDescLength); if (NULL == pDevDesc) { free(pDevDescHeader); return (DWORD)-1; } 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); return DWORD(-1); } //printf("%d\n", pDevDesc->BusType); *type = (WORD)pDevDesc->BusType; free(pDevDescHeader); free(pDevDesc); (void)CloseHandle(hDevice); return 0; } int _tmain(int argc, _TCHAR* argv[]) { WORD t; TCHAR *Driver; Driver=L"\\\\.\\c:"; GetDriveTypeByBus(Driver,&t); fprintf(stdout, "%x\n",t); Driver=L"\\\\.\\d:"; GetDriveTypeByBus(Driver,&t); fprintf(stdout, "%x\n",t); getchar(); return 0; }
运行结果如下 (注意,运行时需要管理员的权限)
表示一个是 BusTypeAta 另一个是 BusTypeUsb,前者是固定在机器中的硬盘,后者是U盘。
源代码以及可执行程序
参考:
1.cutebunny 的BLOG “windows的磁盘操作” 可以在这里下载 WindowsDisk