使用VB.NET读取64位注册表值 [英] Read 64-Bit Registry Values Using VB.NET

查看:511
本文介绍了使用VB.NET读取64位注册表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,



我有问题。我想你们中的任何人都可以帮助我。我的问题是我有一个从注册表中读取值并返回字符串值的函数。但是这个功能在32位操作系统和64位操作系统中运行良好,它有一个空引用。如何解决这个问题?

我的功能是:

 公共 共享 功能 GetOSProductKey()作为 < span class =code-keyword> String  
如果 GetOSIs64Bit()= False 然后
尝试
Dim regKey As RegistryKey = _
Registry.LocalMachine.OpenSubKey( < span class =code-string> SOFTWARE \ Mycoftoft \ Windows NT \ CurrentVersion, False
Dim digitalPid()作为 字节 = CType (regKey.GetValue( DigitalProductID ),字节())
如果 digitalPid IsNot Nothing 然后
Dim 键( 14 As 字节 ' 0-14 = 15字节
Array.Copy(digitalPid , 52 ,key, 0 15
Dim 个字符 As String = BCDFGHJKMPQRTVWXY2346789
Dim productKey As < span class =code-keyword> String
=
< span class =code-keyword>对于 j 作为 整数 = 0 24
Dim curValue As = 0
对于 i 作为 整数 = 14 0 S. tep -1
curValue = CShort (curValue * 256 Xor key(i))
key(i)= CByte (Int(curValue / 24 ))
curValue = CShort (curValue Mod 24
下一步
productKey = characters.Substring(curValue, 1 )& productKey
下一步
对于 i 正如 整数 = 4 1 步骤 -1
productKey = productKey.Insert(i * 5 -
下一步

返回 productKey
否则
返回 mUnknown
结束 如果
Catch ex As 异常
返回 mUnknown
结束 尝试
结束 功能

解决方案

在64位窗口中,您可以使用RegistryKey.OpenBaseKey [ ^ ] > RegistryView [ ^ ]作为Registry64。这允许你阅读64位注册表。





只是附注,你可以使用这种方法,无论您使用的是32位系统还是64位系统,都可以通过Registry64获得正确的结果。关键文本是我发布的第二个链接:



引用:MSDN文章

如果在32位操作系统上请求64位视图,则返回的键将位于32位视图中。


这是因为部分注册表有独立的64位和32位部分,32位应用程序使用在WoW64下执行的32位部分。这被称为观点。

请参阅我的答案:获取读取注册表值的问题。它总是返回null [ ^ ]。



您还可以使用节点HKLM \Software \Wow6432Node来自64位应用程序:

获取读取注册表值的问题。它总是返回null [ ^ ]。



-SA


朋友使用此格式读取64位注册表值:

  Dim  rk1 < span class =code-keyword> As  RegistryKey 
Dim rk2 As RegistryKey
rk1 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,RegistryView.Registry64)
rk2 = rk1.OpenSubKey( HARDWARE\DESCRIPTION\System\BIOS
Dim PID 正如 字符串 = rk2.GetValue( SystemProductName)。的ToString


Friends,

I have a problem. I think anyone among you can help me. My Problem is that I have a function which reads a value from registry and returns a string value. But this function works well in 32 -Bit OS and in 64-Bit OS it has a null reference. How to get red of this problem ?
My Function is :

Public Shared Function GetOSProductKey() As String
        If GetOSIs64Bit() = False Then
            Try
                Dim regKey As RegistryKey = _
                    Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion", False)
                Dim digitalPid() As Byte = CType(regKey.GetValue("DigitalProductID"), Byte())
                If digitalPid IsNot Nothing Then
                    Dim key(14) As Byte '0-14 = 15 bytes
                    Array.Copy(digitalPid, 52, key, 0, 15)
                    Dim characters As String = "BCDFGHJKMPQRTVWXY2346789"
                    Dim productKey As String = ""
                    For j As Integer = 0 To 24
                        Dim curValue As Short = 0
                        For i As Integer = 14 To 0 Step -1
                            curValue = CShort(curValue * 256 Xor key(i))
                            key(i) = CByte(Int(curValue / 24))
                            curValue = CShort(curValue Mod 24)
                        Next
                        productKey = characters.Substring(curValue, 1) & productKey
                    Next
                    For i As Integer = 4 To 1 Step -1
                        productKey = productKey.Insert(i * 5, "-")
                    Next

                    Return productKey
                Else
                    Return mUnknown
                End If
            Catch ex As Exception
                Return mUnknown
            End Try
End Function

解决方案

On 64-bit windows you can use the RegistryKey.OpenBaseKey[^] with the RegistryView[^] as Registry64. This allows you to read the 64-bit registry.

[Edit]
Just a side-note, you can use this method and pass Registry64 whether you are on a 32-bit system or a 64-bit system and you will get the proper result. The key text is this in the second link I posted:

Quote: MSDN Article

If you request a 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.


This is because part of the registry has separate 64-bit and 32-bit parts, with 32-bit part used by the 32-bit application executed under WoW64. This is called "views".
Please see my answer: Getting issue to read registry value . It always returning "null"[^].

You can also use the node "HKLM\Software\Wow6432Node" from a 64-bit application:
Getting issue to read registry value . It always returning "null"[^].

—SA


Friends use this Format to Read 64-Bit Registry values :

Dim rk1 As RegistryKey
Dim rk2 As RegistryKey
rk1 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
rk2 = rk1.OpenSubKey("HARDWARE\DESCRIPTION\System\BIOS")
Dim PID As String = rk2.GetValue("SystemProductName").ToString


这篇关于使用VB.NET读取64位注册表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