Page 1 of 1

RegSetValue()

Posted: Mon Jan 08, 2007 5:00 pm
by Bill Simmeth
It seems the function only stores STRING values? How can I store DWORD and BINARY values to the PPC registry?

Thanks.

Posted: Mon Jan 08, 2007 5:45 pm
by Antonio Linares
Bill,

We are going to modify RegSetValue(), thanks

Posted: Mon Jan 08, 2007 5:57 pm
by Bill Simmeth
Very good, thanks!

Posted: Tue Jan 09, 2007 7:58 am
by Antonio Linares
Bill,

This one should work for strings and numbers (not decimals):

Code: Select all

HB_FUNC( REGSETVALUE )
{
      LPWSTR pW1 = AnsiToWide( hb_parc( 2 ) );

      switch( hb_itemType( hb_param( 3, HB_IT_ANY ) ) )
      {
         case HB_IT_STRING: 	
              LPWSTR pW2 = AnsiToWide( hb_parc( 3 ) );  
              hb_retnl( RegSetValueEx( ( HKEY ) hb_parnl( 1 ), pW1, 0, REG_SZ, ( CONST BYTE * ) pW2, 
                                     ( hb_parclen( 3 ) * 2 ) + 1 ) );
              hb_xfree( pW2 );
              break;
              
         case HB_IT_INTEGER:  
         case HB_IT_LONG:	   
              hb_retnl( RegSetValueEx( ( HKEY ) hb_parnl( 1 ), pW1, 0, REG_DWORD, ( const BYTE * ) hb_parnl( 3 ), sizeof( DWORD ) ) ); 
              break;         	
      }	

      hb_xfree( pW1 );
}
For binary values we may need to supply a fourth parameter to the function to specify the type, as there is no a Clipper type that may represent binary.

Posted: Tue Jan 09, 2007 3:34 pm
by Bill Simmeth
Hi Antonio,

Thanks for the efforts. However, I have tried to set a DWORD value but receive a return value of 87, which is "INVALID PARAMETER" and the value is not added to the registry.

Here is a sample of my call to it:

Code: Select all

//nHnd returned from RegOpenKey()
cVal := "AutoEnter"
nVal := 1

nResult := RegSetValue2( nHnd, cVal, nVal )
Also, I had to change your code slightly as the compiler complained about pW2 being declared within the CASE. Here is revised:

Code: Select all

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>
#include <item.api>

LPWSTR AnsiToWide( LPSTR );

HB_FUNC( REGSETVALUE2 )
{
    LPWSTR pW1 = AnsiToWide( hb_parc( 2 ) );
    LPWSTR pW2;

    switch( hb_itemType( hb_param( 3, HB_IT_ANY ) ) )
    {
       case HB_IT_STRING:
            pW2 = AnsiToWide( hb_parc( 3 ) );
            hb_retnl( RegSetValueEx( ( HKEY ) hb_parnl( 1 ), pW1, 0, REG_SZ, ( CONST BYTE * ) pW2, ( hb_parclen( 3 ) * 2 ) + 1 ) );
            break;

       case HB_IT_INTEGER:
       case HB_IT_LONG:
            hb_retnl( RegSetValueEx( ( HKEY ) hb_parnl( 1 ), pW1, 0, REG_DWORD, ( const BYTE * ) hb_parnl( 3 ), sizeof( DWORD ) ) );
            break;
    }

    hb_xfree( pW1 );
    hb_xfree( pW2 );
}

#pragma ENDDUMP

Posted: Tue Jan 09, 2007 3:43 pm
by Bill Simmeth
Antonio,

I would be in favor of adding an optional fourth parameter to allow for BINARY values. I have an important need for this.

Thanks,
Bill

Posted: Tue Jan 09, 2007 4:01 pm
by Bill Simmeth
Sorry, I forgot to add that using the function to set a REG_SZ (text) value works fine.

Posted: Tue Jan 09, 2007 10:42 pm
by Antonio Linares
Bill,

We are going to review it asap

Posted: Tue Jan 09, 2007 10:45 pm
by Antonio Linares
Bill,

Please try this:

Code: Select all

         DWORD dwValue = hb_parnl( 3 );
         ...
         case HB_IT_INTEGER:  
         case HB_IT_LONG:       
              hb_retnl( RegSetValueEx( ( HKEY ) hb_parnl( 1 ), pW1, 0, REG_DWORD, ( const BYTE * ) &dwValue, sizeof( DWORD ) ) ); 
              break; 

Posted: Wed Jan 10, 2007 1:10 pm
by Bill Simmeth
Antonio, thanks. That change works. So, now REG_SZ and DWORD values are working.