• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2895
  • Last Modified:

VB.Net Create ISO File

Hello guys,

I am trying to determine how to save an ISO file of a CD image, though VB.Net...  My goal is to have a system to backup CD's and DVD's (Data, not movie) to our server here at work, and another a way to burn them back down to media if needed.

We run a shop where we fix a lot of computers - and want to make a vault of the 'Recovery Disks' provided with some systems.  After I can get the DVD -> ISO and ISO -> DVD (or CD) working, I will implement a feature to look for manufacture, model, etc.

Any idea's on the first part, Creating the ISO?  I would like to have a status bar as well - killing my original thought of a command-line background process...

Thanks for any solutions - sample code, please.

  • 3
  • 3
1 Solution
Crazy_PenguinsAuthor Commented:
<<  am trying to determine how to save an ISO file of a CD image >>

If you already have the CD that contains the image you just open the volume for direct access and read it like any normal file and save it as .ISO extension.

Here is how you can do this using API: (note: You will need admin privileges)

1) Open the cd volume for direct access. Use these flags below for cdrom. (e: would be the CDROM letter)
hDevice = CreateFileW("\\?\e:", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0)
2) Create a new file that you will write the image to. Use flags below.
3) Signals the file system driver not to perform any I/O boundary checks on partition read or write calls.
DeviceIoControl(hDevice, FSCTL_ALLOW_EXTENDED_DASD_IO, 0, 0, 0, 0, dwBytes, 0)

4)Just read the device like a regular file. Make sure to read the file in chunks a good buffer size would be 64KB.

ReadFile(hDevice, Buffer, BufferSize, dwRead, 0)
WriteFile(hFile, ByVal Buffer, dwRead, dwWritten, 0)

5)When your done reading and writing from the device finish by flushing the file and closing the handles.

Call FlushFileBuffers(hDevice)
' Handle to the file you created NOT the device handle like is shown in the above example. Can't belive I missed that before posting.
Should be:
Call FlushFileBuffers(hFile)
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Crazy_PenguinsAuthor Commented:
I will give this a better look in the morning, but to be sure we are on the same page, this will allow me to make an ISO image of the disk, correct?  Almost read like perhaps I did not state this well enough, but again I will look at your sample in the morning.

Thank you,
Yes. Funny you say that does it sound to easy to be true? :) if you already have a CD-ROM with data on it than what I mentioned above will create the exact image. I did this awhile back and managed to find the code I wrote in my backup although in VB6. Convert it to .NET or you can have someone convert it for you.

You should also visit this website from microsoft and use there tool. This will let you map the image to a drive letter. You can use it for testing or do what you like with it.
Virtual CD-ROM Control Panel
Option Explicit
' Author's Name
' Exact Copy CDROM
Private Const INVALID_HANDLE_VALUE As Long = (-1)
Private Const FSCTL_ALLOW_EXTENDED_DASD_<wbr ></wbr>IO As Long = &H90083
Private Const FILE_FLAG_BACKUP_SEMANTICS<wbr ></wbr> As Long = &H2000000
Private Const FILE_FLAG_WRITE_THROUGH As Long = &H80000000
Private Const FILE_FLAG_NO_BUFFERING As Long = &H20000000
Private Const FILE_SHARE_READ As Long = &H1
Private Const OPEN_EXISTING As Long = 3
Private Const CREATE_ALWAYS As Long = 2
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const DRIVE_CDROM As Long = 5
Private Declare Function CreateFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, ByVal lpInBuffer As Long, ByVal nInBufferSize As Long, ByVal lpOutBuffer As Long, ByVal nOutBufferSize As Long, ByRef lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function GetDriveTypeW Lib "kernel32" (ByVal lpRootPathName As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Dim bAbort As Boolean
Public Sub AbortWrite()
' Should be able to abort a write in progress.
bAbort = True
End Sub
Public Sub WriteImage(ByVal Device As String, ByVal SaveAs As String)
' Device: The device drive letter or volume guid. e:
' SaveAs: The location of the file being created. d:\dummy.iso
' Multiple of sectore size 2,048 bytes for CD-ROMs.
' (2048 * 32)
Dim Buffer(65536 - 1) As Byte
Dim cbBogus As Long
Dim cbRead As Long
Dim cbWritten As Long
Dim hDevice As Long
Dim hFile As Long
' Set abort flags.
bAbort = False
' Should check if this is a valid CD-ROM device.
If GetDriveTypeW(StrPtr(Devic<wbr ></wbr>e & "\")) <> DRIVE_CDROM Then
  ' Failed CDROM checksum.
  Exit Sub
End If
' Open the volume/device for direct access (DASD)
hDevice = CreateFileW(StrPtr("\\.\" & Device), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS<wbr ></wbr>, 0)
  ' Failed on accessing the volume handle.
  Exit Sub
End If
' Create a new file with restricted access. FILE_FLAG_NO_BUFFERING flag as a replacement for calling the FlushFileBuffers
  ' Failed creating file to write image.
  CloseHandle hDevice
  Exit Sub
End If
' Signals the file system driver not to perform any I/O boundary checks on partition read or write calls.
' Allows access to hidden sectors, a part of the partition that might exist between the first sector of
' the partition (the boot parameter block) and the first useful sector of the partition.
Call DeviceIoControl(hDevice, FSCTL_ALLOW_EXTENDED_DASD_<wbr ></wbr>IO, 0, 0, 0, 0, cbBogus, 0)
' Read/Write the image.
Debug.Print "Write Begin: " & Time
  If ReadFile(hDevice, Buffer(0), 65536, cbRead, 0) Then
    Call WriteFile(hFile, Buffer(0), cbRead, cbWritten, 0)
    Exit Do
  End If
  ' User wants to abort.
  If bAbort = True Then Exit Do
Loop Until cbRead = 0
' Check if user aborted or write finished.
If bAbort = True Then
  Debug.Print "Abort!"
  Debug.Print "Write End: " & Time
End If
' Cleanup
CloseHandle hDevice
CloseHandle hFile
Erase Buffer
End Sub

Open in new window

Crazy_PenguinsAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now