如何将“二进制数据”按原样写入Registry(即:我将可见二进制数据仅作为来自regedit的字符串)

时间:2011-02-23 05:08:11

标签: c# asp.net vb.net vb6 binary

我一直在谷歌上搜索,并且整整一天都没有找到可行的解决方案。我对二进制数据类型一无所知(因为我从来没有故意使用过它们)而且我正在尝试将我在注册表中看到的二进制值写入注册表。现在,我所看到的是以下[如下所示] ...如果我尝试将其作为字符串传递给WinAPI中的RegSetValueEx,当然它会出错......我不知道是什么数字'我需要传入lpData As Any, RegSetValueEx的{​​{1}}参数(我尝试了一个数组),以便它在注册表中显示如下[如下所示]。我真的不知道,我的测试在位数组中放置随机数只会产生相应的随机“数字”,如注册表中所见,我不明白如何将它们“逻辑”联系在一起。

这是罪魁祸首!

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Some\Registry\Location]
"RegistryKey"=hex:01,00,00,00,d0,8c,9d,df,01,15,d1,11,8c,7a,00,c0,4f,c2,97,eb,\
  01,00,00,00,7b,96,8a,78,b9,cc,c1,4f,b1,35,11,01,5e,3c,25,9b,00,00,00,00,02,\
  00,00,00,00,00,10,66,00,00,00,01,00,00,20,00,00,00,22,1c,b6,ea,e3,a5,06,8b,\
  58,69,7b,89,19,b3,1f,a3,1d,d8,b7,5b,30,72,65,4b,22,41,a8,73,d1,92,bb,36,00,\
  00,00,00,0e,80,00,00,00,02,00,00,20,00,00,00,87,a0,1a,79,a7,c9,fe,7a,1b,24,\
  9a,71,5d,cf,7b,87,bc,1b,14,6e,59,96,e3,42,c6,f5,08,78,00,a6,42,3d,30,00,00,\
  00,02,0a,2a,ea,d9,49,0a,fa,48,b3,f1,e2,ae,e2,f8,42,a2,54,1e,56,dc,dd,9b,0d,\
  9b,73,41,72,54,cc,64,49,99,f1,5c,12,70,33,8e,fb,b1,31,66,df,b4,e0,02,bb,40,\
  00,00,00,13,c2,7d,88,16,af,56,ac,82,21,39,95,43,04,50,71,c2,4c,6a,44,a6,03,\
  ea,32,4d,d9,f3,0f,22,2e,41,17,2e,26,11,9b,10,9d,99,60,fc,12,d2,ce,7c,1c,78,\
  6b,f0,59,23,fd,84,46,2c,58,41,ee,31,7a,f8,95,57,54

如果我在字符串或位数组中有以下内容,我必须填充比特数,以便注册表在写入时输出这个[上面]?或者,我怎样才能弄明白该怎么做?我希望有一个函数可以读取我的二进制数据串并将其转换为真正的二进制格式,以提交给VB6中的RegSetValueEx函数或VB.Net或C#中的其他函数(这实际上是一个与语言无关的问题,并且问题存在于所有语言中;即:相同的过程)。

我希望有一个解决方案可以解决这个问题或领导问题。谢谢你们,当我把头发拉出来时,你们是最棒的!

2 个答案:

答案 0 :(得分:2)

没有必要为此使用API​​。 .NET框架中内置了一个注册表类

试试这段代码,让我知道会发生什么:

Dim rk = Registry.CurrentUser.OpenSubKey("KEY_TO_OPEN", True)
rk.SetValue("BinaryValue", New Byte() {10, 43, 44, 45, 14, 255}, _
            RegistryValueKind.Binary)
rk.Close()

您还可以阅读MSDN上的Registry文档以获取更多信息。

答案 1 :(得分:1)

我在VB6中的注册表类中使用此函数:

