|
-
Apr 15th, 2000, 09:23 PM
#1
Thread Starter
Hyperactive Member
Hi,
I'm making a program and would like it to atomaticly start up when windows boots. To do this I'm going to add a shortcut called "C:\run.exe" to this registry directory:
HKLM\Software\Microsoft\Windows\CurrentVersion\Run
Any idea how I could do this using Visual Basic code? All I know is that an API call is required.
Thank you!
-
Apr 15th, 2000, 09:55 PM
#2
I don't know if this is what you wanted but here is
a way to access the Registry.
Code:
' function prototypes, constants, and type definitions
' for Windows 32-bit Registry API
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
' Registry API prototypes
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
'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
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hkey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hkey As Long, ByVal lpValueName As String) As Long
Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hkey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
'Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hkey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
'Declare Function RegGetKeySecurity Lib "advapi32.dll" (ByVal hKey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR, lpcbSecurityDescriptor As Long) As Long
Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" (ByVal hkey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long, ByVal hEvent As Long, ByVal fAsynchronus As Long) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult 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 RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long
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 RegReplaceKey Lib "advapi32.dll" Alias "RegReplaceKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpNewFile As String, ByVal lpOldFile As String) As Long
Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hkey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long
'Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
'Declare Function RegSetKeySecurity Lib "advapi32.dll" (ByVal hKey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR) As Long
Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData 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, lpData As Any, ByVal cbData As Long) As Long
Declare Function RegUnLoadKey Lib "advapi32.dll" Alias "RegUnLoadKeyA" (ByVal hkey As Long, ByVal lpSubKey As String) As Long
Public Const REG_NONE = 0 ' No value type
Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_EXPAND_SZ = 2 ' Unicode nul terminated string
Public Const REG_BINARY = 3 ' Free form binary
Public Const REG_DWORD = 4 ' 32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN = 4 ' 32-bit number (same as REG_DWORD)
Public Const REG_DWORD_BIG_ENDIAN = 5 ' 32-bit number
Public Const REG_LINK = 6 ' Symbolic Link (unicode)
Public Const REG_MULTI_SZ = 7 ' Multiple Unicode strings
Public Const REG_RESOURCE_LIST = 8 ' Resource list in the resource map
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9 ' Resource list in the hardware description
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
Public Const REG_CREATED_NEW_KEY = &H1 ' New Registry Key created
Public Const REG_OPENED_EXISTING_KEY = &H2 ' Existing Key opened
Public Const REG_WHOLE_HIVE_VOLATILE = &H1 ' Restore whole hive volatile
Public Const REG_REFRESH_HIVE = &H2 ' Unwind changes to last flush
Public Const REG_NOTIFY_CHANGE_NAME = &H1 ' Create or delete (child)
Public Const REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
Public Const REG_NOTIFY_CHANGE_LAST_SET = &H4 ' Time stamp
Public Const REG_NOTIFY_CHANGE_SECURITY = &H8
Public Const REG_LEGAL_CHANGE_FILTER = (REG_NOTIFY_CHANGE_NAME Or REG_NOTIFY_CHANGE_ATTRIBUTES Or REG_NOTIFY_CHANGE_LAST_SET Or REG_NOTIFY_CHANGE_SECURITY)
'Public Const REG_LEGAL_OPTION = (REG_OPTION_RESERVED Or REG_OPTION_NON_VOLATILE Or REG_OPTION_VOLATILE Or REG_OPTION_CREATE_LINK Or REG_OPTION_BACKUP_RESTORE)
' Reg Create Type Values...
Public Const REG_OPTION_RESERVED = 0 ' Parameter is reserved
Public Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Public Const REG_OPTION_VOLATILE = 1 ' Key is not preserved when system is rebooted
Public Const REG_OPTION_CREATE_LINK = 2 ' Created key is a symbolic link
Public Const REG_OPTION_BACKUP_RESTORE = 4 ' open for backup or restore
Public Const STANDARD_RIGHTS_READ = &H20000
Public Const STANDARD_RIGHTS_WRITE = &H20000
Public Const STANDARD_RIGHTS_EXECUTE = &H20000
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const DELETE = &H10000
Public Const READ_CONTROL = &H20000
Public Const WRITE_DAC = &H40000
Public Const WRITE_OWNER = &H80000
Public Const SYNCHRONIZE = &H100000
' Reg Key Security Options
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
'Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
'Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
'Public Const KEY_EXECUTE = (KEY_READ)
Public 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))
Public Sub savekey(hkey As Long, strpath As String)
Dim keyhand&
r = RegCreateKey(hkey, strpath, keyhand&)
r = RegCloseKey(keyhand&)
End Sub
Function RegQueryStringValue(ByVal hkey As Long, ByVal strValueName As String)
Dim lResult As Long
Dim lValueType As Long
Dim strBuf As String
Dim lDataBufSize As Long
On Error GoTo 0
lResult = RegQueryValueEx(hkey, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(hkey, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
RegQueryStringValue = StripTerminator(strBuf)
End If
End If
End If
End Function
Public Function getstring(hkey As Long, strpath As String, strvalue As String)
'This module allows easy access to the Windows 95 and NT Registry.
'It takes the following parameters:
'
'HKEY - one of the following constants:
'HKEY_CLASSES_ROOT
'HKEY_CURRENT_USER
'HKEY_LOCAL_MACHINE
'HKEY_USERS
'HKEY_PERFORMANCE_DATA
'
'strpath - the rest of the registry 'path' eg:
'"Control Panel\desktop"
'
'strvalue - A string that refers to the key that you want to retrieve eg:
'"Wallpaper"
'
'The Example above would be called as follows:
'
'dim strwallpaper as string
'strwallpaper = getstring(HKEY_CURRENT_USER, "Control Panel\desktop", "Wallpaper")
'
'This would return the current desktop wallpaper
'
'This module downloaded from VB-World at http://www.geocities.com/SiliconValley/Bay/8409/
Dim keyhand&
Dim datatype&
r = RegOpenKey(hkey, strpath, keyhand&)
getstring = RegQueryStringValue(keyhand&, strvalue)
r = RegCloseKey(keyhand&)
End Function
Function StripTerminator(ByVal strString As String) As String
Dim intZeroPos As Integer
intZeroPos = InStr(strString, Chr$(0))
If intZeroPos > 0 Then
StripTerminator = Left$(strString, intZeroPos - 1)
Else
StripTerminator = strString
End If
End Function
Public Sub savestring(hkey As Long, strpath As String, strvalue As String, strdata As String)
'This module allows easy access to the Windows 95 and NT Registry.
'It takes the following parameters:
'
'HKEY - one of the following constants:
'HKEY_CLASSES_ROOT
'HKEY_CURRENT_USER
'HKEY_LOCAL_MACHINE
'HKEY_USERS
'HKEY_PERFORMANCE_DATA
'
'strpath - the rest of the registry 'path' eg:
'"Control Panel\desktop"
'
'strvalue - A string that refers to the key that you want to save eg:
'"Wallpaper"
'
'strdata - the string you want to save against strvalue eg:
'"c:\windows\clouds.bmp"
'
'The Example above would be called as follows:
'
'dim strwallpaper as string
'call savestring(HKEY_CURRENT_USER, "Control Panel\desktop", "Wallpaper", "C:\Windows\Clouds.bmp")
'
'If this 'path' does not exist, it will be created.
'
'This module downloaded from VB-World at http://www.geocities.com/SiliconValley/Bay/8409/
Dim keyhand&
r = RegCreateKey(hkey, strpath, keyhand&)
r = RegSetValueEx(keyhand&, strvalue, 0, REG_SZ, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand&)
End Sub
Pretty long, but it works.
-
Apr 15th, 2000, 10:01 PM
#3
Thread Starter
Hyperactive Member
Thanks Megatron for the code but its all a little vague. I would like the proper code with no added stuff.
-
Apr 16th, 2000, 10:51 AM
#4
Addicted Member
Hello Rino_2,
Megatron gave you everthing that you would need to do what you're asking, however, I understand how it could look a little daunting. Anyway here's the code to do what you need:
Declarations
Code:
Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
Private Const REG_SZ As Integer = 1
Private Const KEY_SET_VALUE As Integer = &H2
Private Declare Function RegCloseKey _
Lib "advapi32.dll" _
(ByVal hkey As Long) 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 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
Function to write to Registry
Code:
Private Function mbWriteValueToReg_SZ( _
ByVal vsKey As String, _
ByVal vsSubKey As String, _
ByVal vsValueName As String, _
ByVal vsValueData As String) As Boolean
Dim bWriteSuccess As Boolean
Dim hRegKey As Long
Dim lRetVal As Long
lRetVal = RegOpenKeyEx(vsKey, vsSubKey, 0, KEY_SET_VALUE, hRegKey)
If lRetVal = ERROR_SUCCESS Then
lRetVal = RegSetValueEx(hRegKey, vsValueName, 0, REG_SZ, _
ByVal vsValueData, Len(vsValueData))
bWriteSuccess = CBool(lRetVal)
lRetVal = RegCloseKey(hRegKey)
If lRetVal <> ERROR_SUCCESS Then
bWriteSuccess = False
End If
End If
mbWriteValueToReg_SZ = bWriteSuccess
End Function
To call the write function
Code:
Call mbWriteValueToReg_SZ(HKEY_LOCAL_MACHINE, _
"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", _
App.EXEName, App.Path)
C'est tout, hope it's more clear now. Later.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|