How do I write binary keys to the registry?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
huiyi_ch
Posts: 56
Joined: Sat May 21, 2016 5:27 am
Has thanked: 1 time
Been thanked: 7 times

How do I write binary keys to the registry?

Post by huiyi_ch » 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.

User avatar
Pablo César
Posts: 4052
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 175 times

Post by Pablo César » Wed May 17, 2017 11:50 am

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: 826
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Has thanked: 7 times
Been thanked: 18 times
Contact:

Post by andyglezl » 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 )
Andrés González López
Desde Guadalajara, Jalisco. México.

huiyi_ch
Posts: 56
Joined: Sat May 21, 2016 5:27 am
Has thanked: 1 time
Been thanked: 7 times

Post by huiyi_ch » Wed May 17, 2017 11:07 pm

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: 56
Joined: Sat May 21, 2016 5:27 am
Has thanked: 1 time
Been thanked: 7 times

Post by huiyi_ch » Wed May 17, 2017 11:11 pm

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: 104
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 24 times
Been thanked: 44 times

Post by edk » Thu May 18, 2017 11:56 am

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 18 times

trmpluym
Posts: 190
Joined: Tue Jul 15, 2014 6:52 pm
Location: The Netherlands
Has thanked: 4 times
Been thanked: 11 times

Post by trmpluym » Thu May 18, 2017 12:37 pm

Edward,

Very good, thank you !

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

Theo

edk
Posts: 104
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 24 times
Been thanked: 44 times

Post by edk » Thu May 18, 2017 1:40 pm

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: 56
Joined: Sat May 21, 2016 5:27 am
Has thanked: 1 time
Been thanked: 7 times

Post by huiyi_ch » Fri May 19, 2017 12:28 am

Hello Edward,
Very good, thank you very much!

Kindest regards, huiyi_ch.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests