Page 1 of 2

Writing into the Windows registry

Posted: Tue Jun 30, 2015 2:26 pm
by driessen
Hello,

Can someone provide me a small example how to update a key in the Windows registry?

Thanks.

Re: Writing into the Windows registry

Posted: Tue Jun 30, 2015 2:40 pm
by Richard Chidiak
Michel

this is an example, HTH

Code: Select all

  oReg := TReg32():Create( HKEY_CURRENT_USER, "Control Panel\Desktop\WindowMetrics" )  // scrollbars
   I := 0

   oReg:Get( "ScrollHeight", @I )
   IF I # 0
       oReg:Set( "ScrollHeight", DVAL )
       oReg:Set( "ScrollWidth", DVAL )
       oReg:Close()
   ENDIF
 

Re: Writing into the Windows registry

Posted: Tue Jun 30, 2015 2:53 pm
by driessen
Richard,

Thanks a lot for your help. But I'm afraid I don't quite understand.
I have never written into the registry before.

This is what I need to write into the registry :
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]
"STCUNR"="53"
"STCURA"="Name1"
"STJUDA"="Name2"
Maybe you could translate it for my in FHW?

Thanks a lot in advance.

Re: Writing into the Windows registry

Posted: Tue Jun 30, 2015 3:19 pm
by AntoninoP

Code: Select all

oReg := TReg32():Create( HKEY_LOCAL_MACHINE, "ControlSet001\Control\Session Manager\Environment" ) 
oReg:Set( "STCUNR", "53")
oReg:Set( "STCURA", "name1" )
oReg:Set( "STJUDA", "name2" )
oReg:Close() 
and

Code: Select all

#define  HKEY_LOCAL_MACHINE      2147483650        // 0x80000002

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 7:52 am
by driessen
Hello,

Thank you all for your help. Until now, everything is running just fine.

I use this function to change environment variables. Because I think it is better to change user environment varaibles, I changed my code to HKEY_CURRENT USER like this :

Code: Select all

#DEFINE HKEY_CURRENT_USER 2147483649 // 0x80000002

......

IF RegRet
   oReg := TReg32():Create( HKEY_CURRENT_USER,"Environment")
   IF UPPER(cPar) = "J"
      IF !EMPTY(cNAAM) ; oReg:Set("STJUDA",cNAAM) ; ENDIF
   ELSE
      IF !EMPTY(cNAAM) ; oReg:Set("STCURA",cNAAM) ; ENDIF
      IF !EMPTY(cNR  ) ; oReg:Set("STCUNR",cNR  ) ; ENDIF
   ENDIF
   oReg:Close()
ENDIF
By using this code, the variables are changed in the register.

Still one problem left : how do I activate them without having to reboot or to logoff?

Thanks.

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 8:02 am
by Antonio Linares
Michel,

You may need to use function SetEnvironmentVariable() from Windows API:

https://msdn.microsoft.com/en-us/librar ... s.85).aspx

Here you can review a thread about it:

http://forums.fivetechsupport.com/viewt ... 610#p73610

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 8:25 am
by driessen
Antonio,

I tried that API-function. The variables are only changed while my application is running. But not permanently.

On the MSDN-page you send me, I also read : "Sets the contents of the specified environment variable for the current process.".

But thanks anyway.

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 8:27 am
by driessen
Could someone send me a small example how I can read my variables from the registry?

Thanks.

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 8:37 am
by Antonio Linares
Michel,

oReg := TReg32():New( HKEY_CURRENT_USER,"Environment")

MsgInfo( oReg:Get("STJUDA") )

oReg:Close()

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 8:38 am
by Antonio Linares
Also you may use to set and retrieve environment variables:

hb_SetEnv( "michel", "this is a test" )

MsgInfo( GetEnv( "michel" ) )

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 8:40 am
by driessen
Sorry, Antonio.

Sure that I know that function but I completely forgot.

Thanks.

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 10:13 am
by Otto
Michel,
please see my post:
http://forums.fivetechsupport.com/viewt ... ll#p143009
lg
Otto

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 11:32 am
by driessen
Antonio,

I tried HB_SETENV but I got an error "unresolved function".

Which library do I need to add?

Thanks.

Otto,

I'll have a look at your topic. Thanks.

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 11:36 am
by MarcoBoschi
I use this function to Use pdfCreator in AutoSave Mode
It works
Bye

Code: Select all

FUNCTION SETTAREG( cDir , cFile , cAutoSave )
   LOCAL oReg

   oReg:=TReg32():New( HKEY_CURRENT_USER , "SOFTWARE\PDFCreator\Program" )
   oReg:Set( "UseAutosave"          , cAutoSave      )
   oReg:Set( "UseAutosaveDirectory" , cAutoSave      )
   oReg:Set( "AutosaveDirectory"    , UPPER( cDir )  )
   oReg:Set( "AutosaveFilename"     , UPPER( cFile ) )

   oReg:Close()

RETURN .T.
 

Re: Writing into the Windows registry

Posted: Wed Jul 01, 2015 8:08 pm
by Antonio Linares
Michel,

I am afraid that hb_SetEnv() is only available for Harbour