Write binary value in Registry

Just starting out? Need help? Post your questions and find answers here.
boyoss
User
User
Posts: 74
Joined: Fri Feb 05, 2016 10:11 am

Write binary value in Registry

Post by boyoss »

Hello, i'm looking for a procedure to write binary value in registry

My binary is
$6F,$63,$61,$6C,$16,$00,$00,$00,$66,$69,$6C,$65,$1A,$2F,$2F,$44,$3A,$2F
I've found this code here http://www.purebasic.fr/english/viewtop ... 139#p58139, but it seems to be a brutal way...

Code: Select all

DataSection 
regbin_data: 
Data.b 0,1,2,3,4,5,6,7,8,9,128,255 ;byte in decimal ! 
end_regbin_data: 
EndDataSection 

openkey = #HKEY_LOCAL_MACHINE 
subkey.s = "SOFTWARE" 
keyset.s = "test" 
hkey.l = 0 

RegCreateKey_(OpenKey,SubKey,@hKey) 
RegSetValueEx_(hKey,keyset,0,#REG_BINARY,?regbin_data,?end_regbin_data - ?regbin_data) 
RegCloseKey_(hKey) 
If anybody could help, i'll be thankfull
Thanks
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Write binary value in Registry

Post by firace »

Hi, what's brutal about it? Is it the comment about decimals? Hex is just fine:

Code: Select all


DataSection 
  regbin_data: 
  Data.b $6F,$63,$61,$6C,$16,$00,$00,$00,$66,$69,$6C,$65,$1A,$2F,$2F,$44,$3A,$2F 
  end_regbin_data: 
EndDataSection 

openkey = #HKEY_CURRENT_USER
subkey.s = "SOFTWARE" 
keyset.s = "test123" 

RegCreateKey_(OpenKey,SubKey,@hKey) 
RegSetValueEx_(hKey,keyset,0,#REG_BINARY,?regbin_data,?end_regbin_data - ?regbin_data) 
RegCloseKey_(hKey)

boyoss
User
User
Posts: 74
Joined: Fri Feb 05, 2016 10:11 am

Re: Write binary value in Registry

Post by boyoss »

Mucho Thankos :D :D

I understand there is no way to do it without DataSection
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Write binary value in Registry

Post by kenmo »

Of course you can do it without DataSection.

How do you want to set the key? From a file? From memory? From a string?


When you said the code was "brutal" I expected something 20 times longer :)
boyoss
User
User
Posts: 74
Joined: Fri Feb 05, 2016 10:11 am

Re: Write binary value in Registry

Post by boyoss »

from a string
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Write binary value in Registry

Post by kenmo »

OK, I don't know what format the string is in, but here's an example:

Code: Select all

BinaryString.s = "$6F,$63,$61,$6C,$16,$00,$00,$00,$66,$69,$6C,$65,$1A,$2F,$2F,$44,$3A,$2F"

openkey = #HKEY_LOCAL_MACHINE
subkey.s = "SOFTWARE"
hkey.l = 0

Procedure WriteBinaryToRegistry(hKey.l, keyset.s, BinaryString.s)
  n = 1 + CountString(BinaryString, ",")
  *Buffer = AllocateMemory(n)
  If (*Buffer)
    For i = 1 To n
      PokeB(*Buffer + (i-1), Val(StringField(BinaryString, i, ",")))
    Next i
    RegSetValueEx_(hKey, keyset, 0, #REG_BINARY, *Buffer, n)
    FreeMemory(*Buffer)
  EndIf
EndProcedure

RegCreateKey_(OpenKey,SubKey,@hKey)
WriteBinaryToRegistry(hKey, "test", BinaryString)
WriteBinaryToRegistry(hKey, "test2", BinaryString + ",$FF")
RegCloseKey_(hKey)
boyoss
User
User
Posts: 74
Joined: Fri Feb 05, 2016 10:11 am

Re: Write binary value in Registry

Post by boyoss »

Thank you so much!!
boyoss
User
User
Posts: 74
Joined: Fri Feb 05, 2016 10:11 am

Re: Write binary value in Registry

Post by boyoss »

maybe you have also a procedure to READ those value?
Post Reply