Results 1 to 7 of 7

Thread: making an application run every time windows starts

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 1999
    Posts
    34
    Hi, I want my app to run every time windows (95) starts
    by using the registry settings.

    How do I do this

    PS I know how to alter the registry manually, I just don't know how to do via code.

    thanks

    MICK

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    I have never tested this on WIN95 but very sure it should work..

    Drop this in a module and change the name and location of your executable.

    Code:
    Option Explicit
    
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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 Const HKEY_LOCAL_MACHINE = &H80000002
    Private Const ERROR_SUCCESS = 0&
    Private Const REG_SZ = 1
    Private Const KEY_SET_VALUE = &H2
    
    Private Sub Main()
        Call SetProgramStartup("Project1", "C:\Project1.exe")
    End Sub
    
    Public Function SetProgramStartup(pProgramName As String, pProgramPath As String) As Boolean
        Dim lKeyHandle As Long
        Dim lRet As Long
        Dim strBuffer As String
        Dim strKey As String
        
        strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
        lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_SET_VALUE, lKeyHandle)
        If lRet = ERROR_SUCCESS Then
          lRet = RegSetValueEx(lKeyHandle, pProgramName, 0, REG_SZ, ByVal pProgramPath, Len(pProgramPath))
          RegCloseKey lKeyHandle
        End If
    End Function
    Chemically Formulated As:
    Dr. Nitro

  3. #3
    Guest
    Nitro, how would you remove the program from the registry if you didn't want it to start up anymore? Sorry, I'm not very familiar with the registry.

  4. #4
    Lively Member
    Join Date
    Jun 2000
    Location
    Belgium
    Posts
    77
    Matthew, you can use the RegDeleteKey

    Code:
    Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    
    Const HKEY_LOCAL_MACHINE = &H80000002
    
    Sub DeleteKey(KeyName as string)
     Dim lRet As Long
     lRet = RegDeleteKey(HKEY_LOCAL_MACHINE, KeyName)
    End Sub
    I don't test it, but i think it work!
    KWell

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 1999
    Posts
    34
    Nitro, thank you very much.
    that worked just fine, I'd like to ask another question if I may.

    What would be the best way to incorporate this into my
    application. what I want to do is when I install the
    program from floppies I want the program to automatically
    register itself into the registry using the code you
    supplied. Is this done via the installation procedure or
    elsewhere?

    thanks again

    MICK

    [Edited by bucko on 06-30-2000 at 09:45 PM]

  6. #6
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    Talking I am back!

    Hello Matthew and Bucko!

    Basic concepts of registry, Key is like a folder and Value is like a file. That is just an analogy.

    I made this personally for Matthew. Again drop this into a module and do a step through. While doing the debug step through, run Regedit.Exe under Start Button-Run to verify the actions of the code. You can press F5 to refresh the Regedit screen.

    It will:
    Create a Key
    Create a Value for a registry Key
    Read Data and Path from a Value
    Delete a Value from a registry Key
    Delete a Key and all of its Value

    BTW... Be very careful when playing with registry. I made this example to create a bogus KEY call "Testing".

    KWELL, your code will delete the entire KEY which means all values under it will be deleted. I think Matthew is just asking to remove one program rather than all programs from the StartUp.

    Bucko, I will get back with you on your second question.


    Code:
    Option Explicit
    
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Const ERROR_SUCCESS = 0&
    '****************************************************
      Private Const SYNCHRONIZE = &H100000
      Private Const KEY_CREATE_SUB_KEY = &H4
      Private Const KEY_SET_VALUE = &H2
        Private Const READ_CONTROL = &H20000
      Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    Private Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
      
      'Private Const SYNCHRONIZE = &H100000
      Private Const KEY_CREATE_LINK = &H20
      Private Const KEY_NOTIFY = &H10
      Private Const KEY_ENUMERATE_SUB_KEYS = &H8
      'Private Const KEY_CREATE_SUB_KEY = &H4
      'Private Const KEY_SET_VALUE = &H2
      Private Const KEY_QUERY_VALUE = &H1
      Private Const STANDARD_RIGHTS_ALL = &H1F0000
    Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    '****************************************************
    Private Const HKEY_CLASSES_ROOT = &H80000000
    Private Const HKEY_CURRENT_CONFIG = &H80000005
    Private Const HKEY_CURRENT_USER = &H80000001
    Private Const HKEY_DYN_DATA = &H80000006
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    Private Const HKEY_PERFORMANCE_DATA = &H80000004
    Private Const HKEY_USERS = &H80000003
    '****************************************************
    Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
    Private Const REG_OPTION_NON_VOLATILE = 0
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
    
    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 Const REG_SZ = 1
    
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    
    Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    
    Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    '****************************************************
    
    
    Dim str_KeyFolder As String
    Dim str_Value_ProgramName As String
    Dim str_Data_ProgramPath As String
    Dim lng_Handle_To_KeyFolder As Long  ' handle to the open or new registry key
    Dim lng_Return As Long
    
    Sub Main()
      'CONCEPTS:  KEY   - Folders in Window Explorers
      '           VALUE - Files
      '           DATA  - Path
    
      'PITFALL: Make sure there is no "\" at the end
      
      str_KeyFolder = "SOFTWARE\Microsoft\Windows\CurrentVersion\Testing"
      'str_KeyFolder = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"          'NOTICE: Uncomment this line will make program start everytime log on
      str_Value_ProgramName = "Project1"
      str_Data_ProgramPath = "C:\Test.exe"
    
        
      'Open RegEdit.Exe while step through this code - Refresh(F5) Regedit Screen after each procedure
      
      'OVERALL: Create a Key - Folder Column in RegEdit.Exe
      Call p_Create_Key
      
      'OVERALL: Create a Value for a registry Key - Name Column in RegEdit.Exe
      Call p_Create_Value(str_Value_ProgramName, str_Data_ProgramPath)
      
      'OVERALL: Read Data or File Path from a Value - Data Column in RegEdit.Exe
      Call p_Read_Data_From_Value(str_Value_ProgramName)
    
      'OVERALL: Delete a Value from a registry Key
      Call p_Delete_Value(str_Value_ProgramName)
      
      'OVERALL: Delete a Key and all of its Value
      Call p_Delete_Key
    End Sub
    
    Public Sub p_Create_Key()
        'PURPOSE: Set the name of the new key and the default security settings
        Dim sec_Attri As SECURITY_ATTRIBUTES  ' security settings of the key
        sec_Attri.nLength = Len(sec_Attri)    ' size of the structure
        sec_Attri.lpSecurityDescriptor = 0    ' default security level
        sec_Attri.bInheritHandle = True       ' the default value for this setting
        
        Dim lng_New_Or_Exist As Long          ' "1" if new key - "2" if existing key was opened
        
        'PURPOSE: Create or open the registry key and assign the handle to lng_Handle_To_KeyFolder
        lng_Return = RegCreateKeyEx(HKEY_LOCAL_MACHINE, str_KeyFolder, 0, vbNullString, _
                                    REG_OPTION_NON_VOLATILE, KEY_WRITE, sec_Attri, _
                                    lng_Handle_To_KeyFolder, lng_New_Or_Exist)
        
        If lng_Return <> ERROR_SUCCESS Then
          MsgBox "Error in p_Create_Key procedure!"
          End
        End If
        
        'PURPOSE: Close the registry key if it was successfully opened
        Call RegCloseKey(lng_Handle_To_KeyFolder)
    End Sub
    
    Public Sub p_Create_Value(str_Value_ProgramName As String, str_Data_ProgramPath As String)
        'PURPOSE: Open the registry key (str_KeyFolder) and _
                  assign the handle to lng_Handle_To_KeyFolder
        lng_Return = RegOpenKeyEx(HKEY_LOCAL_MACHINE, str_KeyFolder, 0, _
                                  KEY_ALL_ACCESS, lng_Handle_To_KeyFolder)
        
        If lng_Return = ERROR_SUCCESS Then
          'PURPOSE: Add the desired value to the key.
          Call RegSetValueEx(lng_Handle_To_KeyFolder, str_Value_ProgramName, _
                             0, REG_SZ, ByVal str_Data_ProgramPath, Len(str_Data_ProgramPath))
          
          'PURPOSE: Close the registry key if it was successfully opened
          Call RegCloseKey(lng_Handle_To_KeyFolder)
        End If
    End Sub
    
    Public Sub p_Read_Data_From_Value(str_Value_ProgramName As String)
        'PURPOSE: Open the registry key (str_KeyFolder) and _
                  assign the handle to lng_Handle_To_KeyFolder
        lng_Return = RegOpenKeyEx(HKEY_LOCAL_MACHINE, str_KeyFolder, 0, _
                                  KEY_ALL_ACCESS, lng_Handle_To_KeyFolder)
        
        If lng_Return = ERROR_SUCCESS Then
          'PURPOSE: Read data
          Dim str_Data As String * 50
          Call RegQueryValueEx(lng_Handle_To_KeyFolder, str_Value_ProgramName, _
                               0, REG_SZ, ByVal str_Data, Len(str_Data))
          MsgBox Left(str_Data, Len(str_Data))
    
          'PURPOSE: Close the registry key if it was successfully opened
          Call RegCloseKey(lng_Handle_To_KeyFolder)
        End If
    End Sub
    
    Public Sub p_Delete_Value(str_Value_ProgramName As String)
        'PURPOSE: Open the registry key (str_KeyFolder) and _
                  assign the handle to lng_Handle_To_KeyFolder
        lng_Return = RegOpenKeyEx(HKEY_LOCAL_MACHINE, str_KeyFolder, 0, _
                                  KEY_ALL_ACCESS, lng_Handle_To_KeyFolder)
        
        If lng_Return = ERROR_SUCCESS Then
          'PURPOSE: Delete the desired value from the key.
          Call RegDeleteValue(lng_Handle_To_KeyFolder, str_Value_ProgramName)
          
          'PURPOSE: Close the registry key if it was successfully opened
          Call RegCloseKey(lng_Handle_To_KeyFolder)
        End If
    End Sub
    
    Public Sub p_Delete_Key()
        lng_Return = RegDeleteKey(HKEY_LOCAL_MACHINE, str_KeyFolder)
        
        If lng_Return <> ERROR_SUCCESS Then
          MsgBox "Error in p_Delete_Key procedure!"
        End If
    End Sub
    It might look intimidating, but it is quite easy to understand. If you need anything else, you know where to reach me.
    Chemically Formulated As:
    Dr. Nitro

  7. #7
    Junior Member
    Join Date
    May 2000
    Location
    Alaska
    Posts
    21

    Thumbs up

    I am trying to make an exe startup automatically and came across your codes. Very detail! Thanks for sharing.

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