如何用VB编写一个开机自动运行得程序?

2024-12-28 19:25:38
推荐回答(5个)
回答1:

你这个主要就是在form_load事件下写代码。

代码的功能就是将此可执行文件在注册表里的启动项注册一下。让程序跟着系统一起启动。

Dim strString As String
Dim keyhand As Long
Dim r As Long
strString = App.Path & "\" & App.EXEName & ".exe"
r = RegCreateKey(HKEY_LOCAL_MACHINE, "software\microsoft\windows\currentversion\run", keyhand)
r = RegSetValueEx(keyhand, "jsjuq", 0, REG_SZ, ByVal strString, Len(strString))
r = RegCloseKey(keyhand)

下面的这些代码。你保存为一个BAS。模块文件。这里面有所有的处理注册表的函数和操作步骤。

Option Explicit

'读写注册表任何地方的函数
' 用法见下面
' 杨光宏 http://cako.126.com(VB技巧手册)

'用于操作注册表的API函数定义 -begin-
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 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 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 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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
'用于操作注册表的API函数定义 -end-
'用于操作注册表的常数定义 -begin-
'预定义的根结点
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_USERS = &H80000003
'预定义的键值类型
Public Const REG_NONE = 0
Public Const REG_SZ = 1
Public Const REG_EXPAND_SZ = 2
Public Const REG_BINARY = 3
Public Const REG_DWORD = 4
Public Const REG_DWORD_LITTLE_ENDIAN = 4
Public Const REG_DWORD_BIG_ENDIAN = 5
Public Const REG_LINK = 6
Public Const REG_MULTI_SZ = 7
Public Const REG_RESOURCE_LIST = 8
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
#If WinNT Then
'打开注册文件的方式
Public Const KEY_EVENT = &H1
Public Const KEY_NOTIFY = &H10
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_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_EXECUTE = (KEY_READ)
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
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))
#End If
'注册文件错误描述
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_REGISTRY_RECOVERED = 1014&
Const ERROR_REGISTRY_CORRUPT = 1015&
Const ERROR_REGISTRY_IO_FAILED = 1016&
Const ERROR_NOT_REGISTRY_FILE = 1017&
Const ERROR_KEY_DELETED = 1018&
Const ERROR_NO_LOG_SPACE = 1019&
Const ERROR_KEY_HAS_CHILDREN = 1020&
Const ERROR_CHILD_MUST_BE_VOLATILE = 1021&
Const ERROR_RXACT_INVALID_STATE = 1369&
' 自定义注册文件错误
Const REGAGENT_NOKEY = -1002
Const REGAGENT_NOSUBKEY = -1003

Public glStatus As Long

'用于操作注册表的常数定义 -end-
Function gfsGetKeyStringValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As String

'功 能:从注册表中取得串值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: gfsGetKeyStringValue String 取得的注册表串值
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
Dim llBufferSize As Long '需读取串的串值长度
Dim lsKeyValue As String '存放读取的串值

'预先置为空
gfsGetKeyStringValue = Empty

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Function
End If

'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)

If glStatus = ERROR_SUCCESS Then '成功则取需读取字串的串值大小
glStatus = RegQueryValueEx(llKeyID, psSubKey, 0&, REG_SZ, 0&, llBufferSize)
If llBufferSize < 2 Then '空值
glStatus = RegCloseKey(llKeyID)
Else '有值,正式读取串值
lsKeyValue = String(llBufferSize + 1, " ")
glStatus = RegQueryValueEx(llKeyID, psSubKey, 0&, REG_SZ, ByVal lsKeyValue, llBufferSize)
If glStatus = ERROR_SUCCESS Then
gfsGetKeyStringValue = Left$(lsKeyValue, llBufferSize - 1)
End If
glStatus = RegCloseKey(llKeyID)
End If
End If
End Function
Function gflGetKeyBinaryValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As Long

'功 能:从注册表中取得二进制值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: gflGetKeyBinaryValue Long 取得的注册表二进制值
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
Dim llKeyValue As Long '存放读取的二进制值

'预先置为空
gflGetKeyBinaryValue = Empty

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Function
End If

'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)

