The Autorun program launches a program from the CD/DVD-ROM drive (drive D:
by default) by reading the file autorun.inf (if present) and obeying the instructions within. Here is an
example of this file:
This project contains two Forms and a module. One Form is displayed to
the user if no valid autorun.inf file is present (frmAutorun - Autorun.frm) and
the other is used to configure the CD audio and DVD video playing programs (frmConfigure
- frmConfigure.frm). The Startup Object is the Sub Main procedure in the
module (mainAutorun - Autorun.bas).
mainAutorun
mainAutorun contains the following code:
Public ADrive As String
Sub Main()
On Error GoTo error
Dim ARun As String
If Command() = "/c"
Then
frmConfigure.Show
Exit Sub
End If
If Command() = ""
Then
ADrive = "D:"
Else
ADrive = Mid(UCase(Command()), 1, 2)
End If
If frmAutorun.Allen.DExists(ADrive) = 0
Then
MsgBox "Drive " & ADrive & " does not exist" & vbCrLf &
vbCrLf & "Usage:" & vbCrLf & "Autorun <driveletter>:" & vbCrLf & "Autorun
/c", 64, "Autorun Usage"
Unload frmAutorun
Exit Sub
End If
Testexist:
If frmAutorun.Allen.DExists(ADrive) = 1
Then
Select Case MsgBox("No disc was found in drive " & ADrive &
". Please insert a disc and try again", vbRetryCancel + vbCritical, "Disc
Request")
Case vbRetry
GoTo Testexist
Case vbCancel
Unload frmAutorun
Exit Sub
End Select
End If
If frmAutorun.Allen.FExists(ADrive & "\track01.cda") =
True Then
If frmAutorun.Allen.FExists(GetSetting("Martin Allen", "Autorun",
"AudioCD")) = False Then
Select Case MsgBox("The audio CD
program either does not exist any more or it not been configured yet, do you
want to configure it now?", vbQuestion + vbYesNo, "Audio CD")
Case vbYes
frmConfigure.Show
Unload
frmAutorun
Exit Sub
Case vbNo
Unload
frmAutorun
Exit Sub
End Select
Else
If frmAutorun.Allen.DirExists(GetSetting("Martin Allen", "Autorun", "CDSI")) =
True Then
ChDir
GetSetting("Martin Allen", "Autorun", "CDSI")
ChDrive
GetSetting("Martin Allen", "Autorun", "CDSI")
End If
Shell GetSetting("Martin
Allen", "Autorun", "AudioCD"), vbNormalFocus
Unload frmAutorun
Exit Sub
End If
End If
If frmAutorun.Allen.DirExists(ADrive & "\VIDEO_TS") =
True Then
If frmAutorun.Allen.FExists(GetSetting("Martin Allen", "Autorun",
"VideoDVD")) = False Then
Select Case MsgBox("The video DVD
program either does not exist any more or it not been configured yet, do you
want to configure it now?", vbQuestion + vbYesNo, "Video DVD")
Case vbYes
frmConfigure.Show
Unload
frmAutorun
Exit Sub
Case vbNo
Unload
frmAutorun
Exit Sub
End Select
Else
If frmAutorun.Allen.DirExists(GetSetting("Martin Allen", "Autorun", "DVDSI")) =
True Then
ChDir
GetSetting("Martin Allen", "Autorun", "DVDSI")
ChDrive
GetSetting("Martin Allen", "Autorun", "DVDSI")
End If
Shell GetSetting("Martin
Allen", "Autorun", "VideoDVD"), vbNormalFocus
Unload frmAutorun
Exit Sub
End If
End If
ChDrive ADrive
ChDir ADrive
If frmAutorun.Allen.FExists(ADrive & "\autorun.inf") =
True Then
ARun = frmAutorun.getAutorun.GetFromINI("autorun", "open",
ADrive & "\autorun.inf")
If ARun = "[NOT FOUND]"
Then
frmAutorun.Show
Exit Sub
End If
If frmAutorun.Allen.FExists(ADrive & "\" & ARun) =
True Then
Shell ADrive & "\" & ARun,
vbNormalFocus
Unload frmAutorun
Exit Sub
Else
frmAutorun.Show
End If
Else
frmAutorun.Show
End If
Exit Sub
error:
MsgBox "An error has occured: " & Err.Description & ", " & Err.Number,
vbCritical, "Error"
Unload frmAutorun
End Sub
The ADrive variable holds the drive letter in the format "D:". The ARun
variable, declared within Sub_Main, will eventually hold the filename of the
program to be run.
Firstly, the program checks to see whether the user has run the program with
the argument '/c'. If so, the Configuration Form is displayed and the
procedure terminates.
Next, the program checks to see whether the user has run the program with a
drive letter as the argument. If not, 'D:' is used. The program then
has to check whether this drive exists and whether it contains media. See
Allen OCX to see how the DExists function works.
After this, the program uses the Allen OCX function FExists to check whether
the drive contains a file called 'track01.cda'. If it does this means that
the disc is an audio CD. Once this is known, the program can launch the CD
playing program. The CD and DVD playing programs are stored in the
registry - as configured using the Configuration Form (see below).
If the CD is not an audio CD, the program checks whether a folder called 'VIDEO_TS'
exists. If so, this is a DVD and the DVD playing program is launched.
If the disc is neither an audio CD or video DVD, the program looks for a file
called 'autorun.inf'. If this exists, the INITool control is used to get
the 'open' setting from the file. If this setting exists and is valid, the
program is launched. Otherwise, the Autorun Form is displayed, which gives
the user a choice as to what action to take next:
frmAutorun
The Autorun Form
This Form contains:
1 Label control (lblNoINF)
1 Allen OCX control (Allen)
3 CommandButton controls (Run (Enabled property set to False), Cancel, OD)
1 CheckBox (EOR) with Enabled property set to False
1 Image control (EXEIcon)
1 FileListBox (EXElist) with Pattern property set to "*.exe"
Private Sub Cancel_Click()
Unload frmAutorun
End Sub
Private Sub EXElist_Click()
Run.Enabled = True
EOR.Enabled = True
EXEIcon.Picture = Allen.GetBigIcon(ADrive & "\" & EXElist.FileName)
End Sub
Private Sub Form_Load()
lblNoINF.Caption = "Autorun.inf on drive " & ADrive & " does not exist or
the file does not " & vbCrLf & "contain the correct information, choose a
program to run" & vbCrLf & " from drive " & ADrive & "."
If Allen.DExists(ADrive) = 1
Then Exit Sub
If Allen.DExists(ADrive) = 0
Then Exit Sub
EXElist.Path = ADrive & "\"
End Sub
Private Sub OD_Click()
Shell "explorer " & ADrive, vbNormalFocus
End Sub
Private Sub Run_Click()
ChDir ADrive
ChDrive ADrive
If Allen.FExists(EXElist.FileName) =
True Then
Shell EXElist.FileName, vbNormalFocus
Else
MsgBox "Could not run the program '" & EXElist.FileName &
"'", vbCritical, "Run Error"
Exit Sub
End If
If EOR.Value = vbChecked
Then
Unload Me
End If
End Sub
When the Form loads, the Label control (lblNoINF) is set to a message, which
tells the user that there was a problem with reading the autorun.inf file on the
drive. If the drive held in the ADrive variable is valid, the FileListBox
control (EXEList) control is then set to show .exe files from the drive letter
held in the ADrive variable.
When the user clicks on a item in the list, the Run button and CheckBox
Enabled properties are set to True. Also the Image control is set to the
icon of the selected .exe file using the GetBigIcon method of the Allen OCX
control.
The Run button checks to see whether the file still exists and then executes
it. If the EOR (Exit on Run) CheckBox is checked, the program is unloaded.
The Cancel button unloads the Form. The OD (Open drive) button open an
Explorer window showing the drive contents.
5 CommandButtons (Browse1 - 4 with "..." as their caption, cmdClose with
"Exit" as its caption)
Microsoft Common Dialog Control 6.0 (EXE) (CancelError property set to True so
that the Cancel button on the 'Open' dialog box generates an error)
Directory Dialog (DirDlg)
Private Sub Browse1_Click()
EXEget txtDVDEXE, "Choose a DVD Video Player Application"
End Sub
Private Sub Browse2_Click()
DIRget txtDVDSI, "Choose a folder for the DVD Video player application:"
End Sub
Private Sub Browse3_Click()
EXEget txtCDEXE, "Choose a CD Audio Player Application"
End Sub
Private Sub Browse4_Click()
DIRget txtCDSI, "Choose a folder for the CD Audio player application:"
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub Form_Load()
txtDVDEXE.Text = GetSetting("Martin Allen", "Autorun", "VideoDVD")
txtDVDSI.Text = GetSetting("Martin Allen", "Autorun", "DVDSI")
txtCDEXE.Text = GetSetting("Martin Allen", "Autorun", "AudioCD")
txtCDSI.Text = GetSetting("Martin Allen", "Autorun", "CDSI")
End Sub
Private Sub Form_Unload(Cancel
As Integer)
SaveSetting "Martin Allen", "Autorun", "VideoDVD", txtDVDEXE.Text
SaveSetting "Martin Allen", "Autorun", "DVDSI", txtDVDSI.Text
SaveSetting "Martin Allen", "Autorun", "AudioCD", txtCDEXE.Text
SaveSetting "Martin Allen", "Autorun", "CDSI", txtCDSI.Text
End Sub
Sub EXEget(txt As
TextBox, Desc As String)
On Error GoTo error
EXE.DialogTitle = Desc
EXE.ShowOpen
txt.Text = EXE.FileName
error:
End Sub
Sub DIRget(dir As
TextBox, Desc As String)
Dim d As String
d = DirDlg.DirDlg(Desc, 17)
Select Case d
Case ""
Exit Sub
Case Else
dir.Text = d
End Select
End Sub
The code in this Form contains two sub procedures, which are there to save
duplicating code. The first one, called EXEget, is used to show the Common
Dialog's 'Open' dialog box. The argument 'txt' is used to specify the
TextBox for the resultant filename and 'Desc' specifies the text that will
appear in the dialog's caption. The On Error statement is used so that if
the Open dialog's Cancel button is pressed, the error this generates is handled.
The DIRget procedure is used to show the directory dialog box:
The Directory Dialog Box
The 'dir' argument specifies is used to specify the TextBox for the resultant
folder path and 'Desc' specifies the text that will appear as the instructions
that appear in the dialog box. The Flags argument for the DirDlg control
is set to 17, which means that the item at the top of the folder tree is 'My
Computer'. See the Directory Dialog Control page
for the full list of possible Flag values.
The Browse buttons call the EXEget and DIRget procedures. The Form_Load
procedure uses the GetSetting function to retrieve values from the following
location in the registry:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Martin Allen\Autorun
This is the location used by this program to store the values of the four
TextBoxes used on the Form. When the Form is unloaded, the Form_Unload
procedure uses the SaveSetting statement to save the settings back into the
registry. The cmdClose button is used to unload the Form.
Reply by ajay mehta:
hello sir i want to create a program in vb which has the following work.
here in mp3 discs there is an autorun file which run a executable file.when i
insert this disc in my cd drive it executes and format my system even system
files.this file mostly name called hello.exe and newhello.exe.this file cannot
detect by any antivirus.i want to create a program which ask to run autorun file
or not by displaying its name.
Answer by Martin Allen:
In order to achieve what you want to do, you can adapt the program in the
following way:
3. Remove frmConfigure and mainAutorun from the project (they are no longer
required).
4. Set the Startup Object to frmAutorun in the Project Properties.
5. Add the following to the (General) (Declarations) section.
Dim ADrive As String
6. Replace the code in Form_Load() with the following:
Private Sub Form_Load()
If Command() = "" Then
ADrive = "D:"
Else
ADrive = Mid(UCase(Command()), 1, 2)
If Allen.DExists(ADrive) = 0
Then
MsgBox "Drive " & ADrive & " does not
exist" & vbCrLf & vbCrLf & "Usage:" & vbCrLf & "Autorun <driveletter>:" &
vbCrLf & "Autorun /c", 64, "Autorun Usage"
ADrive = "D:"
End If
End If
If Allen.DExists(ADrive) = 1
Then
Testexist:
If
frmAutorun.Allen.DExists(ADrive) = 1 Then
Select Case MsgBox("No disc
was found in drive " & ADrive & ". Please insert a disc and try again",
vbRetryCancel + vbCritical, "Disc Request")
Case
vbRetry
GoTo Testexist
Case
vbCancel
Unload
frmAutorun
Exit Sub
End Select
End If
Else
lblNoINF.Caption = "Choose a program to run from drive " &
ADrive & "."
End If
EXElist.Path = ADrive & "\"
End Sub
Now, when you run the program, you will always see the Autorun form showing
the programs available to run on drive D (the default drive). Use a different
drive letter followed by a colon in the command line to show programs on the
different drive. You could create a shortcut for this.