//获取指定盘符的分区号,如C盘是1:1
BOOL COneKeyDlg::GetPartitionNumber(LPCTSTR lpszDiskName, CString &szParitionNumber)
{
CString szDiskName = CString("\\\\.\\") + lpszDiskName;
HANDLE hDrv = CreateFile(szDiskName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if(!hDrv)
{
//cout << "Error Can not open the Driver " << GetLastError();
return FALSE;
}
VOLUME_DISK_EXTENTS vde;
DWORD readed;
PARTITION_INFORMATION pinfo;
DWORD lpBytesReturned;
BOOL ret = DeviceIoControl(
(HANDLE) hDrv,
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
NULL,
0,
&vde,
sizeof(vde),
&readed,
NULL);
BOOL bSuccess = DeviceIoControl(
(HANDLE) hDrv, // handle to a partition
IOCTL_DISK_GET_PARTITION_INFO, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
&pinfo, // output buffer
sizeof(pinfo), // size of output buffer
&lpBytesReturned, // number of bytes returned
NULL // OVERLAPPED structure
);
CloseHandle(hDrv);
if(!ret || !bSuccess)
{
//cout << "ERROR " << GetLastError();
return FALSE;
}
szParitionNumber.Format(_T("%d:%d"), vde.Extents->DiskNumber+1, pinfo.PartitionNumber);
return TRUE;
}
参考:
http://hi.baidu.com/shenli1979/blog/item/ea491b30de6a909ea9018ea3.html
http://blog.csdn.net/felixz/archive/2006/05/17/742853.aspx
阅读(1393) | 评论(0) | 转发(1) |