Page 1 of 1

How do I write binary keys to the registry?

Posted: Wed May 17, 2017 4:58 am
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.

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

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

In your place I would make in VBS.

http://stackoverflow.com/questions/2833 ... gistry-key

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

Posted: Wed May 17, 2017 2:16 pm
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 )

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

Posted: Wed May 17, 2017 11:07 pm
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?

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

Posted: Wed May 17, 2017 11:11 pm
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

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

Posted: Thu May 18, 2017 11:56 am
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 ******************

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

Posted: Thu May 18, 2017 12:37 pm
by trmpluym
Edward,

Very good, thank you !

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

Theo

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

Posted: Thu May 18, 2017 1:40 pm
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	

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

Posted: Fri May 19, 2017 12:28 am
by huiyi_ch
Hello Edward,
Very good, thank you very much!

Kindest regards, huiyi_ch.