How do I write binary keys to the registry?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

How do I write binary keys to the registry?

Post by huiyi_ch »

I have an application that needs to write binary keys to the registry.
If I should be in
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU] writes the following data:
"a"=hex:6e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2e,00,65,00,78,00,65,00,00,\
00,44,00,3a,00,5c,00,44,00,45,00,56,00,5c,00,53,00,63,00,72,00,69,00,70,00,\
74,00,45,00,64,00,69,00,74,00,5c,00,56,00,42,00,53,00,45,00,64,00,69,00,74,\
00,5c,00,56,00,42,00,53,00,00,00
How do I implement them?


Thanks in advance.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Re: How do I write binary keys to the registry?

Post by Pablo César »

Hi Huiyi,

In your place I would make in VBS.

http://stackoverflow.com/questions/2833 ... gistry-key
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: How do I write binary keys to the registry?

Post by andyglezl »

Hola Huiyi_ch
Ya probaste con estas funciones ?
Modificar el Registro es de cuidado. . .
------------------------------------------------------
Hi Huiyi_ch
Already tried with these functions?
Modifying the Registry is for care. . .


Functions.RegistryRead: Reads Registry Data
Functions.RegistryWrite: Writes Data To System Registry

RegistryRead( cRegPath )
RegistryWrite( cRegPath , xValue )
Andrés González López
Desde Guadalajara, Jalisco. México.
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: How do I write binary keys to the registry?

Post by huiyi_ch »

Pablo César wrote: Wed May 17, 2017 11:50 am Hi Huiyi,

In your place I would make in VBS.

http://stackoverflow.com/questions/2833 ... gistry-key
您好,Pablo César
非常感谢您的帮助.是否可以写一个能写入二进制数据的注册表函数呢?
Hello Pablo César
Thank you very much for your help. Can you write a registry function that can write binary data?
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: How do I write binary keys to the registry?

Post by huiyi_ch »

andyglezl wrote: Wed May 17, 2017 2:16 pm Hola Huiyi_ch
Ya probaste con estas funciones ?
Modificar el Registro es de cuidado. . .
------------------------------------------------------
Hi Huiyi_ch
Already tried with these functions?
Modifying the Registry is for care. . .


Functions.RegistryRead: Reads Registry Data
Functions.RegistryWrite: Writes Data To System Registry

RegistryRead( cRegPath )
RegistryWrite( cRegPath , xValue )
您好andyglezl
这些函数我已试过了,好象不能将二进制数据写入注册表.
Hi andyglezl
I have tried this function, as if I could not write binary data into the registry
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: How do I write binary keys to the registry?

Post by edk »

huiyi_ch wrote: Wed May 17, 2017 4:58 am I have an application that needs to write binary keys to the registry.
If I should be in
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU] writes the following data:
"a"=hex:6e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2e,00,65,00,78,00,65,00,00,\
00,44,00,3a,00,5c,00,44,00,45,00,56,00,5c,00,53,00,63,00,72,00,69,00,70,00,\
74,00,45,00,64,00,69,00,74,00,5c,00,56,00,42,00,53,00,45,00,64,00,69,00,74,\
00,5c,00,56,00,42,00,53,00,00,00
How do I implement them?


Thanks in advance.
Hi huiyi_ch.

There is a bug in win_reg.prg in Harbour sources that prevents the registry from writing values in binary data.
I modified it by adding an array support with binary data or a binary string.

It seems to me that it should solve your problem.

Kindest regards, Edward.

Code: Select all

#include "hmg.ch"
#include "hbwin.ch"

********************************************* 
PROCEDURE Main()
Local nByte

	//binary array
	aReg := {0x6e,0x00,0x6f,0x00,0x74,0x00,0x65,0x00,0x70,0x00,0x61,0x00,0x64,0x00,0x2e,0x00,0x65,0x00,0x78,0x00,0x65,0x00,0x00,;
	x00,0x44,0x00,0x3a,0x00,0x5c,0x00,0x44,0x00,0x45,0x00,0x56,0x00,0x5c,0x00,0x53,0x00,0x63,0x00,0x72,0x00,0x69,0x00,0x70,0x00,;
	x74,0x00,0x45,0x00,0x64,0x00,0x69,0x00,0x74,0x00,0x5c,0x00,0x56,0x00,0x42,0x00,0x53,0x00,0x45,0x00,0x64,0x00,0x69,0x00,0x74,;
	0x00,0x5c,0x00,0x56,0x00,0x42,0x00,0x53,0x00,0x00,0x00}
		 
	//binary string
    	cReg:=""
	FOR EACH nByte IN aReg
		cReg := cReg + Chr(nByte)
	NEXT
	
	//writing registry value as binary array
	MsgInfo( win_regWrite2( "HKCU\_TEST\a", aReg, WIN_REG_BINARY ) , 'Binary Array')
	
	//writing registry value as binary string
	MsgInfo( win_regWrite2( "HKCU\_TEST\c", cReg, WIN_REG_BINARY ) , 'Binary String')
	QUIT
