[英]How to read protected registry key using vb.net
我正在使用以下代码,但是我如何调用注册表似乎并不重要,我总是在受保护的注册表项上遇到失败或无法使用默认值。
有趣的是,在运行 VBScript 时,访问这些键没有问题。
Const Key As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
Public Overloads ReadOnly Property ProductName As String
Get
Return DirectCast(My.Computer.Registry.LocalMachine.GetValue(Key & "ProductName"), String)
' Dim WshShell As Object = CreateObject("WScript.Shell")
' Return WshShell.RegRead(Key & "ProductName")
End Get
End Property
Public ReadOnly Property DigitalID As String
Get
Return DirectCast(My.Computer.Registry.LocalMachine.GetValue(Key & "DigitalProductId"), String)
' Dim WshShell As Object = CreateObject("WScript.Shell")
' Return WshShell.RegRead(Key & "DigitalProductId")
End Get
End Property
Public ReadOnly Property PID As String
Get
Return DirectCast(My.Computer.Registry.LocalMachine.GetValue(Key & "ProductID"), String)
' Dim WshShell As Object = CreateObject("WScript.Shell")
' Return WshShell.RegRead(Key & "ProductID")
End Get
End Property
Public ReadOnly Property ProductKey As String
Get
Try
Dim pKey As Byte() = System.Text.Encoding.Default.GetBytes(DigitalID)
Dim Chars As String = "BCDFGHJKMPQRTVWXY2346789"
Dim i As Integer = 24
Dim isWin8 As Integer = (pKey(66) \ 6) And 1
Dim Cur As Integer = 0
Dim x As Integer = 14
Dim Last As Integer = 0
Dim keypart1 As String = ""
Dim insert As String = ""
Dim a As String = ""
Dim b As String = ""
Dim c As String = ""
Dim d As String = ""
Dim e As String = ""
Dim KeyOutput As String = ""
Const KeyOffset = 52
pKey(66) = (pKey(66) And &HF7) Or ((isWin8 And 2) * 4)
i = 24
Chars = "BCDFGHJKMPQRTVWXY2346789"
Do
Cur = 0
x = 14
Do
Cur = Cur * 256
Cur = pKey(x + KeyOffset) + Cur
pKey(x + KeyOffset) = (Cur \ 24)
Cur = Cur Mod 24
x = x - 1
Loop While x >= 0
i = i - 1
KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
Last = Cur
Loop While i >= 0
If (isWin8 = 1) Then
keypart1 = Mid(KeyOutput, 2, Last)
insert = "N"
KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
If Last = 0 Then KeyOutput = insert & KeyOutput
End If
a = Mid(KeyOutput, 1, 5)
b = Mid(KeyOutput, 6, 5)
c = Mid(KeyOutput, 11, 5)
d = Mid(KeyOutput, 16, 5)
e = Mid(KeyOutput, 21, 5)
Return a & "-" & b & "-" & c & "-" & d & "-" & e
Catch er As Exception
Return er.Message
End Try
End Get
End Property
使用 WshShell 进行测试时,我将“密钥”设置为HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\
并尝试了HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\
。 我还尝试在开头和结尾使用或不使用斜杠,甚至对整个密钥进行硬编码。
我遇到的问题是所有这些注册表项都没有返回任何内容,而且我找不到如何设置能够打开它们所需的权限,因为我可以使用 RegEdit 看到它们,我也可以使用 VBScript 访问它们.
提前致谢。
您需要先打开子项才能读取值:
Const Key As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
Public Overloads ReadOnly Property ProductName As String
Get
Return My.Computer.Registry.LocalMachine.OpenSubKey(Key).GetValue("ProductName").ToString
End Get
End Property
编辑:如上所述,Hans Passant 需要将构建平台设置为 Windows 安装的“本机”平台(x64 或 64 位 Windows 的任何 CPU),因为 DigitalProductId(以及其他)值仅存在于注册表的“本地”分支。 最好的办法是将它留给 AnyCPU 以确保能够读取任何客户端 PC 上的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.