Article View: borland.public.cppbuilder.nativeapi
Article #1887Re: problems of saving a registry key to a file
From: han
Date: Tue, 29 May 2007 21:57
Date: Tue, 29 May 2007 21:57
74 lines
3323 bytes
3323 bytes
It is the backup privilige. I add the following code before open the key.
static HANDLE hToken;
static TOKEN_PRIVILEGES tp;
static LUID luid;
// Enable backup privilege.
if (OpenProcessToken( GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken )== 0)
{
dwError = GetLastError();
GetErrorMsg(dwError);
}
if (LookupPrivilegeValue( NULL, "SeBackupPrivilege", &luid ) == 0)
//if (LookupPrivilegeValue( NULL, SE_BACKUP_NAME , &luid ) == 0)
{
dwError = GetLastError();
GetErrorMsg(dwError);
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (AdjustTokenPrivileges( hToken, FALSE, &tp,
sizeof(TOKEN_PRIVILEGES), NULL, NULL ) == 0)
{ dwError = GetLastError();
GetErrorMsg(dwError);
}
It works. I tried adding this code before. But I was adding this after openkey. In order to get this work, the privilege has to be granted before open the key.
Thanks a lot for the help. I also thought RegOpenKeyEx and RegSaveKey fill in GetLastError.
Han
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"han" <hanzhangusa@yahoo.com> wrote in message
>news:465c9924$1@newsgroups.borland.com...
>
>> I got save key error :0, which I believe it means the operation
>> completed successfully.
>
>My mistake. RegOpenKeyEx() and RegSaveKey() do not fill in
>GetLastError() at all. The return value is the actual error code
>instead:
>
> HKEY hKey;
> LONG lResult;
> DWORD dwErr;
>
> lResult = RegOpenKeyEx(HKEY_CURRENT_USER,
>"Software\\XXXCompany\\Settings\\ViewForms", 0, KEY_ALL_ACCESS,
>&hKey);
> if( lResult == ERROR_SUCCESS )
> {
> lResult = RegSaveKey(hKey, "tempReg", NULL);
> if( lResult != ERROR_SUCCESS )
> Application->MessageBox(("save key error: " +
>AnsiString(lResult)).c_str(), NULL, MB_OK);
> RegCloseKey(hKey);
> }
> else
> Application->MessageBox(("open key error: " +
>AnsiString(lResult)).c_str(), NULL, MB_OK);
>
>
>Gambit
>
>
Technical Details
Message-ID:
<465ca1e0$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<465a6397@newsgroups.borland.com> <465b4af1$1@newsgroups.borland.com> <465b9222$2@newsgroups.borland.com> <465bf3c5$1@newsgroups.borland.com> <465c84a0$1@newsgroups.borland.com> <465c8700$1@newsgroups.borland.com> <465c9924$1@newsgroups.borland.com> <465c9dcb$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,321743db05a8d51 X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "han" <hanzhangusa@yahoo.com> Sender: "han" <hanzhangusa@yahoo.com> Reply-To: "han" <hanzhangusa@yahoo.com> Subject: Re: problems of saving a registry key to a file Newsgroups: borland.public.cppbuilder.nativeapi X-User-Info: 64.143.221.52 References: <465a6397@newsgroups.borland.com> <465b4af1$1@newsgroups.borland.com> <465b9222$2@newsgroups.borland.com> <465bf3c5$1@newsgroups.borland.com> <465c84a0$1@newsgroups.borland.com> <465c8700$1@newsgroups.borland.com> <465c9924$1@newsgroups.borland.com> <465c9dcb$1@newsgroups.borland.com> NNTP-Posting-Host: 207.105.83.66 Message-ID: <465ca1e0$1@newsgroups.borland.com> Date: 29 May 2007 14:57:52 -0700 X-Trace: newsgroups.borland.com 1180475872 207.105.83.66 (29 May 2007 14:57:52 -0700) Lines: 75 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail