none
Autorun RRS feed

  • Question

  • what I do;

    1.Create small program ToWind.

    2.From C:\Users\User\Documents\Visual Studio 2010\Projects\ToWind\ToWind\bin\Debug\*.* copy to USB drive.

    3.Make the Autorun.inf

    [Autorun]
    Open=ToWind.exe
    Action=Start ToWind.exe
    Icon=diskicon.ico

    4.In USB look like this

    5.Plugged the USB but autorun not working.

    My question is why it not working and that have any way to write code in vb.net?

    Monday, December 17, 2018 6:07 AM

Answers

  • thank you IronRazer make me clear about that.It possible or not we write the vb code for autorun or auto launch

    USB?.May be it look like to use API file?.Just for learning ,give me a clue.

    Thank

     Sorry about such a long delay in my reply but,  I just have not had a lot of time to spend on the forums lately and did not see your post until today.

     Anyways,  the only way I can see is if you have an application running on the computer which monitors for removable drives being inserted.  When it detects one being inserted,  get it's drive letter and append your ToWind.exe file name to it.  Then just use the File.Exists method to see if the file exists on the inserted usb drive or not.  If it does exist,  then use the Process.Start method to start the ToWind app.

     You would probably want to put this usb drive monitoring application's exe file in the users StartUp folder,  or place it elsewhere on the computer and put a shortcut to it in the users StartUp folder.  Then when the user logs in,  the usb monitoring application will be started.  The users Startup folder

     Below is an example that works good for me.  When I insert a usb drive with a file named 'ToWind.exe' in its root,  the ToWind app gets started.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Const WM_DEVICECHANGE As Integer = &H219
        Private Const DEVICE_ARRIVAL As Integer = &H8000
        Private Const DEVTYPE_VOLUME As Integer = &H2
    
        <StructLayout(LayoutKind.Sequential)>
        Public Structure DEV_BROADCAST_HDR
            Public dbch_size As UInteger
            Public dbch_devicetype As UInteger
            Public dbch_reserved As UInteger
        End Structure
    
        <StructLayout(LayoutKind.Sequential)>
        Public Structure DEV_BROADCAST_VOLUME
            Public dbcv_size As UInteger
            Public dbcv_devicetype As UInteger
            Public dbcv_reserved As UInteger
            Public dbcv_unitmask As UInteger
            Public dbcv_flags As UShort
        End Structure
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            If m.Msg = WM_DEVICECHANGE AndAlso m.WParam.ToInt32 = DEVICE_ARRIVAL Then
                Dim dh As DEV_BROADCAST_HDR = DirectCast(m.GetLParam(GetType(DEV_BROADCAST_HDR)), DEV_BROADCAST_HDR)
                If dh.dbch_devicetype = DEVTYPE_VOLUME Then
                    Dim volstrct As DEV_BROADCAST_VOLUME = DirectCast(m.GetLParam(GetType(DEV_BROADCAST_VOLUME)), DEV_BROADCAST_VOLUME)
                    For i As Integer = 0 To 25
                        If volstrct.dbcv_unitmask = (2 ^ i) Then
                            Dim ExePath As String = Chr(65 + i) & ":\ToWind.exe"
                            Dim ExistingProcess As Process = Process.GetProcesses.Where(Function(x) x.ProcessName.ToLower = IO.Path.GetFileNameWithoutExtension(ExePath).ToLower).FirstOrDefault
                            If IO.File.Exists(ExePath) AndAlso ExistingProcess Is Nothing Then Process.Start(ExePath)
                            Exit For
                        End If
                    Next
                End If
            End If
        End Sub
    End Class
    
     

     One other thing you did not mention is what should happen if the usb drive is already inserted when the user first logs in or starts their computer.  Should the ToWind application be found and started then too,  or only when the usb drive is inserted?


    If you say it can`t be done then i`ll try it

    • Marked as answer by mipakteh Saturday, January 12, 2019 9:15 AM
    Wednesday, January 9, 2019 10:35 PM

