Solved

[D5|NT4+|Winapi]  IOCTL_DISK_GET_DRIVE_LAYOUT and DeviceIoControl

Posted on 2000-03-09
17
765 Views
Last Modified: 2011-08-18
Hi,

Is there someone outta there that knows how to use IOCTL_DISK_GET_DRIVE_LAYOUT with DeviceIoControl to retrieve  the features of each partition. What I want is to get information about partitions on all the disks in my computer.
in fact I'm particularly interessed in getting

    DWORD PartitionNumber;
    BYTE  PartitionType;
    BOOLEAN BootIndicator;
from the PARTITION_INFORMATION structure.
Can someone submit me a sample that shows how to call that ? I know it's only for NT. But this is enought for me.
thanks for your help.
If you know how to do, but don't have a sample, I will give you some more points to write me something.

Thanks in advance.

John.


0
Comment
Question by:jeurk
  • 10
  • 6
17 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 2601394
Something wrong with declaration of IOCTL_DISK_GET_DRIVE_LAYOUT in Delphi5. Do you have an idea what is IOCTL_DISK_GET_DRIVE_LAYOUT equal to (is it 11? I don't remeber;).

Cheers,
Igor.

PS: I have some demo code, but can't compile it :(
0
 
LVL 4

Author Comment

by:jeurk
ID: 2601448
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2603991
Hi jeurk,

The code bellow, I hope, is what you want.


const
 IOCTL_DISK_GET_DRIVE_LAYOUT = 475148; // 7400C (hex).

type

 TPARTITION_INFORMATION = packed record
    StartingOffset  : LARGE_INTEGER ;
    PartitionLength : LARGE_INTEGER ;
    HiddenSectors   : DWORD;
    PartitionNumber : DWORD;
    PartitionType   : BYTE;
    BootIndicator   : BOOLEAN ;
    RecognizedPartition : BOOLEAN;
    RewritePartition    : BOOLEAN;
 end;

 TDRIVE_LAYOUT_INFORMATION = packed record
   PartitionCount : DWORD;
   Signature      : DWORD;
   PartitionEntry : array[0..31] of TPARTITION_INFORMATION;
 end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var HNDL : integer; // hDevice
    INF  : TDRIVE_LAYOUT_INFORMATION;
    RET  : cardinal;  // only for compatibilty
    OVR  : TOverlapped; // only for compatibilty
begin
    FillChar(INF,SizeOf(INF),0); // clean input buffer
    RET:=0; // recived bytes counter
   
//
// "\\.\PHYSICALDRIVEx" - your phisical drive number = x.
// 0 - first, 1-second end so on...
//
HNDL:=CreateFile('\\.\PHYSICALDRIVE0',FILE_SHARE_WRITE,0,nil,OPEN_EXISTING,0,0);
    if HNDL > 0 then
    if DeviceIOControl(HNDL,IOCTL_DISK_GET_DRIVE_LAYOUT,nil,0,@INF,SizeOf(INF),RET,@OVR) then
    begin
//
// do something here.
//
// You can read INF.PartitionCount
// INF.PartitionEntry[0..31] - detailed info about every partition.
//
//
    end;
end;


Cheers,
Igor.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Author Comment

by:jeurk
ID: 2604002
I'll try this out...
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2604029
If something wrong, just let me know.

Regards,
Igor.
0
 
LVL 4

Author Comment

by:jeurk
ID: 2604037
something is wrong ;) I'll tell you in a minute
0
 
LVL 4

Author Comment

by:jeurk
ID: 2604040
I get 'an acces denied' for the DeviceIOControl...
Is this working for you ?
0
 
LVL 4

Author Comment

by:jeurk
ID: 2604041
do i need to be administrator under my computer ? with NT ?
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2604059
Yes, you need. Text bellow is from Delphi help:

Disk Devices
Windows NT: You can use the CreateFile function to open a disk drive or a partition on a disk drive. The function returns a handle to the disk device; that handle can be used with the DeviceIOControl function. The following requirements must be met in order for such a call to succeed:

·      The caller must have administrative privileges for the operation to succeed on a hard disk drive.
.....etc...

for more details see "CreateFile"
0
 
LVL 4

Author Comment

by:jeurk
ID: 2604061
is there a problem with the parameters of
CreateFile('\\.\PHYSICALDRIVE0',FILE_SHARE_WRITE,0,nil,OPEN_EXISTING,0,0);
maybe it should be something like :
HNDL := CreateFile('\\.\PHYSICALDRIVE0', 0, FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);

but even that is not working...

if it is working on your computer I'll try this at home...
But maybe you can give me the right parameters to make it work everywhere ?

Thanks
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2604079
just a moment....
0
 
LVL 4

Author Comment

by:jeurk
ID: 2604091
ok. Let me know.

I tried it as an administrator.
The createfiel is ok but I still have an access denied when calling deviceiocontrol...
0
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
ID: 2604113
Yes, something wrong, I have not "access denied", but INF look like ... EMPTY!
Sorry, I will try to understand what happen....
0
 
LVL 4

Author Comment

by:jeurk
ID: 2604132
That would be cool if you could do this for me. Thank you. Let me know.
0
 
LVL 4

Author Comment

by:jeurk
ID: 2616013
hello,
the solution is (thanks to madshi)
  HNDL := CreateFile('\\.\PHYSICALDRIVE0', GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
  if HNDL <> INVALID_HANDLE_VALUE then
    if DeviceIOControl(HNDL, IOCTL_DISK_GET_DRIVE_LAYOUT, nil, 0, @INF, SizeOf(INF), RET, nil) then

;)

Maybe you can find me something else ?
How can I know for a given partition number which letter it is associated ?

Thanks...
0
 
LVL 4

Author Comment

by:jeurk
ID: 2640888
Hello,
Let's say that I accept the answer to clean stuff around.
What you gave is not the solution to what I asked. Or at least in is not working correctly.
But you gave time for that and I thank you.

Regards.
0
 
LVL 1

Expert Comment

by:mlavenne
ID: 10055680
thanks, it helped me a lot too ;)))))
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question