CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2001
    Location
    Philippines
    Posts
    9

    How to automatically load an application!

    To all gurus,

    How can I automatically load my application after the windows desktop has been loaded or before the windows desktop will load just like the network password of win 98?

    Thanks in advance gurus!!!!
    xnor


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to automatically load an application!

    Here is a way provided by (I am not sure...Vb2TheMax, may be)

    -note: to copy and paste this without loosing formattation of code,
    paste first in WordPad, and then select from WordPat and paste in VB editor

    Const REG_SZ = 1
    Const REG_BINARY = 3
    Const REG_DWORD = 4
    Const KEY_WRITE = &H20006 '((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or
    ' KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))

    private Declare Function RegDeleteValue Lib "advapi32.dll" Alias _
    "RegDeleteValueA" (byval hKey as Long, byval lpValueName as string) as Long
    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

    public Sub RunAtStartUp(byval Action as Integer, optional byval AppTitle as string, _
    optional byval AppPath as string)

    ' This is the key under which you must register the apps
    ' that must execute after every restart
    Const HKEY_CURRENT_USER = &H80000001
    Const REGKEY = "Software\Microsoft\Windows\CurrentVersion\Run"

    ' provide a default value for AppTitle
    AppTitle = LTrim$(AppTitle)
    If len(AppTitle) = 0 then AppTitle = App.Title

    ' this is the complete application path
    AppPath = LTrim$(AppPath)
    If len(AppPath) = 0 then
    ' if omitted, use the current application executable file
    AppPath = App.Path & IIf(Right$(App.Path, 1) <> "\", "\", _
    "") & App.EXEName & ".Exe"
    End If

    Select Case Action
    Case 0
    ' we must delete the key from the registry
    DeleteRegistryValue HKEY_CURRENT_USER, REGKEY, AppTitle
    Case 1
    ' we must add a value under the ...\RunOnce key
    SetRegistryValue HKEY_CURRENT_USER, REGKEY & "Once", AppTitle, _
    AppPath
    Case else
    ' we must add a value under the ....\Run key
    SetRegistryValue HKEY_CURRENT_USER, REGKEY, AppTitle, AppPath
    End Select

    End Sub


    ' Delete a registry value
    '
    ' Return true if successful, false if the value hasn't been found

    Function DeleteRegistryValue(byval hKey as Long, byval KeyName as string, _
    byval ValueName as string) as Boolean
    Dim handle as Long

    ' Open the key, exit if not found
    If RegOpenKeyEx(hKey, KeyName, 0, KEY_WRITE, handle) then Exit Function

    ' Delete the value (returns 0 if success)
    DeleteRegistryValue = (RegDeleteValue(handle, ValueName) = 0)
    ' Close the handle
    RegCloseKey handle
    End Function

    Function SetRegistryValue(byval hKey as Long, byval KeyName as string, _
    byval ValueName as string, value as Variant) as Boolean
    Dim handle as Long
    Dim lngValue as Long
    Dim strValue as string
    Dim binValue() as Byte
    Dim length as Long
    Dim retVal as Long

    ' Open the key, exit if not found
    If RegOpenKeyEx(hKey, KeyName, 0, KEY_WRITE, handle) then
    Exit Function
    End If

    ' three cases, according to the data type in Value
    Select Case VarType(value)
    Case vbInteger, vbLong
    lngValue = value
    retVal = RegSetValueEx(handle, ValueName, 0, REG_DWORD, lngValue, 4)
    Case vbString
    strValue = value
    retVal = RegSetValueEx(handle, ValueName, 0, REG_SZ, byval strValue, _
    len(strValue))
    Case vbArray + vbByte
    binValue = value
    length = UBound(binValue) - LBound(binValue) + 1
    retVal = RegSetValueEx(handle, ValueName, 0, REG_BINARY, _
    binValue(LBound(binValue)), length)
    Case else
    RegCloseKey handle
    Err.Raise 1001, , "Unsupported value type"
    End Select

    ' Close the key and signal success
    RegCloseKey handle
    ' signal success if the value was written correctly
    SetRegistryValue = (retVal = 0)
    End Function




    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    May 2001
    Location
    India, Bangalore
    Posts
    14

    Re: How to automatically load an application!

    You can automatically start any application by changing the registry entry under HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run this will make your application to run when ever the user logs into the system. (Tried in Win98)


  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Praveen is right

    I think it is better to use local machine instead of current user.
    In my codesnippet replace
    'Const HKEY_CURRENT_USER = &H80000001
    with
    Const HKEY_LOCAL_MACHINE = &H80000002
    and all HKEY_CURRENT_USER in code with the new constant: HKEY_LOCAL_MACHINE


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jun 2001
    Location
    Philippines
    Posts
    9

    Re: How to automatically load an application!

    Thank you guys......


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

Featured