查看: 12  |  回复: 0
  VB6 自动开始上次关机时关闭的应用
楼主
发表于 昨天 22:45

两点关键:

1 检测是 Windows 关闭引起的 QueryUnload 事件。
2 改写 Software\Microsoft\Windows\CurrentVersion\RunOnce    

声明:

Declare Function RegCloseKey Lib "advapi32.dll" Alias "RegCloseKey" (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 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.

新建From1(窗体),新建Command1(按钮CommandButton),代码:

Private Const REG_SZ = 1
Private Const HKEY_CURRENT_USER = &H80000001

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Dim hKey As Long
    Dim strRunCmd As String
    If UnloadMode = vbAppWindows Then
        strRunCmd = App.Path & "\" & App.EXEName & ".EXE"
        Call RegCreateKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\RunOnce", hKey)
        Call RegSetValueEx(hKey, "MyApp", 0&, REG_SZ, ByVal strRunCmd, Len(strRunCmd) + 1)
        Call RegCloseKey(hKey)
    End If
End Sub


您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2024 MaNongKu.com