vb程序代码

各位大侠:我自己写的程序 怎么设置开机就运行啊 vb代码是什么?????

要通过注册表操作来进行!注册表RUN下写入键值可以达到这个效果!
比如你按下一个按钮来实现开机自启动!
Private Sub command1_Click()
CreateKey "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"
SetStringValue "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "这里写键值的名称随你自定义的", "这里写你程序的路径" '开机启动自己
end sub
然后你添加个模块,里面写(我写的是通用的模块可以进行注册表的各项操作便于你学习就都写出来了,有问题可以百度留言或Q340231101 ):
Type FILETIME
lLowDateTime As Long
lHighDateTime As Long
End Type

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 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 RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) 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, ByVal lpData As String, lpcbData As Long) As Long
Declare Function RegQueryValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As Long, lpcbData 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 RegSetValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Long, ByVal cbData As Long) As Long
Declare Function RegSetValueExB Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Byte, ByVal cbData As Long) As Long

Const ERROR_SUCCESS = 0&
Const ERROR_BADDB = 1009&
Const ERROR_BADKEY = 1010&
Const ERROR_CANTOPEN = 1011&
Const ERROR_CANTREAD = 1012&
Const ERROR_CANTWRITE = 1013&
Const ERROR_OUTOFMEMORY = 14&
Const ERROR_INVALID_PARAMETER = 87&
Const ERROR_ACCESS_DENIED = 5&
Const ERROR_NO_MORE_ITEMS = 259&
Const ERROR_MORE_DATA = 234&

Const REG_NONE = 0&
Const REG_SZ = 1&
Const REG_EXPAND_SZ = 2&
Const REG_BINARY = 3&
Const REG_DWORD = 4&
Const REG_DWORD_LITTLE_ENDIAN = 4&
Const REG_DWORD_BIG_ENDIAN = 5&
Const REG_LINK = 6&
Const REG_MULTI_SZ = 7&
Const REG_RESOURCE_LIST = 8&
Const REG_FULL_RESOURCE_DESCRIPTOR = 9&
Const REG_RESOURCE_REQUIREMENTS_LIST = 10&

Const KEY_QUERY_VALUE = &H1&
Const KEY_SET_VALUE = &H2&
Const KEY_CREATE_SUB_KEY = &H4&
Const KEY_ENUMERATE_SUB_KEYS = &H8&
Const KEY_NOTIFY = &H10&
Const KEY_CREATE_LINK = &H20&
Const READ_CONTROL = &H20000
Const WRITE_DAC = &H40000
Const WRITE_OWNER = &H80000
Const SYNCHRONIZE = &H100000
Const STANDARD_RIGHTS_REQUIRED = &HF0000
Const STANDARD_RIGHTS_READ = READ_CONTROL
Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
Const KEY_WRITE = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Const KEY_EXECUTE = KEY_READ

Dim hKey As Long, MainKeyHandle As Long
Dim rtn As Long, lBuffer As Long, sBuffer As String
Dim lBufferSize As Long
Dim lDataSize As Long
Dim ByteArray() As Byte

'This constant determins wether or not to display error messages to the
'user. I have set the default value to False as an error message can and
'does become irritating after a while. Turn this value to true if you want
'to debug your programming code when reading and writing to your system
'registry, as any errors will be displayed in a message box.
'这个 constant(常量) 用来阻止是否向用户显示错误信息
'我已经将默认值设置为 False
'当读写您的系统注册表时,如果您想调试您的程序代码,把这个值设置为 True
'所有错误将显示在一个信息框里

Const DisplayErrorMsg = False

Function SetDWORDValue(SubKey As String, Entry As String, Value As Long)

Call ParseKey(SubKey, MainKeyHandle)

If MainKeyHandle Then
rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_WRITE, hKey) 'open the key '打开键
If rtn = ERROR_SUCCESS Then 'if the key was open successfully then '如果键成功打开,那么
rtn = RegSetValueExA(hKey, Entry, 0, REG_DWORD, Value, 4) 'write the value '写入值
If Not rtn = ERROR_SUCCESS Then 'if there was an error writting the value '如果写入值发生错误
If DisplayErrorMsg = True Then 'if the user want errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'display the error '显示错误
End If
End If
rtn = RegCloseKey(hKey) 'close the key '关闭键
Else 'if there was an error opening the key '如果打开键时发生错误
If DisplayErrorMsg = True Then 'if the user want errors displayed 如果用户想显示错误
MsgBox ErrorMsg(rtn) 'display the error '显示错误
End If
End If
End If

End Function
Function GetDWORDValue(SubKey As String, Entry As String)

Call ParseKey(SubKey, MainKeyHandle)

