Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / September 2008



Tip: Looking for answers? Try searching our database.

problem using DeviceIoControl

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gianluca - 25 Sep 2008 15:01 GMT
I use DeviceIoControl to create four primary NTFS partitions on a HardDisk.
Partitions are created properly if I use a new Hard Disk,  but if I use an
HardDisk already formatted and erased,
often drive letters are assigned in revers order.
Why does this happens?

I call DeviceIoControl using these control codes:

DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, ...)

DeviceIoControl(hDevice, IOCTL_DISK_CREATE_DISK, ...)    to initialize disk

DeviceIoControl(hDevice, IOCTL_DISK_UPDATE_PROPERTIES, ...)

DeviceIoControl(hDevice, IOCTL_DISK_SET_DRIVE_LAYOUT_EX,  driveLayout,...)
(specifiing 4 partitions)

DeviceIoControl(hDevice, IOCTL_DISK_UPDATE_PROPERTIES, ...).

Regards,
Gianluca
Thorsten Albers - 26 Sep 2008 12:13 GMT
Gianluca <g.facca@teledata-i.com> schrieb im Beitrag
<ua3g8cxHJHA.3960@TK2MSFTNGP04.phx.gbl>...
> I use DeviceIoControl to create four primary NTFS partitions on a HardDisk.
> Partitions are created properly if I use a new Hard Disk,  but if I use an
[quoted text clipped - 4 lines]
> DeviceIoControl(hDevice, IOCTL_DISK_SET_DRIVE_LAYOUT_EX,  driveLayout,...)
> (specifiing 4 partitions)

Without more code it will be very difficult to tell why it happens...

Signature

----------------------------------------------------------------------
Thorsten Albers                               albers(a)uni-freiburg.de
----------------------------------------------------------------------

Jeremiah D. Seitz - 26 Sep 2008 14:47 GMT
>I use DeviceIoControl to create four primary NTFS partitions on a HardDisk.
>Partitions are created properly if I use a new Hard Disk,  but if I use an
>HardDisk already formatted and erased,
>often drive letters are assigned in revers order.

In the above line you say "often". Does this mean that it doesn't
happen all the time?

>DeviceIoControl(hDevice, IOCTL_DISK_SET_DRIVE_LAYOUT_EX,  driveLayout,...)
>(specifiing 4 partitions)

Look at the original layout and see in what order the partitions are
returned. That may have something to do with it.

You may be able to programmatically assign/swap the drive letters
after the partitions are created. A quick scan of MSDN failed to turn
anything up, but I'm only on my first gallon of coffee. :)

>Regards,
>Gianluca

    J.
   Jeremiah D. Seitz
   Omega Techware
   http://www.omegatechware.net
Gianluca - 26 Sep 2008 15:56 GMT
>>I use DeviceIoControl to create four primary NTFS partitions on a
>>HardDisk.
[quoted text clipped - 22 lines]
>    Omega Techware
>    http://www.omegatechware.net

Yes, It do not happen all the time, some time it assign letters in right
order.

the code I  use to create partitions on disk is:

This is the code I use to crate partitions.

 char str[128];
 wsprintf(str,"\\\\.\\PhysicalDrive%d", i);
 hDevice = CreateFile( ,"\\\\.\\PhysicalDrive1, // drive to open
       GENERIC_READ | GENERIC_WRITE,
       0,
       NULL,
       OPEN_EXISTING,
       0,
       NULL );

    LARGE_INTEGER dimDiscoD;
    LARGE_INTEGER dimDiscoE;
    LARGE_INTEGER dimDiscoF;
    LARGE_INTEGER dimDiscoG;
    DISK_GEOMETRY_EX pDiskGeometryEx;

     DWORD dwBytesReturned;
    BOOL r = DeviceIoControl(
           hDevice,
           IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
           NULL,
           0,
           &pDiskGeometryEx,
           sizeof(pDiskGeometryEx),
           &dwBytesReturned,
           NULL
          );

      dimDiscoG.QuadPart = DIM_DISCOG;
     dimDiscoD.QuadPart = (( pDiskGeometryEx.DiskSize.QuadPart -
dimDiscoG.QuadPart )/6) * 4;
     dimDiscoE.QuadPart = dimDiscoD.QuadPart/4;
     dimDiscoF.QuadPart = dimDiscoE.QuadPart;

     CREATE_DISK pCreateDisk;
     ZeroMemory(&pCreateDisk,sizeof(CREATE_DISK));

     DWORD dwBytesReturned;
     pCreateDisk.PartitionStyle =  PARTITION_STYLE_MBR;
     pCreateDisk.Mbr.Signature = 0xA4B57310;

    DeviceIoControl(
          hDevice,
          IOCTL_DISK_CREATE_DISK,  // operation to perform
          (LPVOID)&pCreateDisk,
          sizeof(pCreateDisk),
          NULL,
          0,
          &dwBytesReturned,
          (LPOVERLAPPED) NULL)

      DWORD junk1;
      DeviceIoControl(
          hDevice,
          IOCTL_DISK_UPDATE_PROPERTIES,
          NULL,
          0,
          NULL,
          0,
          &junk1,
          NULL
          );

      int SectorSize=pDiskGeometryEx.Geometry.BytesPerSector;
      DWORD szNewLayout = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+4*sizeof
PARTITION_INFORMATION_EX);
      DRIVE_LAYOUT_INFORMATION_EX *dl = (DRIVE_LAYOUT_INFORMATION_EX*) new
