RegSaveKeyEx

This is a discussion on RegSaveKeyEx within the Windows Programming forums, part of the Platform Specific Boards category; Having problem saving key in windows registry. here is the code Code: #include "stdafx.h" #include <stdlib.h> #include <stdio.h> #include <windows.h> ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Mattoon, IL
    Posts
    4

    RegSaveKeyEx

    Having problem saving key in windows registry. here is the code

    Code:
    #include "stdafx.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    #include <tchar.h>
    #include <iostream>
    #include <string.h>
    
    int saveKey(HKEY tree, const char *folder){
    	long lRval;
    	HKEY hKey;
    	
    		// Open location
    	lRval = RegOpenKeyEx( tree, folder, 0, KEY_READ, &hKey );
    	if (lRval != ERROR_SUCCESS)
    	{
    		printf("open failed: %ld",lRval);
    		return 0;
    	}
    
    	lRval = RegSaveKeyEx(hKey,"envBack",NULL,REG_LATEST_FORMAT);
    	if (lRval != ERROR_SUCCESS)
    	{
    		printf("save failed: %ld", lRval);
    		return 0;
    	}
    	else 
    		return 1;
    
    }
    
    // This is the entry point for this application
    int _tmain(void)
    {
    	//BACKUP REGISTRY KEY: HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Control -> Session Manager -> Environment
    	if (saveKey(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"))
    		printf("BACKUP OF REGISTRY KEY: HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Control -> Session Manager -> Environment successful");
    	else
    		printf("BACKUP OF REGISTRY KEY: HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Control -> Session Manager -> Environment failed");
    
     	return 0;
    }
    On the save I am getting error 1314:ERROR_PRIVILEGE_NOT_HELD
    A required privilege is not held by the client.

    But not sure what that means. I am administrator on my machine.

    Any help?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    To call RegSaveKeyEx successfully, you need the SE_BACKUP_NAME privilege. It seems that administrators have this privilege by default so it probably just needs to be enabled in your program. You can check on your machine by looking under Control Panel->Administrative Tools->Local Security Policy->Local Policies->User Rights Assignment. The SE_BACKUP_NAME privilege has the friendly name "Back up files and directories".

    To enable a privilege, you can use this function from Microsoft or a function that I have (shown below). The advantage of my function is that it will not cause load problems on Windows 9x. By using RegSaveKeyEx, your program will only load on Windows XP or better anyway so you may as well use the simpler Microsoft function.
    Code:
    /* 
     * This function enables or disables a privilege for the current process.
     * Common privileges include SE_SHUTDOWN_NAME, SE_BACKUP_NAME, SE_SYSTEMTIME_NAME, etc.
     * This function dynamically loads the required functions so as not to cause load
     * problems on 9x where these functions may not be available.
     */
    BOOL WINAPI EnableProcessPrivilege(LPCTSTR lpPrivilege, BOOL bEnable) 
    {
    	typedef BOOL (WINAPI * fnptr_openprocesstoken)(HANDLE,DWORD,PHANDLE);
    	typedef BOOL (WINAPI * fnptr_adjusttokenprivileges)(HANDLE,BOOL,PTOKEN_PRIVILEGES,DWORD,PTOKEN_PRIVILEGES,PDWORD);
    	typedef BOOL (WINAPI * fnptr_lookupprivilegevalue)(LPCTSTR,LPCTSTR,PLUID);
    
    	TOKEN_PRIVILEGES tp       = { 0 };
    	HMODULE          hAdvapi  = NULL;
    	HANDLE           hToken   = NULL;
    	BOOL             bResult  = FALSE;
    	fnptr_openprocesstoken      openprocesstoken;
    	fnptr_adjusttokenprivileges adjusttokenprivileges;
    	fnptr_lookupprivilegevalue  lookupprivilegevalue;
    
    	/* Load the required functions */
    	if (!(hAdvapi = LoadLibrary(TEXT("Advapi32.dll"))) ||
    	    !(openprocesstoken      = (fnptr_openprocesstoken)      GetProcAddress(hAdvapi, "OpenProcessToken"))      ||
    	    !(adjusttokenprivileges = (fnptr_adjusttokenprivileges) GetProcAddress(hAdvapi, "AdjustTokenPrivileges")) ||
    #ifndef UNICODE
    	    !(lookupprivilegevalue  = (fnptr_lookupprivilegevalue)  GetProcAddress(hAdvapi, "LookupPrivilegeValueA")))
    #else
    	    !(lookupprivilegevalue  = (fnptr_lookupprivilegevalue)  GetProcAddress(hAdvapi, "LookupPrivilegeValueW")))
    #endif
    	{
    		goto cleanup;
    	}
    
    	/* Get the local id of our desired privilege */
    	if (!lookupprivilegevalue(NULL, lpPrivilege, &tp.Privileges[0].Luid))
    		goto cleanup;
    
    	tp.PrivilegeCount           = 1;
    	tp.Privileges[0].Attributes = (bEnable ? SE_PRIVILEGE_ENABLED : 0);
    
    	/* Get the access token for the current process */
    	if (!openprocesstoken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
    		goto cleanup;
    
    	/* Enable the privilege */
    	if (!adjusttokenprivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
    		goto cleanup;
    
    	bResult = TRUE;
    
    cleanup:
    	if (hAdvapi) FreeLibrary(hAdvapi);
    	if (hToken)  CloseHandle(hToken);
    	return bResult;
    }
    Last edited by anonytmouse; 04-08-2005 at 02:39 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mattoon, IL
    Posts
    4
    WOOHOO! Works great! Thanks!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21