Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running at startup 1

Status
Not open for further replies.

wrighty43

Programmer
Aug 4, 2004
13
0
0
GB
i want a program that will run at startup. when the user opens the program for the first time i want it to write to the registry so the program will run when the computer startups up. every time the program is run after that i want it to check that it is still in the registry and if not write it again. i ahve tried 3 differnt ways of doin this, each way of writing to the registry uses alot of code and wasn't very efficient so i was wondering if their is a simpleish way to write to the registry.

thanks
chris
 
Hey!

I have a nice registry module written for StartUp thing.
Put the next code in the module and use the functions:

StartUpRead - returns 0 if item in the registry, 1 if not
StartUpAdd - adds to the startup
StartUpDelete - removes from startup

Code:
'Registry API
'// Funkcije //
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
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
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long

'//Konstante //
Const HKEY_LOCAL_MACHINE = &H80000002
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const REG_SZ = 1
'////////////////////

Public Function StartUpAdd(ByVal appName As String, ByVal Path As String) As Long
'Odpri Run
Dim hKey As Long
Dim ret As Long
Dim ret2 As Long
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, KEY_SET_VALUE, hKey)
If ret <> 0 Then 'error
 StartUpDodaj = "-1"
 Exit Function
End If
ret2 = RegSetValueEx(hKey, appName, 0, REG_SZ, Path & vbNullChar, Len(Path) + 1)
If ret2 <> 0 Then 'error
 StartUpAdd = "-2"
 Exit Function
End If
StartUpAdd = 0
'close key
Call RegCloseKey(hKey)
hKey = 0
'// End //
End Function


Public Function StartUpRead(ByVal appName As String) As Long
Dim hKey As Long
Dim ret As Long
Dim ret2 As Long
Dim buffer As String
buffer = String(255, vbNullChar)
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, KEY_QUERY_VALUE, hKey)
ret2 = RegQueryValueEx(hKey, appName, 0, 0, ByVal buffer, Len(buffer))
If ret2 <> 0 Then
 StartUpRead = 0
Else
 StartUpRead = 1
End If
RegCloseKey hKey

End Function


Public Function StartUpDelete(ByVal appName As String)
'Odpri Run
Dim hKey As Long
Dim ret As Long
Dim ret2 As Long
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, KEY_SET_VALUE, hKey)
If ret <> 0 Then 'error
 StartUpDelete = "-1"
 Exit Function
End If
ret2 = RegDeleteValue(hKey, appName)
If ret2 <> 0 Then 'error
 StartUpDelete = "-2"
 Exit Function
End If
StartUpDelete = 0
'Close key
Call RegCloseKey(hKey)
hKey = 0
End Function

If you need any further help let me know!
-Gregor



---------------------------------
LendPoint Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top