我想让我的程序运行一次以后,每次开机都自动运行我的程序。(不是手动改注册表)

解决方案 »

  1.   

    写到注册表里
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
      

  2.   

    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         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Const REG_SZ = 1                         ' Unicode nul terminated string
    Private Const HKEY_CURRENT_USER = &H80000001
    private sub form_load()
     test=app.path & "\" & app.exename & ".exe" '取得执行文件的路径
        call regcreatekey(hkey_current_user,key1) '新建一个子键
        call regsetvalueex(key1,"自动启动",0&,reg_sz,byval test,len(test)+1)
        call regclosekey(key1)
    end sub
      

  3.   

    '----------here in a module---------------
    Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Public 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.
    Public Const REG_SZ = 1    ' Unicode nul terminated string
    Public Const HKEY_LOCAL_MACHINE = &H80000002'-------------here in another module---------------
    Private Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
      Dim Keyhand As Long
      Dim r As Long, i As Integer, StrLen As Long
      For i = 1 To Len(strData)
        If Asc(Mid$(strData, i, 1)) < 0 Then StrLen = StrLen + 2
        If Asc(Mid$(strData, i, 1)) >= 0 Then StrLen = StrLen + 1
      Next i
      r = RegOpenKey(hKey, strPath, Keyhand)
      r = RegSetValueEx(Keyhand, strValue, 0, REG_SZ, ByVal strData, StrLen)
      r = RegCloseKey(Keyhand)
    End Sub
    Public Sub SetAutoRun(Enable As Boolean,RunName as String)
      Dim ExeName As String
      ExeName = App.Path + "\" + App.ExeName + ".exe auto"
      If Enable Then
         Call SaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RunName, ExeName)
      Else
         Call SaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RunName, "")
      End If
    End Sub
    然后调用SetAutoRun([True or False],RunName)
    来控制自动启动开或关,Runname是注册表中你的值,用你的软件名就行了。
      

  4.   

    顺便说一下,我的那个子程序SaveString可以用来设置其他注册表选项,如果只用一次(别处没有操作注册表的地方)可以简化SaveString和SetAutoRun(合并)