If glStatus = ERROR_SUCCESS Then '成功则取二进制值
glStatus = RegQueryValueEx(llKeyID, psSubKey, 0&, REG_BINARY, llKeyValue, Len(llKeyValue))
If glStatus = ERROR_SUCCESS Then
gflGetKeyBinaryValue = llKeyValue
End If
glStatus = RegCloseKey(llKeyID)
End If
End Function
Function gflGetKeyDwordValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As Long
'ok
'功 能:从注册表中取得双字值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: gflGetKeyDwordValue Long 取得的注册表双字值
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID
Dim llKeyValue As Long '存放读取的双字值

'预先置为空
gflGetKeyDwordValue = Empty

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Function
End If

'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)

If glStatus = ERROR_SUCCESS Then '成功则取双字值
glStatus = RegQueryValueEx(llKeyID, psSubKey, 0&, REG_DWORD, llKeyValue, Len(llKeyValue))
If glStatus = ERROR_SUCCESS Then
gflGetKeyDwordValue = llKeyValue
End If
glStatus = RegCloseKey(llKeyID)
End If
End Function
Sub gpvSetKeyStringValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal psKeyValue As String)
'ok
'功 能:设置注册表中的串值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' psKeyValue String 要设置的串值
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Sub
End If

'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then '成功则设置值
If Len(psKeyValue) = 0 Then '设为空值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0&, REG_SZ, 0&, 0&)
Else '设为正常值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0&, REG_SZ, ByVal psKeyValue, Len(psKeyValue) + 1)
End If
glStatus = RegCloseKey(llKeyID)
End If

End Sub
Sub gpvSetKeyDwordValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal plKeyValue As Long)
'ok
'功 能:设置注册表中的双字值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' plKeyValue Long 要设置双字值
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Sub
End If

'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)

If glStatus = ERROR_SUCCESS Then '成功则设置值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0&, REG_DWORD, plKeyValue, Len(plKeyValue))
glStatus = RegCloseKey(llKeyID)
End If

End Sub
Sub gpvSetKeyBinaryValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal plKeyValue As Long)
'ok
'功 能:设置注册表中的二进制值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' plKeyValue Long 要设置的二进制值
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Sub
End If

'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)

If glStatus = ERROR_SUCCESS Then '成功则设置值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0&, REG_BINARY, plKeyValue, Len(plKeyValue))
glStatus = RegCloseKey(llKeyID)
End If

End Sub
Sub gpvSetKeyMULTIValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal psKeyValue As String)
'ok
'功 能:设置注册表中的长字符串值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' psKeyValue String 要设置的串值
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '打开键的ID

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置(子键未设置则读默认值)
glStatus = REGAGENT_NOKEY
Exit Sub
End If

'首先打开主键
glStatus = RegOpenKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then '成功则设置值
If Len(psKeyValue) = 0 Then '设为空值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0&, REG_MULTI_SZ, 0&, 0&)
Else '设为正常值
glStatus = RegSetValueEx(llKeyID, psSubKey, 0&, REG_MULTI_SZ, ByVal psKeyValue, Len(psKeyValue) + 1)
End If
glStatus = RegCloseKey(llKeyID)
End If

End Sub
Function gflCreateKey(ByVal plKey As Long, ByVal psKey As String) As Long
'ok
'功 能:创建注册表中的主键
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' 输出: gflCreateKey Long 创建的主键ID
' 影响: glStatus Long 状态值
Dim llKeyID As Long '键ID

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置
glStatus = REGAGENT_NOKEY
Exit Function
End If

'创建主键
glStatus = RegCreateKey(plKey, psKey, llKeyID)
If glStatus = ERROR_SUCCESS Then
gflCreateKey = llKeyID
End If

End Function
Sub gpvDeleteKey(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String)
'ok
'功 能:删除注册表中的主键
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: 无
' 影响: glStatus Long 状态值
Dim llKeyID As Long '键ID

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置
glStatus = REGAGENT_NOKEY
Exit Sub
End If

'利用创建主键判断是否存在主键
glStatus = RegCreateKey(plKey, psKey, llKeyID)
glStatus = RegDeleteKey(llKeyID, ByVal psSubKey)

End Sub
Sub gpvDeleteKeyValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String)
'ok
'功 能:删除注册表中的键值
'参 数:
' 输入: plKey Long 根键名
' psKey String 主键名
' psSubKey String 子键名
' 输出: 无
' 影响: glStatus Long 状态值

Dim llKeyID As Long '键ID

