挂载盘符

挂载盘符

#include <iostream>
#include <windows.h>
using namespace  std;
int GetPartitionCount(int index,LPVOID OutBuffer) {
	DWORD iret = 0;
	WCHAR FileName[260] = {0}; 
	wsprintfW(FileName, L"\\\\.\\PhysicalDrive%d", index);
	HANDLE hFile = CreateFileW(FileName, 0x80000000, 3u, 0, 3u, 0, 0);
	if (hFile != (HANDLE)-1) {
		DWORD BytesReturned = 0; 
		if (DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, 0, 0, OutBuffer, 0x2000, &BytesReturned, 0)) {
			iret = *((DWORD*)OutBuffer + 1);// >> 2; 
		} 
		CloseHandle(hFile);
	}
	return iret;
}
BOOL GetDeviceNumber(STORAGE_DEVICE_NUMBER* pDeviceNumber, int pt) {
	BOOL bRet = 0;
	if (!pDeviceNumber)
		return 0;
	WCHAR FileName[260] = { 0 };
	memset(FileName, 0, sizeof(FileName));
	wsprintfW(FileName, L"\\\\?\\%c:", pt);  //设备路径
	HANDLE hFile = CreateFileW(FileName, 0x80000000, 3u, 0, 3u, 0x80u, 0);
	if (hFile != (HANDLE)-1) {
		DWORD BytesReturned = 0;
		STORAGE_DEVICE_NUMBER OutBuffer = { 0 };
		bRet = DeviceIoControl(hFile, IOCTL_STORAGE_GET_DEVICE_NUMBER, 0, 0, &OutBuffer, 0xCu, &BytesReturned, 0);
		if (bRet) {
			*pDeviceNumber = OutBuffer;
			bRet = 1;
		}
		CloseHandle(hFile);
	}
	return bRet;
}

int PartitionIsExisit(int di, int pi) {
	DWORD LogicalDrives = GetLogicalDrives();
	int pt = 0;
	while (1) {
		if (((1 << pt) & LogicalDrives) != 0) {
			STORAGE_DEVICE_NUMBER sdn = { 0 };
			if (GetDeviceNumber(&sdn, pt + 65)) {
				if (sdn.DeviceNumber == di && sdn.PartitionNumber == pi)
					break;
			}
		}
		if (++pt >= 32)
			retur
剩余50%内容付费后可查看
打赏
关键词: 挂载 盘符

0 评论

发表评论