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 Const HKEY_CURRENT_USER = &H80000001
Private Const REG_SZ = 1
Public Function AddToStarup(DesName As String, exePath As String) As Boolean
Dim SubKey As String
Dim hKey As Long
On Error GoTo acd
AddToStarup = False
SubKey = "Software\Microsoft\Windows\CurrentVersion\Run"
RegCreateKey HKEY_CURRENT_USER, SubKey, hKey
RegSetValueEx hKey, DesName, 0, REG_SZ, ByVal exePath, LenB(StrConv(exePath, vbFromUnicode)) + 1
RegCloseKey hKey
AddToStarup = True
Exit Function
acd:
AddToStarup = False
End Function
Private Sub Form_Load()
Call AddToStarup("abc", App.Path & "\Client.exe") '后面的参数就是你要启动的程序
Unload Me
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set Yan = CreateObject("WScript.Shell")
Yan.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName, "REG_SZ"
MsgBox Yan.REGREAD("HKLM\Software\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName)
End Sub
Private Declare Function BeginPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function EndPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function PathToRegion Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 Const REG_SZ = 1 ' Unicode nul terminated string
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 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
Private Declare Function GetCurrentDirectory Lib "kernel32" Alias "GetCurrentDirectoryA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Dim ret As Long, hKey As Long, lendata As Long, str As String
Private Sub Form_Load()
Dim hRgn As Long
Dim longth As Integer
Dim sname As String
Dim sPath As String
Private Function user_name() As String
Dim str As String
Dim res As Long
str = String(1024, 0)
res = GetUserName(str, 1024)
If res <> 0 Then
user_name = Mid(str, 1, InStr(1, str, Chr(0)) - 1)
Else
user_name = ""
End If
End Function
Private Sub reg_edit()
Dim strLen As Integer
Dim sPath As String
ret = RegOpenKey(&H80000002, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", hKey)
If ret = 0 Then
'讀數據--------------------
ret = RegQueryValueEx(hKey, "", ByVal 0, REG_SZ, ByVal vbNullString, lendata)
'先得到長度
If ret = 0 Then
'有預設值存在
str = String(lendata, Chr(0))
RegQueryValueEx hKey, "", ByVal 0, REG_SZ, ByVal str, lendata
str = Left(str, InStr(str, Chr(0)) - 1)
If str = connstring Then
'MsgBox "已注冊﹗"
Exit Sub
End If
End If
strLen = Len(connstring) + 1
RegSetValueEx hKey, "", 0, REG_SZ, ByVal connstring, strLen
'MsgBox "注冊成功!"
Else
RegCreateKey &H80000002, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", hKey
strLen = Len(connstring) + 1
RegSetValueEx hKey, "", 0, REG_SZ, ByVal connstring, strLen
'MsgBox "注冊成功!"
End If
End Sub
Private Function GetCurrentDir() As String
Dim BufferLength As Long
Dim lBuffer As String
Dim ret As Long
lBuffer = Space(255)
BufferLength = 255
On Error Resume Next
ret = GetCurrentDirectory(BufferLength, lBuffer)
If ret = 0 Then
MsgBox "Error"
Else
GetCurrentDir = stripZero(lBuffer)
End If
End Function
Private Function stripZero(ByVal str As String) As String
Dim intZeroPos As Integer
intZeroPos = InStr(1, str, Chr(0))
If intZeroPos > 0 Then
stripZero = Left(str, intZeroPos - 1)
Else
stripZero = str
End If
End Function
Option Explicit
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()
Text1.Text = App.Path & "\test.exe"
End Sub
Private Sub Command1_Click()
Dim hKey As Long
Dim myexe As String
Dim myint As Integer
myint = Len(Text1.Text) - InStrRev(Text1.Text, "\")
myexe = Right(Text1.Text, myint)
If Text1.Text <> "" Then
RegCreateKey HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", hKey
RegSetValueEx hKey, myexe, 0, REG_SZ, ByVal Text1.Text, 13
RegCloseKey hKey
End If
End Sub
Private Sub Command2_Click()
CmD1.DialogTitle = "选择文件"
CmD1.Filter = "exe|*.exe"
CmD1.ShowOpen
If Len(CmD1.FileName) Then
Text1.Text = CmD1.FileName
End If
End Sub
Private Sub Command3_Click()
End
End Sub