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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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         ' Note that if you declare the lpData parameter as String, you must pass it By Value.Const READ_CONTROL = &H20000
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 KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
                       KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
                       KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
                     
' 注册键 ROOT 类型...
Const HKEY_LOCAL_MACHINE = &H80000002
Const ERROR_SUCCESS = 0
Const REG_SZ = 1                         ' Unicode 以 Null 结尾的字符串
Const REG_DWORD = 4                      ' 32-位数字Const REG_AUTORUN_KEY = "SOFTWARE\MICROSOFT\WINDOWS\CurrentVersion\Run"
Private Sub SetAutoRun(ByVal Autorun As Boolean)
    Dim KeyId As Long, t As String
    
    'RegCreateKey HKEY_LOCAL_MACHINE, ByVal REG_AUTORUN_KEY, KeyId
    RegOpenKey HKEY_LOCAL_MACHINE, ByVal REG_AUTORUN_KEY, KeyId
    If Autorun Then
      t = AppPath + App.EXEName + ".exe"
      RegSetValueEx KeyId, "EyeProtecter", 0&, REG_SZ, ByVal t, lstrlen(t)
    Else
      RegDeleteValue KeyId, "EyeProtecter"
    End If
    RegCloseKey KeyId
End Sub

解决方案 »

  1.   

    Private Sub SetAutoRun(ByVal Autorun As Boolean)
        Dim KeyId As Long, t As String
        
        'RegCreateKey HKEY_LOCAL_MACHINE, ByVal REG_AUTORUN_KEY, KeyId
        RegOpenKey HKEY_LOCAL_MACHINE, ByVal REG_AUTORUN_KEY, KeyId
        If Autorun Then
          t = app.path + App.EXEName + ".exe"
          RegSetValueEx KeyId, "我的程序", 0&, REG_SZ, ByVal t, lstrlen(t)
        Else
          RegDeleteValue KeyId, "我的程序"
        End If
        RegCloseKey KeyId
    End Sub
      

  2.   

    http://www.sqreg.com/file/vb/reg_02.htm
      

  3.   

    上面的同志说得都很好,在此,我还提供一种方法:
    修改win.ini
    将windows下面的"load="后面加上自己的程序的路径和名字就可以了。
      

  4.   

    '以下是为了操作注册表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
    Public Const ERROR_SUCCESS = 0&' Registry API prototypesDeclare 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
    Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult 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 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
    Public Const REG_SZ = 1                         ' Unicode nul terminated string
    Public Const REG_DWORD = 4                      ' 32-bit number
    Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As LongPublic Sub savekey(HKEY As Long, strPath As String)
    Dim keyhand&
    r = RegCreateKey(HKEY, strPath, keyhand&)
    r = RegCloseKey(keyhand&)
    End SubPublic Function getstring(HKEY As Long, ByVal strPath As String, ByVal strValue As String)Dim keyhand As Long
    Dim datatype As Long
    Dim lResult As Long
    Dim strBuf As String
    Dim lDataBufSize As Long
    Dim intZeroPos As Integer
    r = RegOpenKey(HKEY, strPath, keyhand)
    lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
    If lValueType = REG_SZ Then
        strBuf = String(lDataBufSize, " ")
        lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
        If lResult = ERROR_SUCCESS Then
            intZeroPos = InStr(strBuf, Chr$(0))
            If intZeroPos > 0 Then
                getstring = Left$(strBuf, intZeroPos - 1)
            Else
                getstring = strBuf
            End If
        End If
    End If
    End Function
    Public Sub savestring(HKEY As Long, ByVal strPath As String, ByVal strValue As String, ByVal strdata As String)
    Dim keyhand As Long
    Dim r As Long
    r = RegCreateKey(HKEY, strPath, keyhand)
    r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
    r = RegCloseKey(keyhand)
    End Sub
    Function getdword(ByVal HKEY As Long, ByVal strPath As String, ByVal strValueName As String) As Long
    Dim lResult As Long
    Dim lValueType As Long
    Dim lBuf As Long
    Dim lDataBufSize As Long
    Dim r As Long
    Dim keyhand As Longr = RegOpenKey(HKEY, strPath, keyhand) ' Get length/data type
    lDataBufSize = 4
        
    lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)If lResult = ERROR_SUCCESS Then
        If lValueType = REG_DWORD Then
            getdword = lBuf
        End If
    'Else
    '    Call errlog("GetDWORD-" & strPath, False)
    End Ifr = RegCloseKey(keyhand)
        
    End FunctionFunction SaveDword(ByVal HKEY As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
        Dim lResult As Long
        Dim keyhand As Long
        Dim r As Long
        r = RegCreateKey(HKEY, strPath, keyhand)
        lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
        'If lResult <> error_success Then Call errlog("SetDWORD", False)
        r = RegCloseKey(keyhand)
    End FunctionPublic Function DeleteKey(ByVal HKEY As Long, ByVal strKey As String)
    Dim r As Long
    r = RegDeleteKey(HKEY, strKey)
    End FunctionPublic Function DeleteValue(ByVal HKEY As Long, ByVal strPath As String, ByVal strValue As String)
    Dim keyhand As Long
    r = RegOpenKey(HKEY, strPath, keyhand)
    r = RegDeleteValue(keyhand, strValue)
    r = RegCloseKey(keyhand)
    End Function例子:
       '添加DNS主机名
       HKEY = HKEY_LOCAL_MACHINE
        strPath = "Software\Microsoft\Windows\CurrentVersion\Run"
        strValue = "YourName"
         strdata = "你的程序的路径"
      Call savestring(HKEY, strPath, strValue, strdata)
      

  5.   

    http://www.sqreg.com/file/vb/reg_02.htm
      

  6.   

    在Windows98下自动启动程序的10种方法。  1. Autostart 文件  C:\Windows\start menu\programs\startup {chinese/english}  在注册表中的位置:   HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
      Folders Startup="C:\Windows\start menu\programs\startup"   所以它将很容易被程序更改  2. Win.ini  [Windows]
      load=file.exe
      run=file.exe  3. System.ini [boot]  Shell=Explorer.exe file.exe4. c:\Windows\Winstart.bat  看似平常,但每次都重新启动  5. Registry键  [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices]
      [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce]
      [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run]
      [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce]
      [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]
      [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce]
      [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunServices]  6. c:\Windows\Wininit.ini  一旦运行就被Windows删除,安装的setup程序常用
      Example: (content of Wininit.ini)
      [Rename]
      NUL=c:\Windows\picture.exe
      例子:将c:\Windows\picture.exe设置为NUL, 表示删除它,完全隐蔽的执行!  7. Autoexec.bat  在DOS下每次自启动8. Registry Shell Spawning (使用过Subseven吗?看看吧)  [HKEY_CLASSES_ROOT\exefile\shell\open\command] @="\"%1\" %*"
      [HKEY_CLASSES_ROOT\comfile\shell\open\command] @="\"%1\" %*"
      [HKEY_CLASSES_ROOT\batfile\shell\open\command] @="\"%1\" %*"
      [HKEY_CLASSES_ROOT\htafile\Shell\Open\Command] @="\"%1\" %*"
      [HKEY_CLASSES_ROOT\piffile\shell\open\command] @="\"%1\" %*"
      [HKEY_LOCAL_MACHINE\Software\CLASSES\batfile\shell\open\command] @="\"%1\" 
      %*"
      [HKEY_LOCAL_MACHINE\Software\CLASSES\comfile\shell\open\command] @="\"%1\" 
      %*"
      [HKEY_LOCAL_MACHINE\Software\CLASSES\exefile\shell\open\command] @="\"%1\" 
      %*"
      [HKEY_LOCAL_MACHINE\Software\CLASSES\htafile\Shell\Open\Command] @="\"%1\" 
      %*"
      [HKEY_LOCAL_MACHINE\Software\CLASSES\piffile\shell\open\command] @="\"%1\" 
      %*"  这些"%1 %*"需要被赋值, 如果将其改为 "server.exe %1 %*",server.exe将在每次启动时被执行,这些exe/pif/com/bat/hta等文件都可被执行9. Icq Inet  [HKEY_CURRENT_USER\Software\Mirabilis\ICQ\Agent\Apps\test]
      "Path"="test.exe"
      "Startup"="c:\\test"
      "Parameters"=""
      "Enable"="Yes"  [HKEY_CURRENT_USER\Software\Mirabilis\ICQ\Agent\Apps\  当icq发现网络连接时,将被执行(我使用的icq2000b的键值有所不同,但您可以自行查找) 9. 杂项说明找找以下的键值:  [HKEY_LOCAL_MACHINE\Software\CLASSES\ShellScrap] 
      @="Scrap object" "NeverShowExt"=""  NeverShowExt 键 可以隐藏SHS文件的扩展名.shs  如果你将一个文件改名为:"abc.jpg.shs" 它只显示"abc.jpg"  如果你的注册表里有很多NeverShowExt键值,删除他们。  注意:  这些方法不能全部适应Win2K,但您可以自行检测. 如有什么地方不对,还希望各位高手指教。
    www.
      

  7.   

    请问是否要引用什么东西?
    因为我在运行到RegOpenKey HKEY_LOCAL_MACHINE, ByVal REG_AUTORUN_KEY, KeyId
    这句时,显示子程序或函数未定义。