f



Disk Imaging Utility

When I first started working on this project, I did not think this
would be possible with Visual Basic, but to my surprise, I found enough
information to create a nice imaging tool. The only issue, it only
works with disk volumes. Although, this is still very useful, I would
like to mimic the operation of commercial disk imaging utilities like
Ghost. The commercial products can perform an image or disk to disk
copy of an entire disk and all volumes on the disk. I believe I can use
the following API and control codes, among others, to perform the tasks
I am looking for, put can not find VB examples of their use.

API:
DeviceIoControl

Control Codes:
IOCTL_DISK_CREATE_DISK
IOCTL_DISK_DELETE_DRIVE_LAYOUT
IOCTL_DISK_SET_PARTITION_INFO
IOCTL_DISK_SET_DRIVE_LAYOUT
IOCTL_DISK_UPDATE_PROPERTIES

The first issue I would like tackle is to create a disk using
IOCTL_DISK_CREATE_DISK. Here is what I have at this point, but the disk
is not created. I have tried this with a RAW disk as well as an
initialized, partitioned, and formated disks as well.

<code>
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_SHARE_READ As Long = &H1
Public Const FILE_SHARE_WRITE As Long = &H2
Public Const OPEN_EXISTING As Long = 3
Public Const INVALID_HANDLE_VALUE As Long = -1
Public Const PARTITION_FAT32 = &HB
Public Const IOCTL_DISK_CREATE_DISK = &H7C058

Public Enum PARTITION_STYLE
  PARTITION_STYLE_MBR
  PARTITION_STYLE_GPT
  PARTITION_STYLE_RAW
End Enum

Public Type GUID
  Data1 As Long
  Data2 As Integer
  Data3 As Integer
  Data4(0 To 7) As Byte
End Type

Public Type CREATE_DISK_GPT
  DiskId As GUID
  MaxPartitionCount As Long
End Type

Public Type CREATE_DISK_MBR
  Signature As Long
End Type

Public Type CREATE_DISK
  PartitionStyle As PARTITION_STYLE
  PartitionMBRType As CREATE_DISK_MBR
  PartitionGPTType As CREATE_DISK_GPT
End Type

