Get Device Layout

Hi,

  1. I am using the SDK IOCTL_DISK_GET_DEVICE_LAYOUT which gives the partition
    information of a physical drive. This is giving the partition count to be 24
    on my machine which has actually 6 partitions. Am I doing some wrong thing
    or is it misbehaving ??
  2. How can I create a partition’s device handle using CreateFile. I want
    to find out the information about partition 0 on every disk. The device
    name for this is /Device/Harddisk0/partition0. How can I create a handle for
    this using CreateFile.

With regards,
Nagesh Bhattu
The code for the first question is
char *ll=new char[SIZ];
hDevice = CreateFile(“\\.\PHYSICALDRIVE0”,GENERIC_READ,FILE_SHARE_READ
| FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
std::cout << "Handle it self is failed " << std::endl;
return (0);
}
bResult = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
NULL, 0, ll, SIZ, &junk, (LPOVERLAPPED) NULL);
pdg=(PDRIVE_LAYOUT_INFORMATION_EX) ll;
CloseHandle(hDevice);

I’m guessing that you are using Basic volumes and that you have an extended
partition and 1 primary partition. On the extended partition you have 5
logical drives. Giving you a total of 6 “partitions.”

To understand the output of IOCTL_DISK_GET_DRIVE_LAYOUT, I would look into
the layout of the master boot record (MBR) and the extended boot record
(EBR). You can find a good description on www.microsoft.com/technet. It
essentially works this way: The disk’s MBR contains 4 partition entries.
In your case (if I’m correct in guessing your configuration above) one of
the entries will point to the extended partition and one to the primary
partition. Each logical drive on the extended partition contains an EBR,
which also contains 4 entries. Entry 1 points to the beginning of the
current logical drive. Entry 2 points to the EBR of the next logical drive.
Entries 3 and 4 are not used. So if you have a disk with 1 primary
partition and an extended partition containing 5 logical drives you will
have 4 partition entries in the MBR (2 of which are empty) and 4 partition
entries for each of the 5 logical drives (or 20) giving you a total of 24
partition entries. The partition information array that is returned along
with the partition count will resemble that layout. In other words the
first 4 entries in the array will resemble the MBR with the first 2 entries
describing the extended and the primary partition and then next two elements
being empty. Each subsequent set of 4 elements in the array will represent
the EBR for each subsequent logical drive. What’s odd is that if you add 1
or 2 more primary partitions the IOCTL will still return 24 partitions. By
adding additional primary partitions you are just filling in empty partition
entries in the MBR and therefor the 3rd and 4th elements in the array
returned by the IOCTL will no longer be empty, but will describe the new
partions.

The bottom line is the the term PartitionCount is misleading. It is really
a partition entry count.

Hope this helps.

David

“Nagesh Bhattu” wrote in message
news:xxxxx@ntdev…
>
> Hi,
> 1. I am using the SDK IOCTL_DISK_GET_DEVICE_LAYOUT which gives the
partition
> information of a physical drive. This is giving the partition count to be
24
> on my machine which has actually 6 partitions. Am I doing some wrong thing
> or is it misbehaving ??
> 2. How can I create a partition’s device handle using CreateFile. I
want
> to find out the information about partition 0 on every disk. The device
> name for this is /Device/Harddisk0/partition0. How can I create a handle
for
> this using CreateFile.
>
> With regards,
> Nagesh Bhattu
> The code for the first question is
> char *ll=new char[SIZ];
> hDevice =
CreateFile(“\\.\PHYSICALDRIVE0”,GENERIC_READ,FILE_SHARE_READ
> | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
> if (hDevice == INVALID_HANDLE_VALUE) {
> std::cout << "Handle it self is failed " << std::endl;
> return (0);
> }
> bResult = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
> NULL, 0, ll, SIZ, &junk, (LPOVERLAPPED) NULL);
> pdg=(PDRIVE_LAYOUT_INFORMATION_EX) ll;
> CloseHandle(hDevice);
>
>
>
>

Thanks david,
This has solved the problem and your assumptions about the configuration
of partitions is infact correct. Is there any book which talks about these
details ?

  • Nagesh Bhattu

