Copy Registry Entry Without Deleting Old Entry ~ function call parameters not correct 
Author Message
 Copy Registry Entry Without Deleting Old Entry ~ function call parameters not correct

Hi folks.  Sorry for the crosspost, but I am not sure from where the
answer will come on this question.

I have an old code snippet (shown below) which was used to RENAME a
registry key entry and it's tree to a new root under a new name.  What
I need to do is COPY a registry entry and it's tree to a new root name
without changing or affecting anything in the original.  My problem
comes in what I should pass as parameters to the function calls.  I
feel somewhat silly asking for this, but I am tired of banging my head
against the wall.  (Please don't ask me to post the several dozen
things I have tried that have failed.  I already know they don't
work...)

As an example, if I have [HKEY_CURRENT_USER\software\microsoft\Windows
NT\CurrentVersion\Windows Messaging Subsystem\Profiles] with a
defaultprofile key whose value is "my_default_profile", then I know I
have a tree of values under that whose root is "my_default_profile" ~
I would like to copy this to something called "my_new_profile" with
all of the other values underneath it unchanged using the code below.
Can it be done this way?  Is there a better way?  What should my calls
look like for this example?

(This needs to be done programmatically, not using the "copy" button
on the mail icon in the control panel.  The above is only an example
for reference, not the actual values that I need to change.  Please
reference this example only when replying.  Thank you.)

All help and guidance is appreciated.

SWP

//==============================================================================
//     code snippet follows
//==============================================================================

#include <windows.h>
#include <windowsx.h>

BOOL RenameRegistryItem( char *pszFrom, char *pszTo ) {
  HKEY hKey, hFromKey, hToKey;
  BOOL bStatus;
  LONG lStatus;

  hKey = HKEY_CURRENT_USER;                 //  This assumes
HKEY_CURRENT_USER
                                            //  so nothing *drastic*
happens
  lStatus = RegOpenKey( hKey, pszFrom, &hFromKey );        //  Open
"from" key
  if( lStatus != ERROR_SUCCESS )
      return( FALSE );                      //  Fail if "from" key
doesn't open
  lStatus = RegCreateKey( hKey, pszTo, &hToKey );          //  Create
"to" key
  if( lStatus != ERROR_SUCCESS ) {          //  Fail if "to" key
doesn't open
    RegCloseKey( hFromKey );                //  Close "from" key
before leaving
    return( FALSE );
  }
  bStatus = CopyRegistryKey( hFromKey, hToKey );           //  Copy
"from"->"to"
  RegCloseKey( hToKey );                    //  Close both keys and
exit
  RegCloseKey( hFromKey );
  //RegDeleteKey( hKey, pszFrom );            //  Delete last
top-level key
  return( TRUE );

Quote:
}                                           //  End of

RenameRegistryItem()

// ============================================================================

BOOL CopyRegistryKey( HKEY hFrom, HKEY hTo ) {
  LONG lStatus;
  int iKey;
  DWORD dwValueSize, dwBufferSize, dwType;
  char *pszValueName;
  BYTE *pbBuffer;
  HKEY hNewTo, hNewFrom;

  pszValueName = GlobalAllocPtr( GHND, 8192 );      //  Need some
buffer space
  if( ! pszValueName )
    return( FALSE );
  pbBuffer = (BYTE *)pszValueName + 512;
  iKey = 0;                            //  Start with value #0
  do {
    dwValueSize = 512;                 //  Tell system how much buffer
we have
    dwBufferSize = 8192 - 512;         //  and enumerate data values
at current key
    lStatus = RegEnumValue( hFrom, iKey, pszValueName, &dwValueSize,
NULL, &dwType, pbBuffer, &dwBufferSize );
    if( lStatus == ERROR_SUCCESS ) {   //  Move each value to new
place
      lStatus = RegSetValueEx( hTo, pszValueName, 0, dwType, pbBuffer,
dwBufferSize );
      RegDeleteValue( hFrom, pszValueName );        //  Delete old
value
    }
  }
  while( lStatus == ERROR_SUCCESS );   //  Loop until all values found
  iKey = 0;                            //  Start over, looking for
keys now instead of values
  do {
    dwValueSize = 512;                 //  Tell system about the
buffer size
    dwBufferSize = 8192 - 512;         //  Get "next" key
    lStatus = RegEnumKeyEx( hFrom, iKey, pszValueName, &dwValueSize,
NULL, pbBuffer, &dwBufferSize, NULL );
    if( lStatus == ERROR_SUCCESS ) {   //  Was a valid key found?
      //  Open the key if found
      lStatus = RegCreateKey( hTo, pszValueName, &hNewTo );
      if( lStatus == ERROR_SUCCESS ) { //  If the key opened...
        //  Create new key of old name
        lStatus = RegCreateKey( hFrom, pszValueName, &hNewFrom );
        if( lStatus == ERROR_SUCCESS ) {        //  If that worked,
recurse back here
          CopyRegistryKey( hNewFrom, hNewTo );  //  to "walk the tree"
          RegCloseKey( hNewFrom );              //  Close each new key
          //RegDeleteKey( hFrom, pszValueName );  //  Delete old key
        }
        RegCloseKey( hNewTo );                  //  Close each old key
      }
    }
  }
  while( lStatus == ERROR_SUCCESS );            //  Loop until key
enum fails
  GlobalFreePtr( pszValueName );                //  Free buffer and
exit
  return( TRUE );

Quote:
}