BYTE[szNewLayout];
      ZeroMemory(dl,szNewLayout);

      dl->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;
      dl->PartitionEntry[0].StartingOffset.QuadPart = 32256i64; // = 63 *
512
      dl->PartitionEntry[0].PartitionLength.QuadPart = dimDiscoD.QuadPart;
      dl->PartitionEntry[0].PartitionNumber = 1;
      dl->PartitionEntry[0].RewritePartition = TRUE;
      dl->PartitionEntry[0].Mbr.PartitionType = 0x07;// PARTITION_IFS (NTFS
partition or logical drive)
      //dl->PartitionEntry[0].Mbr.BootIndicator = TRUE;
      dl->PartitionEntry[0].Mbr.RecognizedPartition = 1;
      dl->PartitionEntry[0].Mbr.HiddenSectors=32256/SectorSize;

      dl->PartitionEntry[1].PartitionStyle=PARTITION_STYLE_MBR;
      dl->PartitionEntry[1].StartingOffset.QuadPart= 32256i64 +
dimDiscoD.QuadPart;
      dl->PartitionEntry[1].PartitionLength.QuadPart = dimDiscoE.QuadPart;
      dl->PartitionEntry[1].PartitionNumber=2;
      dl->PartitionEntry[1].RewritePartition = TRUE;
      dl->PartitionEntry[1].Mbr.PartitionType = 0x07;
      dl->PartitionEntry[1].Mbr.RecognizedPartition = 1;
      dl->PartitionEntry[1].Mbr.HiddenSectors = (32256i64 +
dimDiscoD.QuadPart)/SectorSize;  //SectorSize;

      dl->PartitionEntry[2].PartitionStyle=PARTITION_STYLE_MBR;
      dl->PartitionEntry[2].StartingOffset.QuadPart= 32256i64 +
dimDiscoD.QuadPart + dimDiscoE.QuadPart;
      dl->PartitionEntry[2].PartitionLength.QuadPart = dimDiscoF.QuadPart;
      dl->PartitionEntry[2].PartitionNumber=3;
      dl->PartitionEntry[2].RewritePartition = TRUE;
      dl->PartitionEntry[2].Mbr.PartitionType = 0x07;
      dl->PartitionEntry[2].Mbr.RecognizedPartition = 1;
      dl->PartitionEntry[2].Mbr.HiddenSectors = (32256i64 +
dimDiscoD.QuadPart + dimDiscoE.QuadPart)/SectorSize;  //SectorSize;

      dl->PartitionEntry[3].PartitionStyle=PARTITION_STYLE_MBR;
      dl->PartitionEntry[3].StartingOffset.QuadPart= 32256i64 +
dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart;
      dl->PartitionEntry[3].PartitionLength.QuadPart =
pDiskGeometryEx.DiskSize.QuadPart -
       (dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart );
      dl->PartitionEntry[3].PartitionNumber=4;
      dl->PartitionEntry[3].RewritePartition = TRUE;
      dl->PartitionEntry[3].Mbr.PartitionType = 0x07;
      dl->PartitionEntry[3].Mbr.RecognizedPartition = 1;
      dl->PartitionEntry[3].Mbr.HiddenSectors = (32256i64 +
dimDiscoD.QuadPart + dimDiscoE.QuadPart + dimDiscoF.QuadPart )/SectorSize;
//SectorSize;

      dl->PartitionStyle = PARTITION_STYLE_MBR;
      dl->PartitionCount = 4;// specify AT LEAST 4 partitions!!!
      dl->Mbr.Signature = 0xA4B57310; //

      DeviceIoControl(
            hDevice,  // device we are querying
            IOCTL_DISK_SET_DRIVE_LAYOUT_EX,  // operation to perform
            dl,
            szNewLayout,
            NULL,
            0, // no input buffer, so pass zero
            &junk, // discard count of bytes returned
            (LPOVERLAPPED) NULL)  // synchronous I/O
      )
       // update disk properties
       DWORD junk;
       DeviceIoControl(
            hDevice,
            IOCTL_DISK_UPDATE_PROPERTIES,
            NULL,
            0,
            NULL,
            0,
            &junk,
            NULL
           );

       CloseHandle(hDevice);
       delete dl;

Regards,
Gianluca
Jeremiah D. Seitz - 27 Sep 2008 00:01 GMT
>Yes, It do not happen all the time, some time it assign letters in right
>order.

That *is* puzzling ...

>This is the code I use to crate partitions.

8< SNIP >8

I'd be remiss if I didn't point out that this is a VB newsgroup. You
might have better luck in a C group.

In the meantime, have you tried setting RecognizedPartition to 0? The
MSDN docs aren't very clear on the actual effect of this member.
Perhaps setting it to 0 would prevent the volumes from being
automatically mounted? If so, then you could possibly take control of
the actual drive letter assignments.

Offhand, I wouldn't know where to look for confirmation, but ...

TrueCrypt performs both volume creation and drive letter assignment,
and it's open source. Being written in C, it should be right up your
alley.

Out of curiosity, how did you arrive at your MBR signature?

>Regards,
>Gianluca

   
   Jeremiah D. Seitz
   Omega Techware
   http://www.omegatechware.net
Gianluca - 29 Sep 2008 08:15 GMT
>>Yes, It do not happen all the time, some time it assign letters in right
>>order.
[quoted text clipped - 28 lines]
>    Omega Techware
>    http://www.omegatechware.net

Thank You for your answer.

I took signature from a code I found on Internet.
I used that number as I read somewhere that it can be a random number.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.