All replies

  • Removable media has not auto-started in quite a while. The default is to do nothing.
    Monday, December 17, 2018 7:02 AM
  • Hi,

    First you should turn on the computer autorun function.

    https://docs.microsoft.com/en-us/windows/desktop/shell/autoplay-reg

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 17, 2018 7:10 AM
  • Hi Alex.

    I begin follow your step.

    1.Go to Control panel then Checcked"Autoplay Device On"

    2.Using Regedit then there is no Explorer folder but I create it.put string "NoDriveTypeAutoRun" set Value to 145

    3.Local Group Police Editor.

    computer config->Admin Template -> Auto Policies -> Auotplay.

    4. Try Use vb code write registery key.

    Imports Microsoft.Win32
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim regVersion As RegistryKey
            regVersion = Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", True)
            If regVersion Is Nothing Then
                regVersion = Registry.CurrentUser.CreateSubKey("HKEY_CURRENT_USER\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer")
            End If
            Dim intVersion As Integer = 0
            If (Not regVersion Is Nothing) Then
                intVersion = regVersion.GetValue("Version", 0)
                intVersion = intVersion + 255
                regVersion.SetValue("NoDriveTypeAutoRun", intVersion)
                regVersion.Close()
            End If
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    
    

    All the way above not function my Autorun.inf still dead.

    can someone give me the real code vb or other way it possible.

    thank.

    Wednesday, December 19, 2018 5:44 AM
  • Copy the program or a shortcut to it into the StartUp Folder
    in VB.NET you can use:

    'Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup)

    Wednesday, December 19, 2018 6:34 AM
  • Did you set it in control panel? This is windows 7. Works for me.

    Wednesday, December 19, 2018 6:38 AM
  • yes,tommytwotrain.

    Wednesday, December 19, 2018 7:49 AM
  • Devon_Nullman,

    I test for your suggestion but cannot do it.It possible the other way.

    thank.

    Imports System.IO
    
    
    Public Class Form1
        Private Const WM_DEVICECHANGE As Integer = &H219
        Private Const DBT_DEVICEARRIVAL As Integer = &H8000
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            If m.Msg = WM_DEVICECHANGE AndAlso m.WParam.ToInt32 = DBT_DEVICEARRIVAL Then
                For Each d As DriveInfo In DriveInfo.GetDrives.Where(Function(x) x.DriveType = DriveType.Removable)
                    If Directory.Exists(d.Name & "VB_Black Screen") Then
                        My.Computer.FileSystem.CopyDirectory(d.Name & "VB_Black Screen", Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup) & "\VB_Black Screen", True)
                    End If
                Next
            End If
    
            'C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
    
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
    
    
        End Sub
    End Class
    

    Monday, December 24, 2018 2:42 AM
  •  Unfortunately,  you can not do this with an Autorun file on a USB thumb drive in windows 7 and probably not any version past 7.  As Devon said,  they disabled this for security reasons.  This Artical about Autorun.inf files for windows XP,  Vista,  and 7.  It explains that only the 'LABEL' and 'ICON' entries of an autorun file will work with a USB drive in windows 7,  it will not execute the file given in the 'OPEN' entry on windows 7.  I have also tested and confirmed it not to work on windows 7,  with AutoPlay turned on or off.

    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 24, 2018 9:27 PM
    Monday, December 24, 2018 9:25 PM
  • thank you IronRazer make me clear about that.It possible or not we write the vb code for autorun or auto launch

    USB?.May be it look like to use API file?.Just for learning ,give me a clue.

    Thank

    Wednesday, December 26, 2018 2:53 AM
  • thank you IronRazer make me clear about that.It possible or not we write the vb code for autorun or auto launch

    USB?.May be it look like to use API file?.Just for learning ,give me a clue.

    Thank

     Sorry about such a long delay in my reply but,  I just have not had a lot of time to spend on the forums lately and did not see your post until today.

     Anyways,  the only way I can see is if you have an application running on the computer which monitors for removable drives being inserted.  When it detects one being inserted,  get it's drive letter and append your ToWind.exe file name to it.  Then just use the File.Exists method to see if the file exists on the inserted usb drive or not.  If it does exist,  then use the Process.Start method to start the ToWind app.

     You would probably want to put this usb drive monitoring application's exe file in the users StartUp folder,  or place it elsewhere on the computer and put a shortcut to it in the users StartUp folder.  Then when the user logs in,  the usb monitoring application will be started.  The users Startup folder

     Below is an example that works good for me.  When I insert a usb drive with a file named 'ToWind.exe' in its root,  the ToWind app gets started.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Const WM_DEVICECHANGE As Integer = &H219
        Private Const DEVICE_ARRIVAL As Integer = &H8000
        Private Const DEVTYPE_VOLUME As Integer = &H2
    
        <StructLayout(LayoutKind.Sequential)>
        Public Structure DEV_BROADCAST_HDR
            Public dbch_size As UInteger
            Public dbch_devicetype As UInteger
            Public dbch_reserved As UInteger
        End Structure
    
        <StructLayout(LayoutKind.Sequential)>
        Public Structure DEV_BROADCAST_VOLUME
            Public dbcv_size As UInteger
            Public dbcv_devicetype As UInteger
            Public dbcv_reserved As UInteger
            Public dbcv_unitmask As UInteger
            Public dbcv_flags As UShort
        End Structure
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            If m.Msg = WM_DEVICECHANGE AndAlso m.WParam.ToInt32 = DEVICE_ARRIVAL Then
                Dim dh As DEV_BROADCAST_HDR = DirectCast(m.GetLParam(GetType(DEV_BROADCAST_HDR)), DEV_BROADCAST_HDR)
                If dh.dbch_devicetype = DEVTYPE_VOLUME Then
                    Dim volstrct As DEV_BROADCAST_VOLUME = DirectCast(m.GetLParam(GetType(DEV_BROADCAST_VOLUME)), DEV_BROADCAST_VOLUME)
                    For i As Integer = 0 To 25
                        If volstrct.dbcv_unitmask = (2 ^ i) Then
                            Dim ExePath As String = Chr(65 + i) & ":\ToWind.exe"
                            Dim ExistingProcess As Process = Process.GetProcesses.Where(Function(x) x.ProcessName.ToLower = IO.Path.GetFileNameWithoutExtension(ExePath).ToLower).FirstOrDefault
                            If IO.File.Exists(ExePath) AndAlso ExistingProcess Is Nothing Then Process.Start(ExePath)
                            Exit For
                        End If
                    Next
                End If
            End If
        End Sub
    End Class
    
     

     One other thing you did not mention is what should happen if the usb drive is already inserted when the user first logs in or starts their computer.  Should the ToWind application be found and started then too,  or only when the usb drive is inserted?


    If you say it can`t be done then i`ll try it

    • Marked as answer by mipakteh Saturday, January 12, 2019 9:15 AM
    Wednesday, January 9, 2019 10:35 PM
  • thank you very much.

    It working good

    Saturday, January 12, 2019 9:15 AM