Click to See Complete Forum and Search --> : How to automatically load an application!


xnor21
June 19th, 2001, 07:15 PM
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

Cimperiali
June 20th, 2001, 02:12 AM
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.

praveen b p
June 20th, 2001, 02:37 AM
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)

Cimperiali
June 20th, 2001, 02:57 AM
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.

xnor21
June 20th, 2001, 07:37 PM
Thank you guys......