glStatus = ERROR_SUCCESS '假设成功

'确定参数有效
If Len(psKey) = 0 Then '主键未设置
glStatus = REGAGENT_NOKEY
Exit Sub
End If

'利用创建主键判断是否存在主键
glStatus = RegCreateKey(plKey, psKey, llKeyID)
glStatus = RegDeleteValue(llKeyID, ByVal psSubKey)

End Sub

'Function gfsGetKeyStringValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As String
'功 能:从注册表中取得串值

'Function gflGetKeyBinaryValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As Long
'功 能:从注册表中取得二进制值

'Function gflGetKeyDwordValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String) As Long
'功 能:从注册表中取得双字值

'Sub gpvSetKeyStringValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal psKeyValue As String)
'功 能:设置注册表中的串值

'Sub gpvSetKeyDwordValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal plKeyValue As Long)
'功 能:设置注册表中的双字值

'Sub gpvSetKeyBinaryValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String, ByVal plKeyValue As Long)
'功 能:设置注册表中的二进制值

'Function gflCreateKey(ByVal plKey As Long, ByVal psKey As String) As Long
'功 能:创建注册表中的主键

'Sub gpvDeleteKey(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String)
'功 能:删除注册表中的主键

'Sub gpvDeleteKeyValue(ByVal plKey As Long, ByVal psKey As String, ByVal psSubKey As String)
'功 能:删除注册表中的键值

回答2:

不需要 开始 程序 启动 里 把文件放倒这里开机就会自动运行 要是想按时间运行 还有计划任务

下面的代码功能是修改注册表,把D:\wintalen\soft\GKWebTool.exe设置库开机自启动: Set my = CreateObject("WScript.Shell") my.regWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\GKWebTool", dirwin & "D:\wintalen\soft\GKWebTool.exe", "REG_SZ" 你可以把以上代码放到你的软件的启动窗口的Form_Load()事件中。

启动Windows程序自动运行(放入到注册表的Run)。
Option Explicit
'---------------处理注册表的函数-----------------------
Private Declare Function RegCreateKey& Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey&, ByVal lpszSubKey$, lphKey&)
Private 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

Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1

'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
Private Sub Command1_Click()
'声明变量
Dim sKeyName As String, sKeyValue As String, sKeyValueIcon As String
Dim Ret As Integer, lphKey As Long

sKeyName = "Software\Microsoft\Windows\CurrentVersion\Run"
sKeyValue = App.Path & IIf(Len(App.Path) > 3, "\" & "form1.exe", "form1.exe")
Ret = RegCreateKey&(HKEY_LOCAL_MACHINE, sKeyName, lphKey)
Ret = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)

End Sub

回答3:

有这么复杂吗?

Const REG_SZ As Long = 1
Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, 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
Private Sub Form_Load()
Dim paths As String
paths = App.Path & "\AA.exe"
Dim hKey As Long
Dim myexe As String
Dim myint As Integer
myint = Len(paths) - InStrRev(paths, "\")
myexe = Right(paths, myint)
If Text1.Text <> "" Then
RegCreateKey HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", hKey
RegSetValueEx hKey, myexe, 0, REG_SZ, ByVal paths, Len(paths) + 6
RegCloseKey hKey
End If
End Sub

你把上面的代码整个复制到代码窗口中(覆盖原有的内容),别复制到某个事件下。
如上,其实只要把程序加到注册表里就行了。

回答4:

Dim strString As String
Dim keyhand As Long
Dim r As Long
strString = App.Path & "\" & App.EXEName & ".exe"
r = RegCreateKey(HKEY_LOCAL_MACHINE, "software\microsoft\windows\currentversion\run", keyhand)
r = RegSetValueEx(keyhand, "jsjuq", 0, REG_SZ, ByVal strString, Len(strString))
r = RegCloseKey(keyhand)

你注册表RUN是系统启动后也要启动的程序
你把你的EXE 注册在这RUN下 就行了

在form_load 好了

回答5:

楼主好像不明白怎样实现开机自动运行一个程序~~
关键在于在注册表的RUN里写入一个值,指向程序的位置就行了~
理解了这个,问题就变成“如何用程序编辑注册表”了。
这就需要用到3个API函数:
RegCreateKey
RegSetValueEx
RegCloseKey
它们的用法在上面给得很清楚了~~