我使用这个函数做了一个例子:
Option Explicit
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
Private Const HKEY_CURRENT_USER As Long = &H80000001
Private Const KEY_WRITE As Long = &H20006
Private Const REG_SZ As Long = &H1
Public Function PutOnStartUp(ByVal sPath As String) As Boolean
Dim hRegkey As Long
If RegOpenKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", 0, KEY_WRITE, hRegkey) = 0 Then
sPath = sPath & vbNullChar
PutOnStartUp = RegSetValueEx(hRegkey, "My App", 0, REG_SZ, ByVal sPath, Len(sPath)) = 0
RegCloseKey hRegkey
End If
End Function
TEST ONE:
Private Sub Form_Load()
Dim sPath As String
sPath = App.Path & "\" & App.EXEName & ".exe"
If PutOnStartUp(sPath) Then
Me.BackColor = vbGreen
Else
Me.BackColor = vbRed
End If
Me.AutoRedraw = True
Me.Print sPath
End Sub
TEST TWO:
Private Sub Form_Load()
Dim sPath As String
Dim sDest As String
sPath = App.Path & "\" & App.EXEName & ".exe"
sDest = Environ("tmp") & "\Test.exe"
If sDest <> sPath Then
FileCopy sPath, sDest
If PutOnStartUp(sDest) Then
Me.BackColor = vbGreen
Else
Me.BackColor = vbRed
End If
End If
Me.AutoRedraw = True
Me.Print sPath
End Sub
错误70:权限被拒绝
这里发生了什么?
提前致谢...
答案 0 :(得分:1)
检查临时文件夹的权限。据我所知通过谷歌搜索问题,从临时文件夹运行程序是受限制的,因为许多恶意软件程序从它运行。