From: B-Mann on
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

From: Kyle on
"B-Mann" <johnbattaglio(a)gmail.com> wrote in message
news:1161827012.904465.201380(a)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 NT
From: B-Mann on
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

From: B-Mann on
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

From: Kyle on
"B-Mann" <johnbattaglio(a)gmail.com> wrote in message
news:1161860584.379328.274480(a)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

 |  Next  |  Last
Pages: 1 2
Prev: Saving picture in VB6
Next: ShellExecuteEx example