Results 1 to 4 of 4

Thread: How start your application automatically after restarting the PC

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Location
    India
    Posts
    21

    Question

    Can anyone help me.
    I have used Visual Basic Code to restart the PC.
    Also I want the program to continue executing further code
    after restarting.But what happens generally is that when I restart the PC through VB ,my application gets terminated.
    How do I make the application to start executing further code.
    Also when I restart the PC I have to log on to the Lan Network using a user name and Password.Is it possible to
    automatically feed the username and password to connect to the Lan network rather than the user typing in the details.

    Please Help.
    Thomas Mathews

  2. #2
    Addicted Member Mage33's Avatar
    Join Date
    Aug 2000
    Location
    Petaluma, California
    Posts
    138

    Thumbs up re: ..

    Well, the best way to do that would be to have the program insert itself into either the startup folder or the autoexec.bat file... the startup might be easier but is less fool-proof, but I'd still go with the startup in your situation. You might want to have two programs... one that ones before the restart and one that runs after. Otherwise you're going to have to make a bunch of checks to see whether it's the first time through or the second... in other words the prog is still getting killed, but you can re-load it when windows loads. Also, if you put it in the startup you can have it automatically remove itself easier as well. I don't know about the password things though, I seriously doubt it...
    Stephen Haney- 115 116 101 118 101 31 72 65 78 69 89
    -ShardsOfSilence.net- ^ My name in ASCII ^
    You forget something new every day
    | WinME | VB6 Pro | MSC++ | Lambda MOO |

  3. #3
    Guest
    Unless you're writing a DOS program (which being on this forum I seriously doubt!!) then putting it in the autoexec.bat file is just daft. You'll just get an error message saying "This program requires Microsoft Windows".

    The Startup folder is an easy one...cept if the startup folder or your shortcut is hidden then it won't be run. Also if you have multiple profiles setup (on a Win9x) machine it won't always be run.

    Your best bet is the "Run" or "RunServices" section of the registry.

    Code:
    Option Explicit
    
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const REG_SZ = 1
    Public Const ERROR_SUCCESS = 0& 'Well look at that...a successful error!!
    Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    
    Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
    Dim hCurKey As Long
    Dim lRegResult As Long
    
    lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    
    lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
    
    If lRegResult <> ERROR_SUCCESS Then
      'there is a problem
    End If
    
    lRegResult = RegCloseKey(hCurKey)
    End Sub
    
    Sub RegisterStartup()
    
        Dim sPath As String
    
        If Right$(App.Path, 1) = "\" Then
            sPath = App.Path & App.EXEName & ".exe"
        Else
            sPath = App.Path & "\" & App.EXEName & ".exe"
        End If
    
        SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", App.ProductName, sPath & " /afterreboot"
    
    End Sub
    
    Sub UnRegisterStartup()
        Dim hCurKey As Long
        Dim lRegResult As Long
    
        lRegResult = RegOpenKey(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", hCurKey)
        lRegResult = RegDeleteValue(hCurKey, App.ProductName)
        lRegResult = RegCloseKey(hCurKey)
    
    End Sub
    Bung that lot in a module and call "RegisterStartup" or "UnRegisterStartup" to err...well...I'll let you figure that one out.

    You don't need two versions of the program. Just put a check like this into your Form_Load event or Sub_Main procedure:

    Code:
    If Instr(Command, "/afterreboot" Then
      'Code to be run after reboot
    Else
      'Code to run before reboot
    End If

    [Edited by matthewralston on 08-10-2000 at 06:02 AM]

  4. #4
    Guest
    As for loggin in automatically...
    Use similar registry functions to the ones I gave you above to put the username into:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Winlogon\DefaultUserName

    and the password into:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Winlogon\DefaultPassword

    You should know that these are not encrypted or anything - they are just stored as plain text. Also remember to remove them from the registry afterwards.

    Unfortunately... the Login Prompt is still displayed and it takes a second or so for Windows to fillin the username and password (during which time someone could press Cancel or something). Ain't nothing I can do about that I'm affraid.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width