martin2k

ForumsVB6GamesSoftwareDirectoryHTMLArticlesBlogContact

Autorun


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

Program

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:

[autorun]
OPEN=setup.exe
ICON=\Quake3\Quake3.exe
shell\dinstall\command=Quake3\directx7\dxsetup.exe
shell\dinstall=&Install DirectX...

This file is used by Windows to automatically launch a program from the disc when it is inserted into the drive.

The part of the file used by the Autorun program is the OPEN key in the [autorun] section.

This article will show how the program works.  You can download the source code here:


vb_53_autorun.zip
(9.12 KB)

This program uses the following ActiveX controls:

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
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.

frmConfigure


The Configuration Form

This Form contains:

  • 2 Frames (fraDVD, fraCD)
  • 5 Label controls (lblDVDEXE, lblDVDSI, lblCDEXE, lblDVDEXE, lblInfo)
  • 4 TextBoxes (txtDVDEXE, txtDVDSI, txtCDEXE, txtDVDEXE)
  • 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 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:

1. Download and unzip the project above.

2. Open the project in VB6.

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.


blog comments powered by Disqus

Archived Comments

From: Alpi Deshmukh
Date: Thursday, March 24, 2011 at 14:02:15
Comments: I love the way you hepp student.. And this site help me to complte my project...
 
From: varsha
Date: Saturday, January 30, 2010 at 08:18:08
Comments: this site is very helpful in my project
 
From: Septian
Date: Wednesday, October 7, 2009 at 13:11:19
Comments: thank you so much.
this is so help me to make my task.
 
From: Jerry
Date: Monday, September 28, 2009 at 19:51:00
Comments: Nice Program. Logically designed
 
From: Alfesh Gouri
Date: Wednesday, June 17, 2009 at 06:38:32
Comments: thinks for  helping. this link is useful for vb and vb.net devlopment
 
From: Ken
Date: Tuesday, May 26, 2009 at 14:48:37
Comments: What a wonderful idea. Very helpful to many computer users all over the world. Am from Africa Kenya and also a programmer
 
From: sri
Date: Tuesday, April 28, 2009 at 05:49:11
Comments: thanks for your source code in vb, but i'm need more spesification , because i'll make skripsi
 
From: raja k
Date: Friday, February 6, 2009 at 15:11:21
Comments: Keep it Up
 
From: belinda
Date: Thursday, November 20, 2008 at 14:25:19
Comments: thank you...it's so very help me....
thanks so much ... ^_^
 
From: thanigesan
Date: Wednesday, August 13, 2008 at 07:32:00
Comments: nice program
 
From: Siraj
Date: Thursday, July 24, 2008 at 08:47:17
Comments: thanks for helping us. need it so much i am a new programer
 
From: Gehlot Lalit
Date: Thursday, October 4, 2007 at 12:30:42
Comments: I'm a VB and VB.net Programmer from India. You Provide such a important information for the field of devlopment
 
From: Lyon
Date: Monday, September 10, 2007 at 19:35:42
Comments: Great programming

Martin Allen 1999 - 2012.  Last updated Friday 05 October 2012 06:26:31 PM +0100.