Writing binary values to Windows registry

Using (or providing) components based on the "Win32" framework
Post Reply
User avatar
OSRK
Posts: 17
Joined: Tue Dec 27, 2016 8:48 am

Writing binary values to Windows registry

Post by OSRK »

I'm trying to do write a binary value to Windows registry this way:
⎕USING,←⊂''
      hkcu←Microsoft.Win32.Registry.CurrentUser
      key←hkcu.CreateSubKey'Software\Foo\Bar'
      key.SetValue'Data'(1 2 3 4)Microsoft.Win32.RegistryValueKind.Binary
but I get this:
EXCEPTION: The type of the value object did not match the specified RegistryValueKind or the object could not be properly converted.
      key.SetValue'Data'(1 2 3 4)Microsoft.Win32.RegistryValueKind.Binary
      ∧
As far as I understand, the array I am passing is not considered as a byte array. And, as far as I understand, there is an issue with creating byte arrays: https://forums.dyalog.com/viewtopic.php?f=18&t=172
Are there any other hints on how to write binary Windows registry values, maybe?
RichardP|Dyalog
Posts: 33
Joined: Fri Oct 12, 2018 3:05 pm

Re: Writing binary values to Windows registry

Post by RichardP|Dyalog »

Following on from the post that you linked, I was able to write to a binary registry entry as follows:

Code: Select all

      ⎕using←''   ⍝ Using full paths to show where everything comes from
      hkcu←Microsoft.Win32.Registry.CurrentUser
      key←hkcu.CreateSubKey⊂'Software\Foo\'
      ba←System.Array.CreateInstance(System.Byte)4   ⍝ Create length 4 byte[] array
      ⎕io←0                                          ⍝ Match .NET index origin
      ba[2]←1                                        ⍝ Modify byte[] value
      ba[⍳4]
0 0 1 0
      key.SetValue'Data'(ba)Microsoft.Win32.RegistryValueKind.Binary
I then opened the registry editor and could see my new value in Computer\HKEY_CURRENT_USER\Software\Foo\Data
Post Reply