Public Declare Function CreateFile Lib "kernel32" _
   Alias "CreateFileA" _
  (ByVal lpFileName As String, _
   ByVal dwDesiredAccess As Long, _
   ByVal dwShareMode As Long, _
   ByVal lpSecurityAttributes As Any, _
   ByVal dwCreationDisposition As Long, _
   ByVal dwFlagsAndAttributes As Long, _
   ByVal hTemplateFile As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" _
   (ByVal hObject As Long) As Long

Public Declare Function DeviceIoControl Lib "kernel32" _
   (ByVal hdevice As Long, _
   ByVal dwIoControlCode As Long, _
   lpInBuffer As Any, ByVal _
   nInBufferSize As Long, _
   lpOutBuffer As Any, _
   ByVal nOutBufferSize As Long, _
   lpBytesReturned As Long, _
   lpOverlapped As Any) As Long

  Dim cd As CREATE_DISK
  Dim j As Integer
  Dim hdevice As Long
  Dim bytesreturned As Long
  Dim PhysicalDrive As Long
  Dim retval As Long

  PhysicalDrive = 0
  hdevice = CreateFile("\\.\PHYSICALDRIVE" & CStr(PhysicalDrive), _
                            GENERIC_READ Or GENERIC_WRITE, _
                            FILE_SHARE_READ Or FILE_SHARE_WRITE, _
                            ByVal 0&, _
                            OPEN_EXISTING, _
                            0&, 0&)

  If hdevice <> INVALID_HANDLE_VALUE Then
    cd.PartitionStyle = PARTITION_STYLE_MBR
    cd.PartitionMBRType.Signature = PARTITION_FAT32
    cd.PartitionGPTType.DiskId.Data1 = 0
    cd.PartitionGPTType.DiskId.Data2 = 0
    cd.PartitionGPTType.DiskId.Data3 = 0
    For j = 0 To 7
    cd.PartitionGPTType.DiskId.Data4(j) = 0
    Next j
    cd.PartitionGPTType.MaxPartitionCount = 0

    retval = DeviceIoControl(hdevice, _
                        IOCTL_DISK_CREATE_DISK, _
                        cd, _
                        Len(cd), _
                        ByVal 0&, _
                        0&, _
                        bytesreturned, _
                        ByVal 0&)


  CloseHandle hdevice

  End If

</code>

Any information on this or the other control codes would be very
helpful. I am developing this with Visual Basic 6.0 SP6 on a Windows XP
Pro workstation. The target OS's that this utility would work under
will be Windows XP or Windows Server 2003.

*** TAKE CAUTION IF YOU RUN THIS CODE ***
Be sure you change the PhysicalDrive value to a test drive on your
development system!

Thanks for any assistance!

B-Mann

0
10/26/2006 1:43:32 AM
comp.lang.basic.visual.misc 10153 articles. 0 followers. Post Follow

6 Replies
1090 Views

Similar Articles

[PageSpeed] 52

"B-Mann" <johnbattaglio@gmail.com> wrote in message
news:1161827012.904465.201380@h48g2000cwc.googlegroups.com...
| When I first started working on this project, I did not think this
| would be possible with Visual Basic, but to my surprise, I found
enough
| information to create a nice imaging tool. The only issue, it only
| works with disk volumes. Although, this is still very useful, I
would
| like to mimic the operation of commercial disk imaging utilities
like
| Ghost. The commercial products can perform an image or disk to disk
| copy of an entire disk and all volumes on the disk. I believe I can
use
| the following API and control codes, among others, to perform the
tasks
| I am looking for, put can not find VB examples of their use.
|
| API:
| DeviceIoControl
|
| Control Codes:
| IOCTL_DISK_CREATE_DISK
| IOCTL_DISK_DELETE_DRIVE_LAYOUT
| IOCTL_DISK_SET_PARTITION_INFO
| IOCTL_DISK_SET_DRIVE_LAYOUT
| IOCTL_DISK_UPDATE_PROPERTIES
|
| The first issue I would like tackle is to create a disk using
| IOCTL_DISK_CREATE_DISK. Here is what I have at this point, but the
disk
| is not created. I have tried this with a RAW disk as well as an
| initialized, partitioned, and formated disks as well.
|
| <code>
| Public Const GENERIC_READ = &H80000000
| Public Const GENERIC_WRITE = &H40000000
| Public Const FILE_SHARE_READ As Long = &H1
| Public Const FILE_SHARE_WRITE As Long = &H2
| Public Const OPEN_EXISTING As Long = 3
| Public Const INVALID_HANDLE_VALUE As Long = -1
| Public Const PARTITION_FAT32 = &HB
| Public Const IOCTL_DISK_CREATE_DISK = &H7C058
|
| Public Enum PARTITION_STYLE
|   PARTITION_STYLE_MBR
|   PARTITION_STYLE_GPT
|   PARTITION_STYLE_RAW
| End Enum
|
| Public Type GUID
|   Data1 As Long
|   Data2 As Integer
|   Data3 As Integer
|   Data4(0 To 7) As Byte
| End Type
|
| Public Type CREATE_DISK_GPT
|   DiskId As GUID
|   MaxPartitionCount As Long
| End Type
|
| Public Type CREATE_DISK_MBR
|   Signature As Long
| End Type
|
| Public Type CREATE_DISK
|   PartitionStyle As PARTITION_STYLE
|   PartitionMBRType As CREATE_DISK_MBR
|   PartitionGPTType As CREATE_DISK_GPT
| End Type
|
| Public Declare Function CreateFile Lib "kernel32" _
|    Alias "CreateFileA" _
|   (ByVal lpFileName As String, _
|    ByVal dwDesiredAccess As Long, _
|    ByVal dwShareMode As Long, _
|    ByVal lpSecurityAttributes As Any, _
|    ByVal dwCreationDisposition As Long, _
|    ByVal dwFlagsAndAttributes As Long, _
|    ByVal hTemplateFile As Long) As Long
|
| Public Declare Function CloseHandle Lib "kernel32" _
|    (ByVal hObject As Long) As Long
|
| Public Declare Function DeviceIoControl Lib "kernel32" _
|    (ByVal hdevice As Long, _
|    ByVal dwIoControlCode As Long, _
|    lpInBuffer As Any, ByVal _
|    nInBufferSize As Long, _
|    lpOutBuffer As Any, _
|    ByVal nOutBufferSize As Long, _
|    lpBytesReturned As Long, _
|    lpOverlapped As Any) As Long
|
|   Dim cd As CREATE_DISK
|   Dim j As Integer
|   Dim hdevice As Long
|   Dim bytesreturned As Long
|   Dim PhysicalDrive As Long
|   Dim retval As Long
|
|   PhysicalDrive = 0
|   hdevice = CreateFile("\\.\PHYSICALDRIVE" & CStr(PhysicalDrive), _
|                             GENERIC_READ Or GENERIC_WRITE, _
|                             FILE_SHARE_READ Or FILE_SHARE_WRITE, _
|                             ByVal 0&, _
|                             OPEN_EXISTING, _
|                             0&, 0&)
|
|   If hdevice <> INVALID_HANDLE_VALUE Then
|     cd.PartitionStyle = PARTITION_STYLE_MBR
|     cd.PartitionMBRType.Signature = PARTITION_FAT32
|     cd.PartitionGPTType.DiskId.Data1 = 0
|     cd.PartitionGPTType.DiskId.Data2 = 0
|     cd.PartitionGPTType.DiskId.Data3 = 0
|     For j = 0 To 7
|     cd.PartitionGPTType.DiskId.Data4(j) = 0
|     Next j
|     cd.PartitionGPTType.MaxPartitionCount = 0
|
|     retval = DeviceIoControl(hdevice, _
|                         IOCTL_DISK_CREATE_DISK, _
|                         cd, _
|                         Len(cd), _
|                         ByVal 0&, _
|                         0&, _
|                         bytesreturned, _
|                         ByVal 0&)
|
|
|   CloseHandle hdevice
|
|   End If
|
| </code>
|
| Any information on this or the other control codes would be very
| helpful. I am developing this with Visual Basic 6.0 SP6 on a Windows
XP
| Pro workstation. The target OS's that this utility would work under
| will be Windows XP or Windows Server 2003.
|
| *** TAKE CAUTION IF YOU RUN THIS CODE ***
| Be sure you change the PhysicalDrive value to a test drive on your
| development system!
|


To mimic a product like Ghost, you'll need to perform HD sector access
as well as writing some very sophisticated code to rewrite/resize the
FAT or NTFS volume tables (I know little about NTFS volume
design/structures, but certainly, NTFS is much more complex than
FAT32).  Ghost includes the ability to create a backup image and
restore it to a target partition that is larger than the original
source partition, then Ghost will resize the FAT or NTFS file data
structures so that they correspond to the target partition size.

IOW, you decide to image a 10 gig partition with Ghost, then decide to
restore the image to a 20 gig partition, and Ghost will recognize it
needs to (and actually does) rewrite the FAT or NTFS file structures
to correspond with the size of the target partition.  Why is this
important?  Well, if the FAT/volume structures are not resized to
"fit" the target partition when the target is larger than the source
partition, then the OS will be unable to utilize the entire available
disk space of the 20 gig drive since it now has a FAT or volume
structure originally sized for a 10 gig partition.  Ghost is
essentially a sector copying program of significant sophistication, as
it also includes file assembly capability (analyzing the FAT or NTFS
volume structure to assemble individual files for recovery purposes)
in addition to the aforementioned capability regarding resizing sector
usage tables/structures.  I suppose with some "helper" drivers (a
driver that enables direct sector read/write access) one might write a
ghost-like program in VB6, but why?  Purchasing a copy of Ghost is a
lot less headache.

Hope I have not stifled your enthusiasm, it is perhaps a doable feat,
but you will become a master of FAT and NTFS data structures before
you are done.  Reading and writing sectors will be the easy part of
the challenge.
-- 
Best regards,
Kyle

0
me4 (19624)
10/26/2006 5:21:04 AM
Kyle wrote:
>
> To mimic a product like Ghost, you'll need to perform HD sector access
> as well as writing some very sophisticated code to rewrite/resize the
> FAT or NTFS volume tables (I know little about NTFS volume
> design/structures, but certainly, NTFS is much more complex than
> FAT32).  Ghost includes the ability to create a backup image and
> restore it to a target partition that is larger than the original
> source partition, then Ghost will resize the FAT or NTFS file data
> structures so that they correspond to the target partition size.
>
> IOW, you decide to image a 10 gig partition with Ghost, then decide to
> restore the image to a 20 gig partition, and Ghost will recognize it
> needs to (and actually does) rewrite the FAT or NTFS file structures
> to correspond with the size of the target partition.  Why is this
> important?  Well, if the FAT/volume structures are not resized to
> "fit" the target partition when the target is larger than the source
> partition, then the OS will be unable to utilize the entire available
> disk space of the 20 gig drive since it now has a FAT or volume
> structure originally sized for a 10 gig partition.  Ghost is
> essentially a sector copying program of significant sophistication, as
> it also includes file assembly capability (analyzing the FAT or NTFS
> volume structure to assemble individual files for recovery purposes)
> in addition to the aforementioned capability regarding resizing sector
> usage tables/structures.  I suppose with some "helper" drivers (a
> driver that enables direct sector read/write access) one might write a
> ghost-like program in VB6, but why?  Purchasing a copy of Ghost is a
> lot less headache.
>
> Hope I have not stifled your enthusiasm, it is perhaps a doable feat,
> but you will become a master of FAT and NTFS data structures before
> you are done.  Reading and writing sectors will be the easy part of
> the challenge.
> --
> Best regards,
> Kyle

Kyle,

Yes I realize all the points you make, and understand these challanges.
I already have the ability to perform the sector copy process. I have
written this part and can create image files as well as volume to
volume imaging directly. Currently, I am only looking to perform same
size disk imaging, as the project expands, I will look at the resizing
of the drives as well. From what I am reading from the Disk Management
pages on MSDN, the DeviceIoControl should allow me to access the drive
at the disk level instead of a per volume level. This is all I am
attempting to accomplish at this point. If I am not able to perform the
creation of the disk, I may as well stop at attempting anything
further. I can use the DeviceIoControl in combination with
IOCTL_DISK_DELETE_DRIVE_LAYOUT to delete all partitions on the drive,
so I know I have access at that level. When I attempt to create the
disk using the code I provided prior, I get no error, but the drive is
not created.
As for purchasing the Ghost product, that would be fine if I was only
wanting to make a single copy or image of a drive. But I have a much
larger project in mind and the licensing of such a commercial product
would not be feasible.

Again, any assistant would be helpful.
Thanks,

B-Mann

0
10/26/2006 11:02:38 AM
Kyle wrote:
>
> To mimic a product like Ghost, you'll need to perform HD sector access
> as well as writing some very sophisticated code to rewrite/resize the
> FAT or NTFS volume tables (I know little about NTFS volume
> design/structures, but certainly, NTFS is much more complex than
> FAT32).  Ghost includes the ability to create a backup image and
> restore it to a target partition that is larger than the original
> source partition, then Ghost will resize the FAT or NTFS file data
> structures so that they correspond to the target partition size.
>
> IOW, you decide to image a 10 gig partition with Ghost, then decide to
> restore the image to a 20 gig partition, and Ghost will recognize it
> needs to (and actually does) rewrite the FAT or NTFS file structures
> to correspond with the size of the target partition.  Why is this
> important?  Well, if the FAT/volume structures are not resized to
> "fit" the target partition when the target is larger than the source
> partition, then the OS will be unable to utilize the entire available
> disk space of the 20 gig drive since it now has a FAT or volume
> structure originally sized for a 10 gig partition.  Ghost is
> essentially a sector copying program of significant sophistication, as
> it also includes file assembly capability (analyzing the FAT or NTFS
> volume structure to assemble individual files for recovery purposes)
> in addition to the aforementioned capability regarding resizing sector
> usage tables/structures.  I suppose with some "helper" drivers (a
> driver that enables direct sector read/write access) one might write a
> ghost-like program in VB6, but why?  Purchasing a copy of Ghost is a
> lot less headache.
>
> Hope I have not stifled your enthusiasm, it is perhaps a doable feat,
> but you will become a master of FAT and NTFS data structures before
> you are done.  Reading and writing sectors will be the easy part of
> the challenge.
> --
> Best regards,
> Kyle

Kyle,

Yes I realize all the points you make, and understand these challanges.
I already have the ability to perform the sector copy process. I have
written this part and can create image files as well as volume to
volume imaging directly. Currently, I am only looking to perform same
size disk imaging, as the project expands, I will look at the resizing
of the drives as well. From what I am reading from the Disk Management
pages on MSDN, the DeviceIoControl should allow me to access the drive
at the disk level instead of a per volume level. This is all I am
attempting to accomplish at this point. If I am not able to perform the
creation of the disk, I may as well stop at attempting anything
further. I can use the DeviceIoControl in combination with
IOCTL_DISK_DELETE_DRIVE_LAYOUT to delete all partitions on the drive,
so I know I have access at that level. When I attempt to create the
disk using the code I provided prior, I get no error, but the drive is
not created.
As for purchasing the Ghost product, that would be fine if I was only
wanting to make a single copy or image of a drive. But I have a much
larger project in mind and the licensing of such a commercial product
would not be feasible.

Again, any assistant would be helpful.
Thanks,

B-Mann

0
10/26/2006 11:03:04 AM
"B-Mann" <johnbattaglio@gmail.com> wrote in message
news:1161860584.379328.274480@i3g2000cwc.googlegroups.com...
| Kyle wrote:
| >
| > To mimic a product like Ghost, you'll need to perform HD sector
access
| > as well as writing some very sophisticated code to rewrite/resize
the
| > FAT or NTFS volume tables (I know little about NTFS volume
| > design/structures, but certainly, NTFS is much more complex than
| > FAT32).  Ghost includes the ability to create a backup image and
| > restore it to a target partition that is larger than the original
| > source partition, then Ghost will resize the FAT or NTFS file data
| > structures so that they correspond to the target partition size.
| >
| > IOW, you decide to image a 10 gig partition with Ghost, then
decide to
| > restore the image to a 20 gig partition, and Ghost will recognize
it
| > needs to (and actually does) rewrite the FAT or NTFS file
structures
| > to correspond with the size of the target partition.  Why is this
| > important?  Well, if the FAT/volume structures are not resized to
| > "fit" the target partition when the target is larger than the
source
| > partition, then the OS will be unable to utilize the entire
available
| > disk space of the 20 gig drive since it now has a FAT or volume
| > structure originally sized for a 10 gig partition.  Ghost is
| > essentially a sector copying program of significant
sophistication, as
| > it also includes file assembly capability (analyzing the FAT or
NTFS
| > volume structure to assemble individual files for recovery
purposes)
| > in addition to the aforementioned capability regarding resizing
sector
| > usage tables/structures.  I suppose with some "helper" drivers (a
| > driver that enables direct sector read/write access) one might
write a
| > ghost-like program in VB6, but why?  Purchasing a copy of Ghost is
a
| > lot less headache.
| >
| > Hope I have not stifled your enthusiasm, it is perhaps a doable
feat,
| > but you will become a master of FAT and NTFS data structures
before
| > you are done.  Reading and writing sectors will be the easy part
of
| > the challenge.
| > --
| > Best regards,
| > Kyle
|
| Kyle,
|
| Yes I realize all the points you make, and understand these
challanges.
| I already have the ability to perform the sector copy process. I
have
| written this part and can create image files as well as volume to
| volume imaging directly. Currently, I am only looking to perform
same
| size disk imaging, as the project expands, I will look at the
resizing
| of the drives as well. From what I am reading from the Disk
Management
| pages on MSDN, the DeviceIoControl should allow me to access the
drive
| at the disk level instead of a per volume level. This is all I am
| attempting to accomplish at this point. If I am not able to perform
the
| creation of the disk, I may as well stop at attempting anything
| further. I can use the DeviceIoControl in combination with
| IOCTL_DISK_DELETE_DRIVE_LAYOUT to delete all partitions on the
drive,
| so I know I have access at that level. When I attempt to create the
| disk using the code I provided prior, I get no error, but the drive
is
| not created.
| As for purchasing the Ghost product, that would be fine if I was
only
| wanting to make a single copy or image of a drive. But I have a much
| larger project in mind and the licensing of such a commercial
product
| would not be feasible.
|
| Again, any assistant would be helpful.
| Thanks,
|


Perhaps there is a sequence of API calls required to "mount" the drive
for use by the system.  It could be the create disk process in your
code is working fine, but the drive is not accessible b/c it is not
"mounted".
-- 
Best regards,
Kyle

0
me4 (19624)
10/26/2006 5:01:10 PM
"Kyle" <me@privacy.net> wrote in message
news:ApqdnY9oR7YyRt3YnZ2dnUVZ_u6dnZ2d@comcast.com...
| "B-Mann" <johnbattaglio@gmail.com> wrote in message
| news:1161860584.379328.274480@i3g2000cwc.googlegroups.com...
| | Kyle wrote:
| | >
| | > To mimic a product like Ghost, you'll need to perform HD sector
| access
| | > as well as writing some very sophisticated code to
rewrite/resize
| the
| | > FAT or NTFS volume tables (I know little about NTFS volume
| | > design/structures, but certainly, NTFS is much more complex than
| | > FAT32).  Ghost includes the ability to create a backup image and
| | > restore it to a target partition that is larger than the
original
| | > source partition, then Ghost will resize the FAT or NTFS file
data
| | > structures so that they correspond to the target partition size.
| | >
| | > IOW, you decide to image a 10 gig partition with Ghost, then
| decide to
| | > restore the image to a 20 gig partition, and Ghost will
recognize
| it
| | > needs to (and actually does) rewrite the FAT or NTFS file
| structures
| | > to correspond with the size of the target partition.  Why is
this
| | > important?  Well, if the FAT/volume structures are not resized
to
| | > "fit" the target partition when the target is larger than the
| source
| | > partition, then the OS will be unable to utilize the entire
| available
| | > disk space of the 20 gig drive since it now has a FAT or volume
| | > structure originally sized for a 10 gig partition.  Ghost is
| | > essentially a sector copying program of significant
| sophistication, as
| | > it also includes file assembly capability (analyzing the FAT or
| NTFS
| | > volume structure to assemble individual files for recovery
| purposes)
| | > in addition to the aforementioned capability regarding resizing
| sector
| | > usage tables/structures.  I suppose with some "helper" drivers
(a
| | > driver that enables direct sector read/write access) one might
| write a
| | > ghost-like program in VB6, but why?  Purchasing a copy of Ghost
is
| a
| | > lot less headache.
| | >
| | > Hope I have not stifled your enthusiasm, it is perhaps a doable
| feat,
| | > but you will become a master of FAT and NTFS data structures
| before
| | > you are done.  Reading and writing sectors will be the easy part
| of
| | > the challenge.
| | > --
| | > Best regards,
| | > Kyle
| |
| | Kyle,
| |
| | Yes I realize all the points you make, and understand these
| challanges.
| | I already have the ability to perform the sector copy process. I
| have
| | written this part and can create image files as well as volume to
| | volume imaging directly. Currently, I am only looking to perform
| same
| | size disk imaging, as the project expands, I will look at the
| resizing
| | of the drives as well. From what I am reading from the Disk
| Management
| | pages on MSDN, the DeviceIoControl should allow me to access the
| drive
| | at the disk level instead of a per volume level. This is all I am
| | attempting to accomplish at this point. If I am not able to
perform
| the
| | creation of the disk, I may as well stop at attempting anything
| | further. I can use the DeviceIoControl in combination with
| | IOCTL_DISK_DELETE_DRIVE_LAYOUT to delete all partitions on the
| drive,
| | so I know I have access at that level. When I attempt to create
the
| | disk using the code I provided prior, I get no error, but the
drive
| is
| | not created.
| | As for purchasing the Ghost product, that would be fine if I was
| only
| | wanting to make a single copy or image of a drive. But I have a
much
| | larger project in mind and the licensing of such a commercial
| product
| | would not be feasible.
| |
| | Again, any assistant would be helpful.
| | Thanks,
| |
|
|
| Perhaps there is a sequence of API calls required to "mount" the
drive
| for use by the system.  It could be the create disk process in your
| code is working fine, but the drive is not accessible b/c it is not
| "mounted".
| -- 
| Best regards,
| Kyle
|

E.g.,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/setvolumemountpoint.asp

SetVolumeMountPoint is required to mount a new volume on a HD, and a
registry entry/GUI entry is created for persistent access to the
device.  Or, maybe DefineDosDevice can be used to temporarily access
the HD without creating the GUI registry entries, am unceratin on
this.

Looks like this page will be of assistance to you with volume
manipulation API calls:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/volume_management_functions.asp

-- 
Best regards,
Kyle

0
me4 (19624)
10/26/2006 5:28:50 PM
Kyle,

    Thanks for your responses. I have looked at those links, and have
made some headway.

All,

I am currently able to create a disk from a RAW drive using the
following:

================================================
  Dim cd As CREATE_DISK
  Dim newdli As DRIVE_LAYOUT_INFORMATION

    With cd
      .PartitionStyle = PARTITION_STYLE_MBR
      .PartitionMBRType.Signature = PARTITION_FAT32
      .PartitionGPTType.DiskId.Data1 = 0
      .PartitionGPTType.DiskId.Data2 = 0
      .PartitionGPTType.DiskId.Data3 = 0
      For j = 0 To 7
        .PartitionGPTType.DiskId.Data4(j) = 0
      Next j
      .PartitionGPTType.MaxPartitionCount =
UBound(newdli.PartitionEntry)
    End With

    Call DiskCreateDisk(hdevice, cd)
    Call DiskUpdateProperties(hdevice)

Sub DiskCreateDisk(hdevice As Long, cd As CREATE_DISK)
  Dim retval As Long
  Dim bytesreturned As Long

  retval = DeviceIoControl(hdevice, _
             IOCTL_DISK_CREATE_DISK, _
             cd, _
             Len(cd), _
             ByVal 0&, _
             0&, _
             bytesreturned, _
             ByVal 0&)

' At this point the entire disk is unallocated.
End Sub

Sub DiskUpdateProperties(hdevice As Long)
  Dim retval As Long
  Dim bytesreturned As Long

  retval = DeviceIoControl(hdevice, _
             IOCTL_DISK_UPDATE_PROPERTIES, _
             ByVal 0&, _
             0&, _
             ByVal 0&, _
             0&, _
             bytesreturned, _
             ByVal 0&)

End Sub

================================================
My next hurdle is to create the partition(s) on the drive.
The following code creates a single partition which is the entire
size of the disk no matter what I make the PartitionLength:

Public Const IOCTL_DISK_SET_DRIVE_LAYOUT = &H7C010

    newdli.PartitionCount = UBound(newdli.PartitionEntry) - 1
    newdli.Signature = 0
    With newdli.PartitionEntry(0)
      .BootIndicator = False
      .HiddenSectors = 63
      .PartitionLength.liLow = 1073447424
      .PartitionLength.liHigh = 0
      .PartitionNumber = 1
      .PartitionType = 7
      .RecognizedPartition = True
      .RewritePartition = True
      .StartingOffset.liLow = 32256
      .StartingOffset.liHigh = 0
    End With

    For k = 2 To newdli.PartitionCount
      With newdli.PartitionEntry(k)
        .BootIndicator = False
        .HiddenSectors = 0
        .PartitionLength.liLow = 0
        .PartitionLength.liHigh = 0
        .PartitionNumber = k
        .PartitionType = PARTITION_ENTRY_UNUSED
        .RecognizedPartition = False
        .RewritePartition = True
        .StartingOffset.liLow = 0
        .StartingOffset.liHigh = 0
      End With
    Next k

    Call SetDriveLayout(hdevice, newdli)
    Call DiskUpdateProperties(hdevice)

Sub SetDriveLayout(hdevice As Long, dli As DRIVE_LAYOUT_INFORMATION)
  Dim retval As Long
  Dim bytesreturned As Long

  retval = DeviceIoControl(hdevice, _
             IOCTL_DISK_SET_DRIVE_LAYOUT, _
             dli, _
             Len(dli), _
             ByVal 0&, _
             0&, _
             bytesreturned, _
             ByVal 0&)

End Sub

The values that I am using to create the first partition was returned
from
a call to DeviceIoControl using IOCTL_DISK_GET_PARTITION_INFO
against the same disk prior to deleting the disk. The partition should
be a 1GB
partition, and the remaining area of the disk should be unallocated.

Thanks for any assistance,

B-Mann

0
10/29/2006 2:41:03 PM
Reply: