none
Save and Replace the registry not working on WEC 2013 RRS feed

  • Question


  • I am trying to save and replace hive based registry for root keys HKEY_LOCAL_MACHINE, but it ends up corrupting registries. Here are my steps:

    1. RegSaveKey to \\Storage Card\\SaveReg.srg

    -          Binary file of approx 260 KB gets created

    1.        Modify Registry manually using Regedit or some sample test Application.

    -          Modified registry value saves successfully

    -          Even after reboot this modified registry persist

    1.  RegReplaceKey to restore earlier saved registries in \\Storage Card\\SaveReg.srg

    -          No Error occurred

    -          Reboot and check but old registry values are not applied

    -          Modify any Registry manually works but  modified value does not persist after reboot

    -          Try to RegReplaceKey again. It returns Error Code (14)  bur GetLastError does not show any error. Error Log for this failure:

                                            Replace  Key failed ret 14 get 0

    Following are my code snippets:

    if( ERROR_SUCCESS != RegSaveKey( HKEY_LOCAL_MACHINE, (L"\\mmcmemory\\SaveReg1.srg"), NULL ))
       RETAILMSG( 1,((L"RegSaveKey failed (%d)\n"), GetLastError));


    ret=RegReplaceKey(HKEY_LOCAL_MACHINE, NULL, TEXT("\\MMCMemory\\SaveReg1.srg"), NULL);
    if(ret != ERROR_SUCCESS)
    	RETAILMSG(1,(_T(" Replace  Key failed ret %d get %d \r\n"),ret, GetLastError()));
    

    Please suggest.

    Thanking you !

    <o:p></o:p>

    Regards,<o:p></o:p>

    Rahul Bhatia<o:p></o:p>


    Wednesday, April 5, 2017 6:32 AM

Answers

  • Hi Rahul,

    I just did a test and it all works on my computer ;-)

    The test I did, on a hive-based (debug) kernel where the uSD card's name is MicroSD4:

    • Clean boot
    • Save the registry to a file on uSD card
    • Store a registry key + value into the registry
    • Flush the registry so it gets persisted. Read back the value to see if it correctly in the registry
    • reboot
    • Read back the value to see if it correctly in the registry
    • Replace the registry with the stored one on uSD.
    • Flush the registry.
    • Readback the value and it is not there (successfully restored the registry)
    • Reboot
    • Readback the value and it is not there (successfully restored the registry)

    Here is my test code (sorry for any mistakes if any, i just typed it quickly.. but it did the trick).

    #include "stdafx.h"
    #include <pwinreg.h>
    
    TCHAR szFileName[] = { L"\\MicroSD4\\registry.srg" };
    
    int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
    {
        BOOL bSave = FALSE;
    
        for (;;)
        {   // Use for-loop for easy break-out error handling
    
            if (argc != 2)
            {
                RETAILMSG(1, (L"Invalid args specified\r\n"));
                break;
            }
    
            if (_tcscmp(argv[1], L"s") == 0)
                bSave = TRUE;
            else if (_tcscmp(argv[1], L"r") == 0)
                bSave = FALSE;
            else
                break;
    
            if (bSave)
            {
                if (RegSaveKey(HKEY_LOCAL_MACHINE, szFileName, NULL) != ERROR_SUCCESS)
                    RETAILMSG(1, (TEXT("RegSaveKey failed (%d)\n"), GetLastError()));
                else
                    RETAILMSG(1, (L"Registry successfully saved to %s", szFileName));
            }
            else
            {
                if (RegReplaceKey(HKEY_LOCAL_MACHINE, NULL, szFileName, NULL) != ERROR_SUCCESS)
                    RETAILMSG(1, (TEXT("RegReplaceKey failed (%d)\n"), GetLastError()));
                else
                {
                    RETAILMSG(1, (L"Registry successfully restored %s, reboot in order to reflect the changes\r\n", szFileName));
                    RegFlushKey(HKEY_LOCAL_MACHINE);
                }
            }
            // Always break out of loop
            break;
        }
    
        return 0;
    }

    If you are interested I can send you both the programs. Application to save/restore registry and to set/read registry key.. ping me at myfirstname@mycompany.com


    Good Luck,

    Erwin Zwart, eMVP
    Check out my blog: http://guruce.com/blog
    GuruCE
    Microsoft Embedded Partner
    http://guruce.com
    Consultancy, training and development services.

    Saturday, April 15, 2017 7:23 PM

All replies

  • I never tried this functionality so here my thoughts

    In your summary you talk about \Storage Card\ and in the code snippets you use \\mmcmemory\ ? typo?

    MSDN documentation is confusing, I would expect that you should take a look at the return code first. returning 14 means out of memory?

    Perhaps you need to flush the registry after you call RegReplaceKey the first time?

    Also the comment:

    To restore a saved user hive, move the file into the user's profile directory while the user is not logged on. Subsequent calls to the SetCurrentUser function use the restored hive.

    So perhaps this means that you first need to 'logout' to be able to restore the users hive.


    Good Luck,

    Erwin Zwart, eMVP
    Check out my blog: http://guruce.com/blog
    GuruCE
    Microsoft Embedded Partner
    http://guruce.com
    Consultancy, training and development services.

    Wednesday, April 5, 2017 7:41 AM
  • Dear Erwin,

    Thanks a lot for reply. 

    Apologies for confusion due to difference in the text and code snippet. Actually i am saving registry file in mmcmemory only. 

    Yes, Its out of memory return code but i am not able to guess why. RegReplaceKey works first time without any error.

    I have tried with Flush Key but no benefit. Same behavior.

    Regarding USER, I guess that description is for user specific keys but i am trying to save LOCAL MACHINE KEY. 

    I am new to windows and started working recently on WEC 2013. Please suggest.

    Regards,

    Rahul Bhatia

    Wednesday, April 5, 2017 10:45 AM
  • Dear Erwin,

    It is not working in my multiple trials. Can you please suggest some alternative / workaround for this issue.

    My main requirement is to preserve registry key and values while updating NK.nb0 image. 

    Thanking you

    Regards,

    Rahul Bhatia

    Tuesday, April 11, 2017 6:31 AM
  • I would have to dig into this to give you some sensible answers. I will try to reproduce this behavior but I am afraid that this won't be anytime soon..

    Luckily Bruce had a some great articles regarding save and restoring the registry check it: here


    Good Luck,

    Erwin Zwart, eMVP
    Check out my blog: http://guruce.com/blog
    GuruCE
    Microsoft Embedded Partner
    http://guruce.com
    Consultancy, training and development services.

    Tuesday, April 11, 2017 2:19 PM
  • Thanks a lot Erwin !

    I followed same articles for my implementation. I came to know that it works fine with CE 6.0 too. But seems to be some issue in WEC 2013.

    I will wait for your trials.

    Regards,

    Rahul Bhatia

    Wednesday, April 12, 2017 6:22 AM
  • Rahul

    Do you flush the registry after restoring it?  That is necessary.

    If you are using my registry editor to modify the registry, then after a change it always flushes the registry.  Also, there are flushing options that an OEM can use which could explain the difference between Compact 7 and 2013.



    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman
    I work for Eurotech

    Thursday, April 13, 2017 2:16 PM
    Moderator
  • Hi Rahul,

    I just did a test and it all works on my computer ;-)

    The test I did, on a hive-based (debug) kernel where the uSD card's name is MicroSD4:

    • Clean boot
    • Save the registry to a file on uSD card
    • Store a registry key + value into the registry
    • Flush the registry so it gets persisted. Read back the value to see if it correctly in the registry
    • reboot
    • Read back the value to see if it correctly in the registry
    • Replace the registry with the stored one on uSD.
    • Flush the registry.
    • Readback the value and it is not there (successfully restored the registry)
    • Reboot
    • Readback the value and it is not there (successfully restored the registry)

    Here is my test code (sorry for any mistakes if any, i just typed it quickly.. but it did the trick).

    #include "stdafx.h"
    #include <pwinreg.h>
    
    TCHAR szFileName[] = { L"\\MicroSD4\\registry.srg" };
    
    int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
    {
        BOOL bSave = FALSE;
    
        for (;;)
        {   // Use for-loop for easy break-out error handling
    
            if (argc != 2)
            {
                RETAILMSG(1, (L"Invalid args specified\r\n"));
                break;
            }
    
            if (_tcscmp(argv[1], L"s") == 0)
                bSave = TRUE;
            else if (_tcscmp(argv[1], L"r") == 0)
                bSave = FALSE;
            else
                break;
    
            if (bSave)
            {
                if (RegSaveKey(HKEY_LOCAL_MACHINE, szFileName, NULL) != ERROR_SUCCESS)
                    RETAILMSG(1, (TEXT("RegSaveKey failed (%d)\n"), GetLastError()));
                else
                    RETAILMSG(1, (L"Registry successfully saved to %s", szFileName));
            }
            else
            {
                if (RegReplaceKey(HKEY_LOCAL_MACHINE, NULL, szFileName, NULL) != ERROR_SUCCESS)
                    RETAILMSG(1, (TEXT("RegReplaceKey failed (%d)\n"), GetLastError()));
                else
                {
                    RETAILMSG(1, (L"Registry successfully restored %s, reboot in order to reflect the changes\r\n", szFileName));
                    RegFlushKey(HKEY_LOCAL_MACHINE);
                }
            }
            // Always break out of loop
            break;
        }
    
        return 0;
    }

    If you are interested I can send you both the programs. Application to save/restore registry and to set/read registry key.. ping me at myfirstname@mycompany.com


    Good Luck,

    Erwin Zwart, eMVP
    Check out my blog: http://guruce.com/blog
    GuruCE
    Microsoft Embedded Partner
    http://guruce.com
    Consultancy, training and development services.

    Saturday, April 15, 2017 7:23 PM
  • Dear Erwin,

    Thanks for taking out time and doing sample program.

    It worked for me and i am able to save and replace registries. I have a next level problem though, where i want to save and replace registries during NK.Nb0  Image update. I will create a separate thread for that discussion.

    Thanks a lot.

    Regards,

    Rahul Bhatia

    Tuesday, April 18, 2017 2:29 PM
  • Thanks a lot Bruce for the help.

    Regards,

    Rahul Bhatia

    Tuesday, April 18, 2017 2:29 PM