//==============================================================================
//     end of code snippet
//==============================================================================


Tue, 09 Dec 2003 03:50:41 GMT  
 Copy Registry Entry Without Deleting Old Entry ~ function call parameters not correct
You can use

            RegSaveKey() to save the original key and all its nodes to a
temporary file.
then use RegLoadKey() to load it back with a new key name.

Cheers
Check Abdoul
---------------------


Quote:
> Hi folks.  Sorry for the crosspost, but I am not sure from where the
> answer will come on this question.

> I have an old code snippet (shown below) which was used to RENAME a
> registry key entry and it's tree to a new root under a new name.  What
> I need to do is COPY a registry entry and it's tree to a new root name
> without changing or affecting anything in the original.  My problem
> comes in what I should pass as parameters to the function calls.  I
> feel somewhat silly asking for this, but I am tired of banging my head
> against the wall.  (Please don't ask me to post the several dozen
> things I have tried that have failed.  I already know they don't
> work...)

> As an example, if I have [HKEY_CURRENT_USER\software\microsoft\Windows
> NT\CurrentVersion\Windows Messaging Subsystem\Profiles] with a
> defaultprofile key whose value is "my_default_profile", then I know I
> have a tree of values under that whose root is "my_default_profile" ~
> I would like to copy this to something called "my_new_profile" with
> all of the other values underneath it unchanged using the code below.
> Can it be done this way?  Is there a better way?  What should my calls
> look like for this example?

> (This needs to be done programmatically, not using the "copy" button
> on the mail icon in the control panel.  The above is only an example
> for reference, not the actual values that I need to change.  Please
> reference this example only when replying.  Thank you.)

> All help and guidance is appreciated.

> SWP

file://=====================================================================
=========
Quote:
> //     code snippet follows

file://=====================================================================
=========

- Show quoted text -

Quote:

> #include <windows.h>
> #include <windowsx.h>

> BOOL RenameRegistryItem( char *pszFrom, char *pszTo ) {
>   HKEY hKey, hFromKey, hToKey;
>   BOOL bStatus;
>   LONG lStatus;

>   hKey = HKEY_CURRENT_USER;                 //  This assumes
> HKEY_CURRENT_USER
>                                             //  so nothing *drastic*
> happens
>   lStatus = RegOpenKey( hKey, pszFrom, &hFromKey );        //  Open
> "from" key
>   if( lStatus != ERROR_SUCCESS )
>       return( FALSE );                      //  Fail if "from" key
> doesn't open
>   lStatus = RegCreateKey( hKey, pszTo, &hToKey );          //  Create
> "to" key
>   if( lStatus != ERROR_SUCCESS ) {          //  Fail if "to" key
> doesn't open
>     RegCloseKey( hFromKey );                //  Close "from" key
> before leaving
>     return( FALSE );
>   }
>   bStatus = CopyRegistryKey( hFromKey, hToKey );           //  Copy
> "from"->"to"
>   RegCloseKey( hToKey );                    //  Close both keys and
> exit
>   RegCloseKey( hFromKey );
>   file://RegDeleteKey( hKey, pszFrom );            //  Delete last
> top-level key
>   return( TRUE );
> }                                           //  End of
> RenameRegistryItem()