If MainKeyHandle Then
rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_READ, hKey) 'open the key '打开键
If rtn = ERROR_SUCCESS Then 'if the key could be opened then '如果键能被打开,那么
rtn = RegQueryValueExA(hKey, Entry, 0, REG_DWORD, lBuffer, 4) 'get the value from the registry '从注册表中得到键值
If rtn = ERROR_SUCCESS Then 'if the value could be retreived then '如果键值能被获取,那么
rtn = RegCloseKey(hKey) 'close the key '关闭键
GetDWORDValue = lBuffer 'return the value 返回键值
Else 'otherwise, if the value couldnt be retreived '否则,如果键值不能被获取
GetDWORDValue = "Error" 'return Error to the user '返回错误给用户
If DisplayErrorMsg = True Then 'if the user wants errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'tell the user what was wrong '告知用户出了什么错
End If
End If
Else 'otherwise, if the key couldnt be opened '否则,如果键不能被打开
GetDWORDValue = "Error" 'return Error to the user '返回错误给用户
If DisplayErrorMsg = True Then 'if the user wants errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'tell the user what was wrong '告知用户出了什么错
End If
End If
End If

End Function
Function SetBinaryValue(SubKey As String, Entry As String, Value As String)

Call ParseKey(SubKey, MainKeyHandle)

If MainKeyHandle Then
rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_WRITE, hKey) 'open the key '打开键
If rtn = ERROR_SUCCESS Then 'if the key was open successfully then '如果键成功打开,那么
lDataSize = Len(Value)
ReDim ByteArray(lDataSize)
For I = 1 To lDataSize
ByteArray(I) = Asc(Mid$(Value, I, 1))
Next
rtn = RegSetValueExB(hKey, Entry, 0, REG_BINARY, ByteArray(1), lDataSize) 'write the value
If Not rtn = ERROR_SUCCESS Then 'if the was an error writting the value '如果键成功打开,那么
If DisplayErrorMsg = True Then 'if the user want errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'display the error '显示错误
End If
End If
rtn = RegCloseKey(hKey) 'close the key '关闭键
Else 'if there was an error opening the key '如果打开键时发生错误
If DisplayErrorMsg = True Then 'if the user wants errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'display the error '显示错误
End If
End If
End If

End Function

Function GetBinaryValue(SubKey As String, Entry As String)

Call ParseKey(SubKey, MainKeyHandle)

If MainKeyHandle Then
rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_READ, hKey) 'open the key '打开键
If rtn = ERROR_SUCCESS Then 'if the key could be opened '如果键能被打开
lBufferSize = 1
rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, 0, lBufferSize) 'get the value from the registry '从注册表得到键值
sBuffer = Space(lBufferSize)
rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, sBuffer, lBufferSize) 'get the value from the registry '从注册表得到键值
If rtn = ERROR_SUCCESS Then 'if the value could be retreived then '如果键值能被获取,那么
rtn = RegCloseKey(hKey) 'close the key '关闭键
GetBinaryValue = sBuffer 'return the value to the user '返回错误给用户
Else 'otherwise, if the value couldnt be retreived '否则,如果键值不能被获取
GetBinaryValue = "Error" 'return Error to the user '返回错误给用户
If DisplayErrorMsg = True Then 'if the user wants to errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'display the error to the user '显示错误
End If
End If
Else 'otherwise, if the key couldnt be opened '否则,如果键不能被打开
GetBinaryValue = "Error" 'return Error to the user '返回错误给用户
If DisplayErrorMsg = True Then 'if the user wants to errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'display the error to the user '显示错误
End If
End If
End If

End Function
Function DeleteKey(Keyname As String)

Call ParseKey(Keyname, MainKeyHandle)

If MainKeyHandle Then
rtn = RegOpenKeyEx(MainKeyHandle, Keyname, 0, KEY_WRITE, hKey) 'open the key '打开键
If rtn = ERROR_SUCCESS Then 'if the key could be opened then '如果键能被打开,那么
rtn = RegDeleteKey(hKey, Keyname) 'delete the key '删除键
rtn = RegCloseKey(hKey) 'close the key '关闭键
End If
End If

End Function

Function GetMainKeyHandle(MainKeyName As String) As Long

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006

Select Case MainKeyName
Case "HKEY_CLASSES_ROOT"
GetMainKeyHandle = HKEY_CLASSES_ROOT
Case "HKEY_CURRENT_USER"
GetMainKeyHandle = HKEY_CURRENT_USER
Case "HKEY_LOCAL_MACHINE"
GetMainKeyHandle = HKEY_LOCAL_MACHINE
Case "HKEY_USERS"
GetMainKeyHandle = HKEY_USERS
Case "HKEY_PERFORMANCE_DATA"
GetMainKeyHandle = HKEY_PERFORMANCE_DATA
Case "HKEY_CURRENT_CONFIG"
GetMainKeyHandle = HKEY_CURRENT_CONFIG
Case "HKEY_DYN_DATA"
GetMainKeyHandle = HKEY_DYN_DATA
End Select

End Function

Function ErrorMsg(lErrorCode As Long) As String

'If an error does accurr, and the user wants error messages displayed, then
'如果一个错误发生了,且用户想显示错误信息,那么
'display one of the following error messages
'显示以下错误信息之一

Select Case lErrorCode
Case 1009, 1015
GetErrorMsg = "注册表数据库已损坏!(The Registry Database is corrupt!)"
Case 2, 1010
GetErrorMsg = "错误键名(Bad Key Name)"
Case 1011
GetErrorMsg = "无法打开键(Can't Open Key)"
Case 4, 1012
GetErrorMsg = "无法读取键(Can't Read Key)"
Case 5
GetErrorMsg = "存取此键被拒绝(Access to this key is denied)"
Case 1013
GetErrorMsg = "无法写入键(Can't Write Key)"
Case 8, 14
GetErrorMsg = "内存不足(Out of memory)"
Case 87
GetErrorMsg = "错漏的参数(Invalid Parameter)"
Case 234
GetErrorMsg = "缓冲器内太多数据等待被分配.(There is more data than the buffer has been allocated to hold.)"
Case Else
GetErrorMsg = "不明错误代码:" & Str$(lErrorCode) & "(Undefined Error Code: " & Str$(lErrorCode) & ")"
End Select

End Function

Function GetStringValue(SubKey As String, Entry As String)

Call ParseKey(SubKey, MainKeyHandle)

If MainKeyHandle Then
rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_READ, hKey) 'open the key '打开键
If rtn = ERROR_SUCCESS Then 'if the key could be opened then '如果键能被打开,那么
sBuffer = Space(255) 'make a buffer '创建一个缓冲
lBufferSize = Len(sBuffer)
rtn = RegQueryValueEx(hKey, Entry, 0, REG_SZ, sBuffer, lBufferSize) 'get the value from the registry '从注册表中得到键值
If rtn = ERROR_SUCCESS Then 'if the value could be retreived then '如果键值能被获取,那么
rtn = RegCloseKey(hKey) 'close the key '关闭键
sBuffer = Trim(sBuffer)
GetStringValue = Left(sBuffer, Len(sBuffer) - 1) 'return the value to the user '返回键值给用户
Else 'otherwise, if the value couldnt be retreived '否则,如果键无法被获取
GetStringValue = "Error" 'return Error to the user '返回错误给用户
If DisplayErrorMsg = True Then 'if the user wants errors displayed then '如果用户想显示错误信息,那么
MsgBox ErrorMsg(rtn) 'tell the user what was wrong '告知用户出了什么错
End If
End If
Else 'otherwise, if the key couldnt be opened '否则,如果键无法被打开
GetStringValue = "Error" 'return Error to the user '返回错误给用户
If DisplayErrorMsg = True Then 'if the user wants errors displayed then '如果用户想显示错误,那么
MsgBox ErrorMsg(rtn) 'tell the user what was wrong '告知用户出了什么错
End If
End If
End If

End Function

Private Sub ParseKey(Keyname As String, Keyhandle As Long)

rtn = InStr(Keyname, "\") 'return if "\" is contained in the Keyname '返回如果键名中包含"\"

If Left(Keyname, 5) <> "HKEY_" Or Right(Keyname, 1) = "\" Then 'if the is a "\" at the end of the Keyname then '如果"\"出现在键名结尾
MsgBox "Incorrect Format:" + Chr(10) + Chr(10) + Keyname 'display error to the user '显示错误给用户
Exit Sub 'exit the procedure '退出程序
ElseIf rtn = 0 Then 'if the Keyname contains no "\" '如果键名没包含"\"
Keyhandle = GetMainKeyHandle(Keyname)
Keyname = "" 'leave Keyname blank '让键名留空
Else 'otherwise, Keyname contains "\" '另外,键名包含"\"
Keyhandle = GetMainKeyHandle(Left(Keyname, rtn - 1)) 'seperate the Keyname '分离键名
Keyname = Right(Keyname, Len(Keyname) - rtn)
End If

End Sub
Function CreateKey(SubKey As String)

Call ParseKey(SubKey, MainKeyHandle)

If MainKeyHandle Then
rtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'create the key '创建键
If rtn = ERROR_SUCCESS Then 'if the key was created then '如果键被创建
rtn = RegCloseKey(hKey) 'close the key '关闭键
End If
End If

End Function
Function SetStringValue(SubKey As String, Entry As String, Value As String)

Call ParseKey(SubKey, MainKeyHandle)

If MainKeyHandle Then
rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_WRITE, hKey) 'open the key '打开键
If rtn = ERROR_SUCCESS Then 'if the key was open successfully then '如果键被成功打开
rtn = RegSetValueEx(hKey, Entry, 0, REG_SZ, ByVal Value, Len(Value)) 'write the value '写入值
If Not rtn = ERROR_SUCCESS Then 'if there was an error writting the value '如果写入值时发生错误
If DisplayErrorMsg = True Then 'if the user wants errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'display the error '显示错误
End If
End If
rtn = RegCloseKey(hKey) 'close the key '关闭键
Else 'if there was an error opening the key '如果打开键时发生错误
If DisplayErrorMsg = True Then 'if the user wants errors displayed '如果用户想显示错误
MsgBox ErrorMsg(rtn) 'display the error '显示错误
End If
End If
End If

End Function
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-09-27
将你写的代码保存为可执行文件

然后将该文件(或者该文件的快捷方式)复制到

C:\Documents and Settings\Administrator\「开始」菜单\程序\启动

即可
第2个回答  2009-09-27
还是写注册表吧,RUN项
相似回答