Solved

VB.Net Create ISO File

Posted on 2009-07-07
6
2,560 Views
Last Modified: 2014-03-24
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.

Andrew
0
Comment
Question by:Crazy_Penguins
  • 3
  • 3
6 Comments
 

Author Comment

by:Crazy_Penguins
Comment Utility
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
Comment Utility
<<  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.
hFile = CreateFileW("d:\my.iso", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0)
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)
CloseHandle(hFile)
CloseHandle(hDevice)
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
Comment Utility
' 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)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Crazy_Penguins
Comment Utility
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,
Andrew
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
Comment Utility
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
http://support.microsoft.com/kb/916902
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)
 
If hDevice = INVALID_HANDLE_VALUE Then
  ' 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
hFile = CreateFileW(StrPtr(SaveAs)<wbr ></wbr>, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH Or FILE_FLAG_NO_BUFFERING, 0)
 
If hFile = INVALID_HANDLE_VALUE Then
  ' 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
Do
  If ReadFile(hDevice, Buffer(0), 65536, cbRead, 0) Then
    Call WriteFile(hFile, Buffer(0), cbRead, cbWritten, 0)
  Else
    Exit Do
  End If
  ' User wants to abort.
  If bAbort = True Then Exit Do
  DoEvents
Loop Until cbRead = 0
 
' Check if user aborted or write finished.
If bAbort = True Then
  Debug.Print "Abort!"
Else
  Debug.Print "Write End: " & Time
End If
 
' Cleanup
CloseHandle hDevice
CloseHandle hFile
Erase Buffer
 
End Sub

Open in new window

0
 

Author Closing Comment

by:Crazy_Penguins
Comment Utility
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now