Results 1 to 13 of 13

Thread: Autostart App from Registry

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Atlanta, GA
    Posts
    80

    Question

    Isn't there a way to add a key to the registry so that an App can be autostarted without out having to create a shortcut in the Startup folder?

    I would like the app to run for all users if possible, but current user is fine too.

    Thanks,
    Kevin

    VB6 Professional w/ SP4

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    They are stored under:
    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

    The key name is a unique name to identify your application, and the value is the path to the executable (parameters are OK).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Atlanta, GA
    Posts
    80
    I did find a post similar to mine shortly after I submitted. Never the less... thanks for your quick reply! I have done so, and it works like a charm....

  4. #4
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    How can i use 'parameters' i think it is so when someone double clicks on a file that my app uses it will open my app and open the specified file?

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    First you need to associate your filetype to your app, then in runtime you retrieve the parameter line by Command$ function;
    Code:
    Yourfile=Command$
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    When you set up your file association, use:

    "c:\myapp\myapp.exe" "%1"

    The %1 will be expanded into the filename.

    Although - what I meant by 'parameters' was that you can specify absolute parameters and they'll work, but no specific expansions will be performed. Anyone got any details for environment variables?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Basic concepts of registry, Key is like a folder and Value is like a file. That is just an analogy.

    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".
    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 = & H100 000
      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 = & H100 000
      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.
    Chemically Formulated As:
    Dr. Nitro

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    For a full example program, go to http://www.katisha.demon.co.uk/vb/files/registry.zip.
    It demonstrates saving/loading dwords and strings.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Cheers, this has sorted my problem, but how would i load command$ into a string called WhatToDo

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What, just WhatToDo = Command?

    You need to parse out the parameters as your program requires.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    How do i make it(with vb code) so when my program is run i get the %1 part of "D:\MyProg.exe " "%1" and then get my app to open that %1

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Set up a file association in the usual way, then use Command to get the file name.
    Code:
    Private Sub Form_Load()
        Dim Result()
        Dim sFile as String
        Result = Split(Command)
        sFile = CStr(Result(0))
        ' Then load as normal
    End Sub
    Obviously, you'd need to check if there was a filename. Also, I am not familiar with VB6's Split function (I wrote my own because I'm still in VB5 - I could post it here), so this may not work as such, but the principle is OK.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Cheers, i'll try it now

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