Click here to Skip to main content
13,052,669 members (61,697 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 30 Jan 2001

Copy an entire registry key with one simple function

, 31 Mar 2001
Rate this:
Please Sign up or sign in to vote.
A simple API like function that will do this job for you.


This small function will copy an entire registry key including all it's values and sub-keys to a new key.

For example, if you wish to copy: HKEY_CURRENT_USER\SoftWare\MySoftware to HKEY_CURRENT_USER\SoftWare\$MYTEMP$, you'll need to do the following:

HKEY SrcKey;
HKEY TrgKey;

                                 0, KEY_READ, &SrcKey);
                                 0, KEY_READ, &TrgKey);

if (RegCopyKey(SrcKey, TrgKey, "$MYTEMP$") == 
; // All went okay
; // Something went wrong

The code

//  free buffer
__forceinline void FreeBuff(unsigned char** Buff)
    if (*Buff) {
        *Buff = NULL;

//  allocate buffer
__forceinline void AllocBuff(unsigned char** Buff, 
                                    DWORD BuffSize)
    *Buff = (unsigned char*)malloc(BuffSize);

//  copy key to new position
LONG RegCopyKey(HKEY SrcKey, HKEY TrgKey, 
                           char* TrgSubKeyName)

    HKEY    SrcSubKey;
    HKEY    TrgSubKey;
    int    ValEnumIndx=0;
    int    KeyEnumIndx=0;
    char    ValName[MAX_PATH+1];
    char    KeyName[MAX_PATH+1];
    DWORD    size;    
    DWORD    VarType;
    DWORD    BuffSize;
    unsigned char*    Buff=NULL;
    LONG    Err;
    DWORD    KeyDisposition;
    FILETIME LastWriteTime; 

    // create target key
    if (RegCreateKeyEx(TrgKey,TrgSubKeyName,
             &KeyDisposition) != ERROR_SUCCESS)
        return GetLastError();

    do {
        do {
            // read value from source key
            Err = ERROR_NOT_ENOUGH_MEMORY;
            BuffSize = 1024;
            do {                         
                Err = RegEnumValue(SrcKey,ValEnumIndx,
                if ((Err != ERROR_SUCCESS) && 
                         (Err != ERROR_NO_MORE_ITEMS))
                    Err = GetLastError();
            } while (Err == ERROR_NOT_ENOUGH_MEMORY);

            // done copying this key
            if (Err == ERROR_NO_MORE_ITEMS)

            // unknown error return
            if (Err != ERROR_SUCCESS)
                goto quit_err;

            // write value to target key
            if (RegSetValueEx(TrgSubKey,ValName,
                       BuffSize) != ERROR_SUCCESS)
                goto quit_get_err;

            // read next value
        } while (true);

        // free buffer

        // if copying under the same 
        // key avoid endless recursions
        do {
            // enum sub keys
            Err = RegEnumKeyEx(SrcKey,KeyEnumIndx++,
        } while ((SrcKey == TrgKey) && 
           !strnicmp(KeyName,TrgSubKeyName,strlen(KeyName)) && 
           (Err == ERROR_SUCCESS));

        // done copying this key        
        if (Err == ERROR_NO_MORE_ITEMS)

        // unknown error return
        if (Err != ERROR_SUCCESS)
            goto quit_get_err;

        // open the source subkey
        if (RegOpenKeyEx(SrcKey,KeyName,NULL,
                   &SrcSubKey) != ERROR_SUCCESS)
            goto quit_get_err;

        // recurs with the subkey
        if ((Err = CopyKey(SrcSubKey, TrgSubKey, 
                        KeyName)) != ERROR_SUCCESS)

        if (RegCloseKey(SrcSubKey) != ERROR_SUCCESS)
            goto quit_get_err;
    } while (true);

// normal quit
    if (Err == ERROR_NO_MORE_ITEMS)
        return ERROR_SUCCESS;    
        return Err;

// abnormal quit
    return GetLastError();


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ziv Ayalon
Israel Israel
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionUse SHCopyKey() instead Pin
Tornacious12-Mar-12 12:30
memberTornacious12-Mar-12 12:30 
GeneralMy vote of 3 Pin
Tornacious12-Mar-12 12:21
memberTornacious12-Mar-12 12:21 
GeneralMy vote of 2 Pin
oren.shnitzer30-Oct-11 4:52
memberoren.shnitzer30-Oct-11 4:52 
GeneralMy vote of 2 Pin
kpavelko24-Nov-08 6:26
memberkpavelko24-Nov-08 6:26 
GeneralThank you!! Pin
pointer19-Aug-08 3:38
memberpointer19-Aug-08 3:38 
Generalin addition Pin
Valery A. Boronin5-May-06 9:43
memberValery A. Boronin5-May-06 9:43 
Generalunicode & security Pin
Valery A. Boronin5-May-06 7:53
memberValery A. Boronin5-May-06 7:53 
Generali get errors C3861 Pin
E-Male19-May-04 0:08
memberE-Male19-May-04 0:08 
GeneralFixed > 1024 issue Pin
Harlan Seymour12-Feb-04 9:01
sussHarlan Seymour12-Feb-04 9:01 
GeneralRe: Fixed > 1024 issue Pin
wschmidt21-Feb-04 3:00
memberwschmidt21-Feb-04 3:00 
GeneralThe best i´ve ever seen Pin
Anonymous26-Nov-03 5:30
sussAnonymous26-Nov-03 5:30 
GeneralThank you! Pin
Eugene Plokhov26-Oct-03 18:07
memberEugene Plokhov26-Oct-03 18:07 
GeneralDeserves a big 5! Pin
Geert Delmeiren4-Apr-02 1:13
memberGeert Delmeiren4-Apr-02 1:13 
GeneralSome bugs Pin
Michael Walz20-Mar-02 3:18
memberMichael Walz20-Mar-02 3:18 
GeneralExcellent Pin
Mandalis16-Mar-02 6:24
memberMandalis16-Mar-02 6:24 
I had been looking for such a function for days when I finally came upon this article. How could I have missed it all this time? Anyway, it's exactly what I was looking for, I could put it to use in my registry class in 10 minutes.
I just modified it a little so that it would not overwrite existing values.
For this I replaced
// write value to target key
if (RegSetValueEx(TrgSubKey,ValName,NULL,VarType,Buff,BuffSize) != ERROR_SUCCESS)
goto quit_get_err;

if (RegGetValueEx(TrgSubKey,ValName,NULL,&VarType,NULL,NULL) == ERROR_SUCCESS)
// don't do anything, there is already such a value
else // write value to target key
if (RegSetValueEx(TrgSubKey,ValName,NULL,VarType,Buff,BuffSize) != ERROR_SUCCESS)
goto quit_get_err;

GeneralRegKeyOpenEx Pin
Anonymous7-Mar-02 11:33
memberAnonymous7-Mar-02 11:33 
GeneralImporting Reg files Pin
Kunjan14-Jan-02 23:48
memberKunjan14-Jan-02 23:48 
GeneralRe: Importing Reg files Pin
Anonymous7-May-02 4:36
memberAnonymous7-May-02 4:36 
QuestionKEY_READ? Pin
Istrebitjel6-Nov-01 22:25
memberIstrebitjel6-Nov-01 22:25 
AnswerRe: KEY_READ? Pin
Ziv Ayalon8-Nov-01 5:05
memberZiv Ayalon8-Nov-01 5:05 
GeneralRe: KEY_READ? Pin
XIIX11-Aug-02 9:52
memberXIIX11-Aug-02 9:52 
GeneralGood job Pin
Anonymous1-Apr-01 20:30
memberAnonymous1-Apr-01 20:30 
GeneralNice, but... Pin
Anonymous26-Feb-01 5:53
memberAnonymous26-Feb-01 5:53 
GeneralRe: Nice, but... Pin
John Miller Smith Ann28-Feb-02 8:18
memberJohn Miller Smith Ann28-Feb-02 8:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Apr 2001
Article Copyright 2001 by Ziv Ayalon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid