Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

winapi - RegSaveKey returns ERROR_PRIVILEGE_NOT_HELD

I'm trying to save the contents of a particular registry key to a file using the RegSaveKey() API:

HKEY key;
LRESULT result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\MyProduct", 0, KEY_ACCESS_ALL, &key);
result = RegSaveKey(key, L"c:\temp\saved.reg", NULL);

However, RegSaveKey() is returning ERROR_PRIVILEGE_NOT_HELD. The SDK documentation says that "The calling process must have the SE_BACKUP_NAME privilege enabled". The process is running as either a local administrator or as a service.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Despite running as a local administrator or as a service, you probably don't have the "Backup" privilege enabled by default. You'll need to enable this privilege before you try to save the registry key.

MSDN has a good example on how to enable a security privilege in C/C++: http://msdn.microsoft.com/en-us/library/aa446619(VS.85).aspx. If you include the sample function defined on that page, you can then just call:

HANDLE ProcessToken;

if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &ProcessToken)) {

    SetPrivilege(ProcessToken, SE_BACKUP_NAME, TRUE);

    // Save reg key now...
    ...
}

Alternatively, there's also a VB-based example on the wayback machine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...