DWORD dwFlagsAndAttributes,
__in HANDLE hTemplateFile
);
lpFileName为需要打开设备的名称,对于磁盘来说,可能为以下几种形式:
对于物理驱动器x,形式为 \\.\PhysicalDriveX ,编号从0开始,例如
名称
含义
\\.\PhysicalDrive0
打开第一个物理驱动器
\\.\PhysicalDrive2
打开第三个物理驱动器
对于逻辑分区(卷),形式为 \\.\X: ,例如
名称
含义
\\.\A:
打开A盘(软驱)
\\.\C:
打开C盘(磁盘逻辑分区)
最后复制一段MSDN上的示例代码作为本节的结束,该示例获取磁盘的详细信息(包括柱面、磁道、扇区等统计信息)并打印出来。
/* The code of interest is in the subroutine GetDriveGeometry. The
code in main shows how to interpret the results of the call. */
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL bResult; // results flag
DWORD junk; // discard results
hDevice = CreateFile(TEXT("\\\\.\\PhysicalDrive0"), // drive
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}
bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer
pdg, sizeof(*pdg), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED) NULL); // synchronous I/O
CloseHandle(hDevice);
return (bResult);
}
int main(int argc, char *argv[])
{
DISK_GEOMETRY pdg; // disk drive geometry structure
BOOL bResult; // generic results flag
ULONGLONG DiskSize; // size of the drive, in bytes
bResult = GetDriveGeometry (&pdg);
if (bResult)
{
printf("Cylinders = %I64d ", pdg.Cylinders);
printf("Tracks/cylinder = %ld ", (ULONG) pdg.TracksPerCylinder);
printf("Sectors/track = %ld ", (ULONG) pdg.SectorsPerTrack);
printf("Bytes/sector = %ld ", (ULONG) pdg.BytesPerSector);
DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
printf("Disk size = %I64d (Bytes) = %I64d (Gb) ", DiskSize,
DiskSize / (1024 * 1024 * 1024));
}
else
{
printf ("GetDriveGeometry failed. Error %ld. ", GetLastError ());
}
return ((int)bResult);
}
亿恩科技地址(ADD):郑州市黄河路129号天一大厦608室 邮编(ZIP):450008 传真(FAX):0371-60123888
联系:亿恩小凡
QQ:89317007
电话:0371-63322206 本文出自:亿恩科技【www.enkj.com】
服务器租用/服务器托管中国五强!虚拟主机域名注册顶级提供商!15年品质保障!--亿恩科技[ENKJ.COM]
|