Solved

Registry probs with RegSaveKey

Posted on 2004-09-07
3
729 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now