RETURN 

***********************************************************************
FUNCTION win_regWrite2( cRegPath, xValue, nType, nRegSam )
LOCAL nHKEY, cKey, cEntry
win_regPathSplit( cRegPath, @nHKEY, @cKey, @cEntry )
RETURN win_regSet2( nHKEY, cKey, cEntry, xValue, nType, nRegSam )

*************************************************************
FUNCTION win_regSet2( nHKEY, cKeyName, cEntryName, xValue, nValueType, nRegSam ) 
LOCAL cName 
LOCAL lRetVal := .F. 
LOCAL pKeyHandle
LOCAL nByte
hb_default( @nRegSam, 0 )
IF win_regCreateKeyEx( nHKEY, cKeyName, 0, 0, 0, hb_bitOr( KEY_SET_VALUE, nRegSam ), 0, @pKeyHandle ) 
	/* no support for Arrays, Codeblock ... */ 
	SWITCH ValType( xValue )
	CASE "A"		//added support for array of binary
		IF ! HB_ISNUMERIC( nValueType ) .OR. !(nValueType == WIN_REG_BINARY)
			cName := Nil
		ELSE
			cName := ''
			FOR EACH nByte IN xValue
				cName := cName + Chr(nByte)
			NEXT
		ENDIF
		EXIT
	CASE "L" 
		nValueType := WIN_REG_DWORD 
		cName := iif( xValue, 1, 0 ) 
		EXIT 
	CASE "D" 
		nValueType := WIN_REG_SZ 
		cName := DToS( xValue ) 
		EXIT 
	CASE "N" 
		IF ! HB_ISNUMERIC( nValueType ) .OR. ; 
			!( nValueType == WIN_REG_DWORD .OR. ; 
			nValueType == WIN_REG_DWORD_LITTLE_ENDIAN .OR. ; 
			nValueType == WIN_REG_DWORD_BIG_ENDIAN .OR. ; 
			nValueType == WIN_REG_QWORD .OR. ;
			nValueType == WIN_REG_QWORD_LITTLE_ENDIAN ) 
			nValueType := WIN_REG_DWORD 
		ENDIF 
		cName := xValue 
		EXIT 
	CASE "C" 
	CASE "M" 
		//added support for string of binary
		IF ! HB_ISNUMERIC( nValueType ) .OR. ; 
			!( nValueType == WIN_REG_SZ .OR. ; 
			nValueType == WIN_REG_EXPAND_SZ .OR. ;
			nValueType == WIN_REG_BINARY .OR. ;
			nValueType == WIN_REG_MULTI_SZ ) 
			nValueType := WIN_REG_SZ 
		ENDIF 
		cName := xValue 
		EXIT 
	ENDSWITCH 

	IF cName != NIL 
		lRetVal := win_regSetValueEx( pKeyHandle, cEntryName, 0, nValueType, cName ) 
	ENDIF 
	
	win_regCloseKey( pKeyHandle ) 
ENDIF 
RETURN lRetVal 

****** END ******************
Attachments
reg_binary.7z
(1.11 MiB) Downloaded 230 times
trmpluym
Posts: 303
Joined: Tue Jul 15, 2014 6:52 pm
Location: The Netherlands

Re: How do I write binary keys to the registry?

Post by trmpluym »

Edward,

Very good, thank you !

Do you also know a function to convert a string to a binary array ?

Theo
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: How do I write binary keys to the registry?

Post by edk »

Just convert each character from string into ascii value ASC() and put to array.

Code: Select all

Local aBin := {}
Local c1Chr
FOR EACH c1Chr IN cStringBin
	AADD( aBin, ASC(c1Chr))
NEXT	
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: How do I write binary keys to the registry?

Post by huiyi_ch »

Hello Edward,
Very good, thank you very much!

Kindest regards, huiyi_ch.
User avatar
nekbmm
Posts: 118
Joined: Sat Jul 16, 2016 3:16 am
DBs Used: DBF,SQLite
Location: Ivanjica, Serbia

Re: How do I write binary keys to the registry?

Post by nekbmm »

How to read REG_BINARY from 'HKEY_USERS\ .DEFAULT\Printers\DevModePerUser\PDFCreator'
and enter a new value ?

Thanks.
Post Reply