Solved

Registry probs with RegSaveKey

Posted on 2004-09-07
3
739 Views
Last Modified: 2013-12-04
I am having a problem saving a registry to a file using RegSaveKey.
I can open the registry fine but when I try to save it with

    ret= RegSaveKey(hkey,path,NULL);

I get a 1314 return code which is "A required privilege is not held by the client"
I pass in NULL as the LPSECURITY_ATTRIBUTES which should default to the client privs and the key I am reading was placed in by the client as well. I can read and access it just fine its only this function that fails. Any ideas?
0
Comment
Question by:joele23
  • 2
3 Comments
 
LVL 8

Expert Comment

by:mxjijo
ID: 12000172
Take a look at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/regsavekey.asp

Says

<quote>
The calling process must have the SE_BACKUP_NAME privilege enabled. For more information, see Running with Special Privileges.
Windows Me/98/95:  This platform uses a different registry hive file format than Windows NT. Therefore, you cannot load a registry hive file created on Windows 95/98/Me on Windows NT or vice-versa.
</quote>

So you need to change your won process previlages with SE_BACKUP_NAME
0
 
LVL 8

Accepted Solution

by:
mxjijo earned 125 total points
ID: 12000275

Call the following functionto set the privilage

if (!GrabPrivilage(SE_BACKUP_NAME))
    printf("\nFailed. Cannot set previlage");
else
{
    printf("\nSE_BACKUP_NAME Privilage has been set");

    // Now call RegSaveKey() here...


}


BOOL
GrabPrivilage(LPCTSTR szNewPriv)
{
    TOKEN_PRIVILEGES Priv, PrivOld;
    DWORD cbPriv = sizeof(PrivOld);
    HANDLE hToken;
      DWORD  dwError;

    // get current thread token
    if (!OpenThreadToken(GetCurrentThread(),
                         TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES,
                         FALSE, &hToken))
    {
        if (GetLastError() != ERROR_NO_TOKEN)
            return FALSE;
        // revert to the process token, if not impersonating
        if (!OpenProcessToken(GetCurrentProcess(),
                              TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES,
                              &hToken))
            return FALSE;
    }

    Priv.PrivilegeCount                        = 1;
    Priv.Privileges[0].Attributes      = SE_PRIVILEGE_ENABLED;
    LookupPrivilegeValue(NULL, szNewPriv, &Priv.Privileges[0].Luid);

    // try to enable the privilege
    if (!AdjustTokenPrivileges(hToken,
                                                FALSE,
                                                &Priv,
                                                sizeof(Priv),
                                                &PrivOld,
                                                &cbPriv))
    {
        dwError = GetLastError();
        CloseHandle(hToken);
        return SetLastError(dwError), FALSE;
    }

    if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
    {
        // the SE_DEBUG_NAME privilege is not in the caller's token
        CloseHandle(hToken);
        return SetLastError(ERROR_ACCESS_DENIED), FALSE;
    }

      return TRUE;
}
0
 
LVL 2

Author Comment

by:joele23
ID: 12000977
very nice
thank you
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question