Results 1 to 5 of 5

Thread: [RESOLVED] Make my application run at startup - Windows Seven Problem

Threaded View

  1. #1

    Thread Starter
    Addicted Member Witis's Avatar
    Join Date
    Jan 2011
    Location
    VB Forums Online Freedom Mode: Operational
    Posts
    213

    Resolved [RESOLVED] Make my application run at startup - Windows Seven Problem

    I tried to run this code to make my application run at startup, and it works in XP (in dev environment and compiled), although when I tried to use it on windows seven it works only when I am running it in the development environment, not when it is compiled which I find strange... Actually the returned error code from the function is 5 which in winerror.h is "access denied" (http://msdn.microsoft.com/en-us/library/ms819773.aspx) although I am logged in as a user that has full admin rights?

    Here is the code:
    0. In a new Project
    1. Add a module:
    Code:
    Option Explicit
    
    Public Const REG_SZ = 1 ' Unicode nul terminated string
    Public Const REG_BINARY = 3 ' Free form binary
    
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_USERS = &H80000003
    Public Const HKEY_PERFORMANCE_DATA = &H80000004
    Public Const HKEY_CURRENT_CONFIG = &H80000005
    Public Const HKEY_DYN_DATA = &H80000006
    
    Private Const ERROR_SUCCESS = 0&
    
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private 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
    Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    
    Public Function RegistrySaveString(hKey As Long, strPath As String, strValue As String, strData As String) As String
        Dim h&, r
    
        'Create a new key, or open the key already existing in that location, and return the handle to into h
        r = RegCreateKey(hKey, strPath, h)
        If r <> ERROR_SUCCESS Then RegistrySaveString = r: Exit Function
        
        'create a subkey beneath the key just created/opened and store a string in it.
        r = RegSetValueEx(h, strValue, 0, REG_SZ, ByVal strData, Len(strData))
        If r = ERROR_SUCCESS Then RegistrySaveString = "Success" Else RegistrySaveString = r
        
        'close the key
        r = RegCloseKey(h)
        
    End Function
    
    Public Function RegistryDeleteString(hKey As Long, strPath As String, strValue As String) As String
        Dim h&, r
        'Create a new key
        r = RegCreateKey(hKey, strPath, h)
        If r <> ERROR_SUCCESS Then RegistryDeleteString = r: Exit Function
        
        'Delete the key's value
        r = RegDeleteValue(h, strValue)
        If r = ERROR_SUCCESS Then RegistryDeleteString = "Success" Else RegistryDeleteString = r
        'close the key
        RegCloseKey h
    End Function
    
    Public Function MakeThisAppRunAtStartup() As String
        MakeThisAppRunAtStartup = RegistrySaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", App.EXEName, App.Path & "\" & App.EXEName & ".exe")
    End Function
    
    Public Function UnMakeThisAppRunAtStartup() As String
        UnMakeThisAppRunAtStartup = RegistryDeleteString(HKEY_LOCAL_MACHINE, "SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", App.EXEName)
    End Function
    
    ' Errors from these function are in winnerror.h
    ' 1 is invalid function
    ' 2 is specified file is not found
    ' 3 is specified path is not found
    ' 4 is too many open files
    ' 5 is access denied
    
    
    
    ' Most used keys to write to
    '"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"
    '"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"
    
    ' CAUTION: make sure the registry setting is deleted when your application is removed.
    2. Change the project-> Properties -> Make -> Title
    To a custom title which will appear in the registry

    3. Add two command buttons and a label to a form

    4. Add this code in the form
    Code:
    Private Sub Command1_Click()
    MsgBox MakeThisAppRunAtStartup
    End Sub
    
    Private Sub Command2_Click()
    MsgBox UnMakeThisAppRunAtStartup
    End Sub
    
    Private Sub Form_Load()
    Me.Label1 = "Registry Run at Startup" & vbCrLf & App.EXEName & vbCrLf & App.Path & "\" & App.EXEName & ".exe"
    Me.Label1.BackColor = &HFFFFFF
    Me.Command1.Caption = "Run at Startup"
    Me.Command2.Caption = "Do Not Run at Startup"
    End Sub
    or just download the attachment.
    Attached Files Attached Files
    Last edited by Witis; Mar 7th, 2011 at 03:22 AM.

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