Public Function SaveBinaryValue(ByVal hKeyRoot As RegRootKey, ByVal hKeySubKey As String, ByVal ValueName As String, ByRef Value() As Byte) As Boolean
   Dim lenValue As Long
   Dim lngRst As Long
   Dim hKeyHandle As Long
   Dim hKeyDisposition As Long
   Dim lngErrNum As Long
   Dim strErrDesc As String
   Dim strErrSource As String

   '' just to avoid any errors in calling functions using a ubound to check the contents
   On Error Resume Next
   '' check that the array has some elements
   lenValue = UBound(Value) + 1
   If Err.Number = 9 Then
       lenValue = 0
   End If

   On Error GoTo errSaveBinaryValue

   lngRst = RegCreateKeyEx(hKeyRoot, hKeySubKey, 0&, REG_BINARY, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0&, hKeyHandle, hKeyDisposition)
   If hKeyHandle <> 0 Then
       If lenValue > 0 Then
           lngRst = RegSetValueEx(hKeyHandle, ValueName, 0&, REG_BINARY, Value(0), lenValue)
       Else
           lngRst = RegSetValueEx(hKeyHandle, ValueName, 0&, REG_BINARY, "", lenValue)
       End If
   End If

   If lngRst = ERROR_SUCCESS Then
       SaveBinaryValue = True
   Else  '' if the return was non-zero there was an error
       Err.Raise lngRst, App.EXEName, "There was an error writing the " & RootKeyName(hKeyRoot) & "\" & hKeySubKey & " registry key, " & LCase$(FormatClassError(lngRst))
   End If

   If hKeyHandle <> 0 Then
       Call RegCloseKey(hKeyHandle)
       hKeyHandle = 0
   End If

   Exit Function

errSaveBinaryValue:
    lngErrNum = Err.Number
    strErrDesc = Err.Description
    strErrSource = Err.Source

    If hKeyHandle <> 0 Then
        lngRst = RegCloseKey(hKeyHandle)
        hKeyHandle = 0
    End If
    Err.Raise lngErrNum, strErrSource & ":SaveBinaryValue", strErrDesc

End Function

而不是从类声明部分中仅提取常量,函数等,而不是整个部分:

Option Explicit

#If False Then
   Dim HKEY_CLASSES_ROOT
   Dim HKEY_CURRENT_CONFIG
   Dim HKEY_CURRENT_USER
   Dim HKEY_DYN_DATA
   Dim HKEY_LOCAL_MACHINE
   Dim HKEY_PERFORMANCE_DATA
   Dim HKEY_USERS
#End If
Public Enum RegRootKey
   HKEY_CLASSES_ROOT = &H80000000
   HKEY_CURRENT_CONFIG = &H80000005
   HKEY_CURRENT_USER = &H80000001
   HKEY_DYN_DATA = &H80000006
   HKEY_LOCAL_MACHINE = &H80000002
   HKEY_PERFORMANCE_DATA = &H80000004
   HKEY_USERS = &H80000003
End Enum

#If False Then
   Dim REG_NOTIFY_CHANGE_NAME
   Dim REG_NOTIFY_CHANGE_ATTRIBUTES
   Dim REG_NOTIFY_CHANGE_LAST_SET
   Dim REG_NOTIFY_CHANGE_SECURITY
#End If
Public Enum RegistryChangeFlag
   REG_NOTIFY_CHANGE_NAME = &H1 ''Create or delete (child)
   REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
   REG_NOTIFY_CHANGE_LAST_SET = &H4      ''time stamp
   REG_NOTIFY_CHANGE_SECURITY = &H8
End Enum

Private Type SECURITY_ATTRIBUTES
   nLength As Long
   lpSecurityDescriptor As Long
   bInheritHandle As Long
End Type
Private m_udtSecAtts

Private Type OSVERSIONINFO
   dwOSVersionInfoSize As Long
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformId As Long
   szCSDVersion As String * 128      ''  Maintenance string for PSS usage
End Type

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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 RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) 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
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 RegNotifyChangeKeyValue Lib "advapi32.dll" (ByVal hKey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long, ByVal hEvent As Long, ByVal fAsynchronus 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
Private Declare Function GetVersionEx Lib "Kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Private Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As Any) As Long
Private Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal numbytes As Long)

''the following declare is used to return windows error descriptions
Private Declare Function FormatMessage Lib "Kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200

''key constants
Private Const ERROR_NO_MORE_ITEMS = 259&
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_SUCCESS = 0&
Private Const SYNCHRONIZE = &H100000
Private Const READ_CONTROL = &H20000
Private Const READ_WRITE = 2
Private Const READAPI = 0
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Private Const KEY_NOTIFY = &H10
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_EVENT = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
Private Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Private Const REG_OPTION_NON_VOLATILE = 0
Private Const REG_SZ = 1                         '' Unicode nul terminated string
Private Const REG_BINARY = 3
Private Const REG_DWORD = 4
Private Const REG_MULTI_SZ = 7                   '' Multiple Unicode strings
Private Const REG_NONE = 0                       '' No value type

'' the following allows for monitoring keys for changes
Public Event KeyChange(ByVal Change As Long)
Private m_hKeyChange As Long     ''private handle to a key that is being monitored