> //

============================================================================

- Show quoted text -

Quote:

> BOOL CopyRegistryKey( HKEY hFrom, HKEY hTo ) {
>   LONG lStatus;
>   int iKey;
>   DWORD dwValueSize, dwBufferSize, dwType;
>   char *pszValueName;
>   BYTE *pbBuffer;
>   HKEY hNewTo, hNewFrom;

>   pszValueName = GlobalAllocPtr( GHND, 8192 );      //  Need some
> buffer space
>   if( ! pszValueName )
>     return( FALSE );
>   pbBuffer = (BYTE *)pszValueName + 512;
>   iKey = 0;                            //  Start with value #0
>   do {
>     dwValueSize = 512;                 //  Tell system how much buffer
> we have
>     dwBufferSize = 8192 - 512;         //  and enumerate data values
> at current key
>     lStatus = RegEnumValue( hFrom, iKey, pszValueName, &dwValueSize,
> NULL, &dwType, pbBuffer, &dwBufferSize );
>     if( lStatus == ERROR_SUCCESS ) {   //  Move each value to new
> place
>       lStatus = RegSetValueEx( hTo, pszValueName, 0, dwType, pbBuffer,
> dwBufferSize );
>       RegDeleteValue( hFrom, pszValueName );        //  Delete old
> value
>     }
>   }
>   while( lStatus == ERROR_SUCCESS );   //  Loop until all values found
>   iKey = 0;                            //  Start over, looking for
> keys now instead of values
>   do {
>     dwValueSize = 512;                 //  Tell system about the
> buffer size
>     dwBufferSize = 8192 - 512;         //  Get "next" key
>     lStatus = RegEnumKeyEx( hFrom, iKey, pszValueName, &dwValueSize,
> NULL, pbBuffer, &dwBufferSize, NULL );
>     if( lStatus == ERROR_SUCCESS ) {   //  Was a valid key found?
>       //  Open the key if found
>       lStatus = RegCreateKey( hTo, pszValueName, &hNewTo );
>       if( lStatus == ERROR_SUCCESS ) { //  If the key opened...
>         //  Create new key of old name
>         lStatus = RegCreateKey( hFrom, pszValueName, &hNewFrom );
>         if( lStatus == ERROR_SUCCESS ) {        //  If that worked,
> recurse back here
>           CopyRegistryKey( hNewFrom, hNewTo );  //  to "walk the tree"
>           RegCloseKey( hNewFrom );              //  Close each new key
>           file://RegDeleteKey( hFrom, pszValueName );  //  Delete old key
>         }
>         RegCloseKey( hNewTo );                  //  Close each old key
>       }
>     }
>   }
>   while( lStatus == ERROR_SUCCESS );            //  Loop until key
> enum fails
>   GlobalFreePtr( pszValueName );                //  Free buffer and
> exit
>   return( TRUE );
> }

file://=====================================================================
=========
Quote:
> //     end of code snippet

file://=====================================================================
=========


Tue, 09 Dec 2003 03:57:46 GMT  
 Copy Registry Entry Without Deleting Old Entry ~ function call parameters not correct
I am not familiar with these functions.  What includes are required?

Also, I cannot load the VC++ 6 samples for some reason ~ I get a crash
right when it starts to load.  Can you post the sample from there for
me?

Copying a registry entry shouldn't have to be this much trouble ...
it's not like this is rocket science where we have to convert metric
and english ...

have a nice day
swp

Quote:

> You can use

>             RegSaveKey() to save the original key and all its nodes to a
> temporary file.
> then use RegLoadKey() to load it back with a new key name.

> Cheers
> Check Abdoul
> ---------------------



Wed, 10 Dec 2003 02:12:47 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Copy Registry Entry Without Deleting Old Entry ~ function call parameters not correct

2. REQ: How do I delete Win95 registry entries?

3. REQ: How do I delete Win95 registry entries?

4. Yacc entry for crontab , accepting negative entry how ?

5. Required entry field in data entry form

6. Calling DLL functions without known function name, parameter-list until run-time

7. how to read and write registry entry

8. Using Attribute to add custom registry entries

9. ActiveX Control entries in registry

10. ATL Objects & Registry entries with multiple interfaces

11. registry entry

12. about registry entry NoSaveSettings

 

 
Powered by phpBB® Forum Software