程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB綜合教程 >> 通過VB訪問遠程計算機注冊表的方法

通過VB訪問遠程計算機注冊表的方法

編輯:VB綜合教程
 

1. 啟動 Visual Basic 6.0, 並創建新項目的類型標准 EXE。
2. 右擊窗體, 並在 快捷 菜單上單擊 查看代碼 。 開頭的代碼添加以下語

句:
Option Explicit
3. 添加以下代碼來聲明用於訪問注冊表常數:
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003

Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_ALL_ACCESS = &H3F

Private Const REG_SZ As Long = 1
Private Const ERROR_SUCCESS = 0&
4. 添加下列代碼以聲明對 Windows 應用程序編程接口 (API) 中注冊表函

數入口點:
Private Declare Function RegConnectRegistry Lib "advapi32.dll" _
Alias "RegConnectRegistryA" _
(ByVal lpMachineName As String, _
ByVal hKey As Long, _
phkResult As Long) 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 RegQueryValueExString Lib "advapi32.dll" _
Alias "RegQueryValueExA" _
(ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
ByVal lpData As String, _
lpcbData As Long) As Long
5. 添加以下全局變量聲明:
Private hRemoteReg As Long
6. 添加下列代碼以處理 Form _ Load 事件。 此代碼調用

RegConnectRegistry 函數來連接到遠程注冊表。 將 \\RemoteMachineName 參數

替換為計算機要訪問其注冊表名稱:
Private Sub Form_Load()
Dim lRet As Long

'Connect to the remote registry
lRet = RegConnectRegistry("\\RemoteMachineName", _
HKEY_LOCAL_MACHINE, _
hRemoteReg)

If (lRet = ERROR_SUCCESS) Then
MsgBox "Successfully connected to remote registry"
Else
MsgBox "Error:" & Err.LastDllError
Unload Me
Exit Sub
End If
End Sub
7. 添加下列代碼以處理 _ Unload 事件:
Private Sub Form_Unload(Cancel As Integer)
Dim lRet As Long
If hRemoteReg <> 0 Then
lRet = RegCloseKey(hRemoteReg)
End If
End Sub
8. 在窗體設計器, 添加到窗體 命令 控件。 控件具有默認名為 Command


9. 雙擊 Command 以創建為命令按鈕單擊處理程序。 以下代碼添加到單擊

處理函數:
Private Sub Command1_Click()
Dim lRetVal As Long
Dim hKey As Long
Dim sValue As String

lRetVal = RegOpenKeyEx(hRemoteReg, _
"HARDWARE\DESCRIPTION\System", 0, KEY_QUERY_VALUE, hKey)
If lRetVal <> ERROR_SUCCESS Then
MsgBox "Cannot open key"
Else
sValue = String(255, " ")
lRetVal = RegQueryValueExString(hKey, _
"SystemBIOSVersion", 0&, REG_SZ, sValue, 255)
If lRetVal <> ERROR_SUCCESS Then
MsgBox "Cannot query value"
Else
MsgBox sValue
End If
lRetVal = RegCloseKey(hKey)
If lRetVal <> ERROR_SUCCESS Then
MsgBox "Cannot close key"
End If
End If
End Sub
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved