Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1695578
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2011-04-12 17:29:46

//获取指定盘符的分区号,如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
阅读(1405) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~