盘符
查找所有的盘符,并且只显示可移动磁盘,也就是U盘
/** * find all drivers and show removeable drivers */
void CUkeyKitDlg::FindAllDrivers()
{
CComboBox* Driver=(CComboBox*)GetDlgItem(IDC_COMBO1);
Driver->ResetContent();
DWORD dwNumBytesForDriveStrings;//实际存储驱动器号的字符串长度
HANDLE hHeap;
LPSTR lp;
CString strLogdrive;
//获得实际存储驱动器号的字符串长度
dwNumBytesForDriveStrings=GetLogicalDriveStrings(0,NULL)*sizeof(TCHAR);
//如果字符串不为空,则表示有正常的驱动器存在
if (dwNumBytesForDriveStrings!=0) {
//分配字符串空间
hHeap=GetProcessHeap();
lp=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,dwNumBytesForDriveStrings);
//获得标明所有驱动器的字符串
GetLogicalDriveStrings(HeapSize(hHeap,0,lp),lp);
//将驱动器一个个放到下拉框中
char s[40];
while (*lp!=0) {
UINT nDriveType = GetDriveType(lp);
if (nDriveType == DRIVE_REMOVABLE){
sprintf(s, "%s (%c:) %d MB", "可移动驱动器", lp[0],getTotalSpace(lp) );
//驱动器是这样的 c:\ d:\ 所以只取第一个字符
Driver->AddString(s);
}
lp=_tcschr(lp,0)+1;
}
}
}
磁盘号
通过盘符,获取磁盘号
/****************************************************************************** * Function: get disk's physical number from its drive letter * e.g. C-->0 (C: is on disk0) * input: letter, drive letter * output: N/A * return: Succeed, disk number * Fail, -1 ******************************************************************************/
DWORD CUkeyKitDlg::GetPhysicalDriveFromPartitionLetter(CHAR letter)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL result; // results flag
DWORD readed; // discard results
STORAGE_DEVICE_NUMBER number; //use this to get disk numbers
CHAR path[DISK_PATH_LEN];
sprintf(path, "\\\\.\\%c:", letter);
hDevice = CreateFile(path, // 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);
}
result = DeviceIoControl(
hDevice, // handle to device
IOCTL_STORAGE_GET_DEVICE_NUMBER, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
&number, // output buffer
sizeof(number), // size of output buffer
&readed, // number of bytes returned
NULL // OVERLAPPED structure
);
if (!result) // fail
{
fprintf(stderr, "IOCTL_STORAGE_GET_DEVICE_NUMBER Error: %ld\n", GetLastError());
(void)CloseHandle(hDevice);
return (DWORD)-1;
}
//printf("%d %d %d\n\n", number.DeviceType, number.DeviceNumber, number.PartitionNumber);
(void)CloseHandle(hDevice);
return number.DeviceNumber;
}
/* * get totol space of disk * return : MB */
int CUkeyKitDlg::getTotalSpace(const char* lpRootPathName)
{
unsigned long long available,total,free;
if(GetDiskFreeSpaceEx(lpRootPathName,(ULARGE_INTEGER*)&available,(ULARGE_INTEGER*)&total,(ULARGE_INTEGER*)&free)){
return total>>20;
}else{
return -1;
}
}
自己写的grubist.exe 的界面
由于使用的是1.1版本,所以属于一次开发,使用的可以去这里下载想
http://download.csdn.net/detail/li740207611/9595323