Friday, September 22, 2017

ການເພີ່ມໂປຣແກຣມໃຫ້ ເປີດເອງຕອນເປີດຄອມ (VB6 Lesson 20)

ສຳລັບບົດນີ້ ເປັນການສ້າງໂປຣແກຣມ ໃຫ້ເປີດເອງຕອນເວລາເປີດຄອມ
ມີໂຄ້ດດັ່ງນີ້

ແບບທີ່ 1

Dim Startup_key As String
Startup_key = "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"
Dim Reg As Object
Set Reg = CreateObject("wscript.shell")
Reg.RegWrite Startup_key & App.EXEName, App.Path & "" & App.EXEName & ".exe"

ສຳລັບ
- App.EXEName ແມ່ນມີຄ່າເທົ່າຊື່ຂອງໂປຣແກຣມເຮົາເອງ
- App.Path ແມ່ນ ເສັ້ນທາງທີ່ໂປຣແກຣມເຮົາເປີດຢູ່

ແບບທີ່ 2

Option Explicit
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition 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 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 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 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         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Private Const READ_CONTROL = &H20000
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Private Const SYNCHRONIZE = &H100000
Private Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const ERROR_SUCCESS = 0&
Private Const HKEY_CURRENT_USER = &H80000001
Private Const REG_SZ = 1
Private Const EndOfLine = vbCrLf
Public Sub AddToStartUp(ByVal Name As String, ByVal Path As String, Optional ByVal Enabled As Boolean)
Dim hKey As Long
Dim RegValue As String
Dim Result As Long
On Error GoTo RegError
'Open the key, creating it if it doesn't exist.
If RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", ByVal 0&, ByVal 0&, ByVal 0&, KEY_WRITE, ByVal 0&, hKey, ByVal 0&) <> ERROR_SUCCESS Then
MsgBox "Error " & Err.Number & " Opening RegKey" & EndOfLine & Err.Description
Exit Sub
End If
Select Case Enabled
'Create key
Case True
RegValue = Chr$(34) + Path & "" & Name & ".exe" + Chr$(34)
Result = RegSetValueEx(hKey, Name, 0, REG_SZ, ByVal RegValue, Len(RegValue))
If Result <> ERROR_SUCCESS Then
MsgBox "Error " & Err.Number & " Creating RegKey" & EndOfLine & Err.Description
End If
Case False
'Delete Key
RegDeleteValue hKey, Name
End Select
' Close the key.
RegCloseKey hKey
Exit Sub
RegError:
MsgBox Err.Number & " " & Err.Description
Exit Sub
End Sub

ວິທີໃຊ້

Call AddToStartUp("MyApp", "C:\Program Files (x86)\My Application", True)

Subscribe

  • RSS Atom

ອອນລາຍ: 1 | ມື້ນີ້: 13 | ວານນີ້: 25 | ທິດນີ້: 93 | ເດືອນນີ້: 872 | ປີນີ້: 11832 | ລວມ: 78935