逻辑盘符问题,谢谢!

Comeon_01 2008-04-03 08:19:17
1、我想通过读取硬盘分区表获得逻辑盘符,类如:C、D、E、F...,请问怎么获得啊?大家帮帮忙吧,谢谢了!
2、Disk32.dll和ReadDisk32是什么意思啊?通过这个文件和函数就可以成功得得到硬盘分区表吗?谢谢大家了!
...全文
122 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cnzdgs 2008-04-03
系统中的盘符是可以任意指定的,你从分区表中根本无法判断出盘符是什么。
应该用DeviceIoControl,代码如下:
	PARTITION_INFORMATION informations;
DWORD bytesReturned;
HANDLE device = CreateFile(_T("\\\\.\\C:"), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
DeviceIoControl(device, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0, &informations, sizeof(informations), &bytesReturned, NULL);
CloseHandle(device);
// informations中可以得到该盘符对应的分区号、开始位置、大小等信息。
回复
Comeon_01 2008-04-03
但是,分区表中只能有4个主分区啊,而扩展分区不是从5号开始的吗?不能说4对应D盘吧?谢谢了!
回复
ahuisafe 2008-04-03
Shell也可以读取
回复
ahuisafe 2008-04-03
4 对应D盘
回复
Comeon_01 2008-04-03
我是想在这段代码里嵌入,所以才问一下的,要不然不可能知道1、2、3、4号磁盘与C、D、E、F的准确对应,谢谢大家了!
回复
Comeon_01 2008-04-03
PARTITION *PartitionTable;//分区表
WORD SectPerTrackTmp=0, TotHeadsTmp=0;
for(i=0;;i++)
{
if(gOSWin95)
{
if(!(DllThunk32)(i+0x80, 0, 0, 1, 1, (LPBYTE) TmpStr1, 0, FALSE))
break;
}
else
{
char TmpStr[26] = "\\\\.\\PHYSICALDRIVE0";
TmpStr[17] = i+'0';
if((hDisk[i+0x80]=CreateFile(TmpStr, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL))==INVALID_HANDLE_VALUE)
break;
}
char TmpStr[100];
PartitionTable = (PARTITION *) (TmpStr1+0x1BE);//获取主分区表
DWORD Tmp1;
if(gOSWin95)
{
if(!(DllThunk32)(i+0x80, 0, 0, 1, 1, (LPBYTE) TmpStr1, 0, FALSE))
break;
}
else
{
ReadFile(hDisk[i+0x80], TmpStr1, 512, &Tmp1, FALSE);
}

wsprintf(TmpStr, "%lu号硬盘", i+1);
Packet->Drive = 0x80+i;
Packet->Cylinder = 0;//0柱面0磁头1扇区
Packet->Head = 0;
Packet->Sector = 1;
Packet->NumSectors=0;
for(int Tmp=0; Tmp<4; Tmp++)//4个
Packet->NumSectors += PartitionTable[Tmp].NumberSectors;
Packet->Type = PART_TABLE;
Packet->RelativeSector = 0;
Packet->NTRelativeSector = 0;
if(gOSWin95)
Packet->Flag = (DllCheckInt13Extension)(0x80+i);
else
Packet->Flag = 0;

NumHards++;

m_Drives.InsertItem(TVIF_TEXT | TVIF_PARAM |TVIF_IMAGE |TVIF_SELECTEDIMAGE, TmpStr, 1, 1, 0, 0, (LPARAM)Packet, hPhysical, TVI_LAST);
Packet++;
}
HTREEITEM hLogical = m_Drives.InsertItem("逻辑硬盘", 0, 0, TVI_ROOT, TVI_LAST);
for(int PhysHard=0; PhysHard<NumHards; PhysHard++)
{
PrevRelSector = MainPrevRelSector = 0;
if(gOSWin95)
{
if(!(DllThunk32)(DrivePacket[PhysHard].Drive, DrivePacket[PhysHard].Cylinder, DrivePacket[PhysHard].Head,
DrivePacket[PhysHard].Sector, 1, (LPBYTE)BufferDir, DrivePacket[PhysHard].NTRelativeSector, DrivePacket[PhysHard].Flag))
{
AfxMessageBox("Partition table not accessible!");
return;
}
}
else
{
__int64 Tmp64 = ((__int64) DrivePacket[PhysHard].NTRelativeSector) * 512;
long TmpVal = Tmp64 & 0xFFFFFFFF;
long TmpValHi = (Tmp64 >> 32);
SetFilePointer(hDisk[DrivePacket[PhysHard].Drive], TmpVal, &TmpValHi, FILE_BEGIN);
TmpVal = 0;
ReadFile(hDisk[DrivePacket[PhysHard].Drive], BufferDir, 512, (DWORD *) &TmpVal, NULL);
if(TmpVal != (512))
{
AfxMessageBox("Partition table not accessible!");
return;
}
}
PartitionTable = (PARTITION *) (BufferDir+0x1BE);
for(i=0; i<4; i++)
{
Packet->Cylinder = PartitionTable->Cylinder;
Packet->Drive = DrivePacket[PhysHard].Drive;
Packet->Flag = DrivePacket[PhysHard].Flag;
Packet->Head = PartitionTable->Head;
Packet->Sector = PartitionTable->Sector;
Packet->NumSectors = PartitionTable->NumberSectors;
Packet->Type = ((PartitionTable->SysInd == PART_EXTENDED) || (PartitionTable->SysInd == PART_DOSX13X)) ? EXTENDED_PART:BOOT_RECORD;
if((PartitionTable->SysInd == PART_EXTENDED) || (PartitionTable->SysInd == PART_DOSX13X))
{
MainPrevRelSector = PartitionTable->RelativeSector;
Packet->NTRelativeSector = MainPrevRelSector;
}
else
{
Packet->NTRelativeSector = MainPrevRelSector + PartitionTable->RelativeSector;
}
if(Packet->Type == EXTENDED_PART)
break;
if(PartitionTable->SysInd == 0)
break;
char TmpStr1[10];
switch(PartitionTable->SysInd)
{
case PART_DOS2_FAT:
strcpy(TmpStr1, "FAT12");
break;
case PART_DOSX13:
case PART_DOS4_FAT:
case PART_DOS3_FAT:
strcpy(TmpStr1, "FAT16");
break;
case PART_DOS32X:
case PART_DOS32:
strcpy(TmpStr1, "FAT32"); //Normal FAT32
break;
case 7:
strcpy(TmpStr1, "NTFS");
break;
default:
strcpy(TmpStr1, "Unknown");
break;
}
if((TmpStr1[0] == 'F') && gOSWin95)
{
if((DllThunk32)(Packet->Drive, Packet->Cylinder, Packet->Head,
Packet->Sector, 1, (LPBYTE)::TmpStr1, Packet->NTRelativeSector, Packet->Flag))
{
BOOT *Boot=(BOOT *)::TmpStr1;
Packet->SectorsPerTrack=Boot->SectorsPerTrack;
Packet->TotalHeads = Boot->NumHeads;
for(int k=0; k<NumHards; k++)
{
DrivePacket[k].SectorsPerTrack = Boot->SectorsPerTrack;
DrivePacket[k].TotalHeads = Boot->NumHeads;
}
}
}
wsprintf(TmpStr, "%u号硬盘1号分区(%s)", PhysHard+1, TmpStr1);
/*wsprintf(TmpStr, "%s Disk 1 on Hard Disk %u", TmpStr1, PhysHard+1);*/
m_Drives.InsertItem(TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE |TVIF_SELECTEDIMAGE, TmpStr, 1, 1, 0, 0, (LPARAM)Packet, hLogical, TVI_LAST);
Packet++;
PartitionTable++;
}
if(i==4)
break;
for(int LogiHard=0; LogiHard<50; LogiHard++)
{
if(Packet->Type == EXTENDED_PART)
{
if(gOSWin95)
{
if(!(DllThunk32)(Packet->Drive, Packet->Cylinder, Packet->Head,
Packet->Sector, 1, (LPBYTE)BufferDir, Packet->NTRelativeSector, Packet->Flag))
{
AfxMessageBox("Extended partition not accessible!");
return;
}
}
else
{
__int64 Tmp64 = ((__int64) Packet->NTRelativeSector) * 512;
long TmpVal = Tmp64 & 0xFFFFFFFF;
long TmpValHi = (Tmp64 >> 32);
SetFilePointer(hDisk[Packet->Drive], TmpVal, &TmpValHi, FILE_BEGIN);
TmpVal = 0;
ReadFile(hDisk[Packet->Drive], BufferDir, 512, (DWORD *) &TmpVal, NULL);
if(TmpVal != 512)
{
AfxMessageBox("Extended partition not accessible!");
return;
}
}
PartitionTable = (PARTITION *) (BufferDir+0x1BE);
for(i=0; i<4; i++)
{
Packet->Cylinder = PartitionTable->Cylinder;
Packet->Drive = DrivePacket[PhysHard].Drive;
Packet->Flag = DrivePacket[PhysHard].Flag;
Packet->Head = PartitionTable->Head;
Packet->NumSectors = PartitionTable->NumberSectors;
Packet->Sector = PartitionTable->Sector;
Packet->RelativeSector = 0;
Packet->Type = ((PartitionTable->SysInd == PART_EXTENDED) || (PartitionTable->SysInd == PART_DOSX13X)) ? EXTENDED_PART:BOOT_RECORD;
if((PartitionTable->SysInd == PART_EXTENDED) || (PartitionTable->SysInd == PART_DOSX13X))
{
PrevRelSector = PartitionTable->RelativeSector;
Packet->NTRelativeSector = PrevRelSector + MainPrevRelSector;
}
else
{
Packet->NTRelativeSector = MainPrevRelSector + PrevRelSector + PartitionTable->RelativeSector;
}
if(Packet->Type == EXTENDED_PART)
break;
if(PartitionTable->SysInd == 0)
break;
switch(PartitionTable->SysInd)
{
case PART_DOS2_FAT:
strcpy(TmpStr1, "FAT12");
break;
case PART_DOSX13:
case PART_DOS4_FAT:
case PART_DOS3_FAT:
strcpy(TmpStr1, "FAT16");
break;
case PART_DOS32X:
case PART_DOS32:
strcpy(TmpStr1, "FAT32"); //Normal FAT32
break;
case 7:
strcpy(TmpStr1, "NTFS");
break;
default:
strcpy(TmpStr1, "Unknown");
break;
}
wsprintf(TmpStr, "%u号硬盘%u号分区(%s)", PhysHard+1, LogiHard+2, TmpStr1);
/*wsprintf(TmpStr, "%s Disk %u on Hard Disk %u", TmpStr1, LogiHard+2, PhysHard+1);*/
m_Drives.InsertItem(TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE |TVIF_SELECTEDIMAGE, TmpStr, 1, 1, 0, 0, (LPARAM)Packet, hLogical, TVI_LAST);
Packet++;
PartitionTable++;
}
if(i==4)
break;
}
}
}
回复
tccqs 2008-04-03
up and mark,,
回复
marine8086 2008-04-03
读个盘符,犯不上读分区表吧?

DWORD GetLogicalDrives(void);
或者
DWORD GetLogicalDriveStrings(
DWORD nBufferLength,
LPTSTR lpBuffer
);

具体看MSDN
回复
scq2099yt 2008-04-03
UP1楼
回复
相关推荐
发帖
硬件/系统
创建于2007-09-28

2594

社区成员

VC/MFC 硬件/系统
申请成为版主
帖子事件
创建了帖子
2008-04-03 08:19
社区公告
暂无公告