“David Chimitt” wrote in message
news:xxxxx@ntdev…
>
> I’m guessing that you are using Basic volumes and that you have an
extended
> partition and 1 primary partition. On the extended partition you have 5
> logical drives. Giving you a total of 6 “partitions.”
>
> To understand the output of IOCTL_DISK_GET_DRIVE_LAYOUT, I would look into
> the layout of the master boot record (MBR) and the extended boot record
> (EBR). You can find a good description on www.microsoft.com/technet. It
> essentially works this way: The disk’s MBR contains 4 partition entries.
> In your case (if I’m correct in guessing your configuration above) one of
> the entries will point to the extended partition and one to the primary
> partition. Each logical drive on the extended partition contains an EBR,
> which also contains 4 entries. Entry 1 points to the beginning of the
> current logical drive. Entry 2 points to the EBR of the next logical
drive.
> Entries 3 and 4 are not used. So if you have a disk with 1 primary
> partition and an extended partition containing 5 logical drives you will
> have 4 partition entries in the MBR (2 of which are empty) and 4 partition
> entries for each of the 5 logical drives (or 20) giving you a total of 24
> partition entries. The partition information array that is returned along
> with the partition count will resemble that layout. In other words the
> first 4 entries in the array will resemble the MBR with the first 2
entries
> describing the extended and the primary partition and then next two
elements
> being empty. Each subsequent set of 4 elements in the array will
represent
> the EBR for each subsequent logical drive. What’s odd is that if you add
1
> or 2 more primary partitions the IOCTL will still return 24 partitions.
By
> adding additional primary partitions you are just filling in empty
partition
> entries in the MBR and therefor the 3rd and 4th elements in the array
> returned by the IOCTL will no longer be empty, but will describe the new
> partions.
>
> The bottom line is the the term PartitionCount is misleading. It is
really
> a partition entry count.
>
> Hope this helps.
>
> David
>
>
> “Nagesh Bhattu” wrote in message
> news:xxxxx@ntdev…
> >
> > Hi,
> > 1. I am using the SDK IOCTL_DISK_GET_DEVICE_LAYOUT which gives the
> partition
> > information of a physical drive. This is giving the partition count to
be
> 24
> > on my machine which has actually 6 partitions. Am I doing some wrong
thing
> > or is it misbehaving ??
> > 2. How can I create a partition’s device handle using CreateFile. I
> want
> > to find out the information about partition 0 on every disk. The device
> > name for this is /Device/Harddisk0/partition0. How can I create a handle
> for
> > this using CreateFile.
> >
> > With regards,
> > Nagesh Bhattu
> > The code for the first question is
> > char *ll=new char[SIZ];
> > hDevice =
> CreateFile(“\\.\PHYSICALDRIVE0”,GENERIC_READ,FILE_SHARE_READ
> > | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
> > if (hDevice == INVALID_HANDLE_VALUE) {
> > std::cout << "Handle it self is failed " << std::endl;
> > return (0);
> > }
> > bResult = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
> > NULL, 0, ll, SIZ, &junk, (LPOVERLAPPED) NULL);
> > pdg=(PDRIVE_LAYOUT_INFORMATION_EX) ll;
> > CloseHandle(hDevice);
> >
> >
> >
> >
>
>
>
>

Here’s the more complete link that I gave earlier. It takes you straight to
the section on Basic and Dynamic disks. Glad to have helped.

http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechn
ol/windows2000serv/Default.asp

David Chimitt
Unisys Corporation

“Nagesh Bhattu” wrote in message
news:xxxxx@ntdev…
>
> Thanks david,
> This has solved the problem and your assumptions about the
configuration
> of partitions is infact correct. Is there any book which talks about these
> details ?
> - Nagesh Bhattu
>
> “David Chimitt” wrote in message
> news:xxxxx@ntdev…
> >
> > I’m guessing that you are using Basic volumes and that you have an
> extended
> > partition and 1 primary partition. On the extended partition you have 5
> > logical drives. Giving you a total of 6 “partitions.”
> >
> > To understand the output of IOCTL_DISK_GET_DRIVE_LAYOUT, I would look
into
> > the layout of the master boot record (MBR) and the extended boot record
> > (EBR). You can find a good description on www.microsoft.com/technet.
It
> > essentially works this way: The disk’s MBR contains 4 partition
entries.
> > In your case (if I’m correct in guessing your configuration above) one
of
> > the entries will point to the extended partition and one to the primary
> > partition. Each logical drive on the extended partition contains an
EBR,
> > which also contains 4 entries. Entry 1 points to the beginning of the
> > current logical drive. Entry 2 points to the EBR of the next logical
> drive.
> > Entries 3 and 4 are not used. So if you have a disk with 1 primary
> > partition and an extended partition containing 5 logical drives you will
> > have 4 partition entries in the MBR (2 of which are empty) and 4
partition
> > entries for each of the 5 logical drives (or 20) giving you a total of
24
> > partition entries. The partition information array that is returned
along
> > with the partition count will resemble that layout. In other words the
> > first 4 entries in the array will resemble the MBR with the first 2
> entries
> > describing the extended and the primary partition and then next two
> elements
> > being empty. Each subsequent set of 4 elements in the array will
> represent
> > the EBR for each subsequent logical drive. What’s odd is that if you
add
> 1
> > or 2 more primary partitions the IOCTL will still return 24 partitions.
> By
> > adding additional primary partitions you are just filling in empty
> partition
> > entries in the MBR and therefor the 3rd and 4th elements in the array
> > returned by the IOCTL will no longer be empty, but will describe the new
> > partions.
> >
> > The bottom line is the the term PartitionCount is misleading. It is
> really
> > a partition entry count.
> >
> > Hope this helps.
> >
> > David
> >
> >
> > “Nagesh Bhattu” wrote in message
> > news:xxxxx@ntdev…
> > >
> > > Hi,
> > > 1. I am using the SDK IOCTL_DISK_GET_DEVICE_LAYOUT which gives the
> > partition
> > > information of a physical drive. This is giving the partition count to
> be
> > 24
> > > on my machine which has actually 6 partitions. Am I doing some wrong
> thing
> > > or is it misbehaving ??
> > > 2. How can I create a partition’s device handle using CreateFile. I
> > want
> > > to find out the information about partition 0 on every disk. The
device
> > > name for this is /Device/Harddisk0/partition0. How can I create a
handle
> > for
> > > this using CreateFile.
> > >
> > > With regards,
> > > Nagesh Bhattu
> > > The code for the first question is
> > > char *ll=new char[SIZ];
> > > hDevice =
> > CreateFile(“\\.\PHYSICALDRIVE0”,GENERIC_READ,FILE_SHARE_READ
> > > | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
> > > if (hDevice == INVALID_HANDLE_VALUE) {
> > > std::cout << "Handle it self is failed " << std::endl;
> > > return (0);
> > > }
> > > bResult = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
> > > NULL, 0, ll, SIZ, &junk, (LPOVERLAPPED) NULL);
> > > pdg=(PDRIVE_LAYOUT_INFORMATION_EX) ll;
> > > CloseHandle(hDevice);
> > >
> > >
> > >
> > >
> >
> >
> >
> >
>
>
>
>