Thread View: borland.public.cppbuilder.nativeapi
10 messages
10 total messages
Started by han
Mon, 28 May 2007 05:07
problems of saving a registry key to a file
Author: han
Date: Mon, 28 May 2007 05:07
Date: Mon, 28 May 2007 05:07
12 lines
1342 bytes
1342 bytes
I am trying to save a key to a file using SaveKey, but I always fail. The open key is successfull, but saveKey is not successful, The file is generated, but the size of the file is zero. The following is my code.
TRegistry myRegistry = new TRegistry;
AnsiString Key = "Software\\XXXCompany\\Settings\\ViewForms"
if ( myRegistry->OpenKey (key, false) != true )
Application->MessageBox("open key error", NULL, MB_OK);
AnsiString fileName("tempReg");
if ( myRegistry->SaveKey(key, fileName) != true)
Application->MessageBox("save key error", NULL, MB_OK);;
Any suggestions? Thanks a lot.
Re: problems of saving a registry key to a file
Author: Bob Gonder
Date: Mon, 28 May 2007 21:06
Date: Mon, 28 May 2007 21:06
9 lines
1286 bytes
1286 bytes
han wrote: >Any suggestions? Thanks a lot. Please do not multipost. Please pick only one group to post your question to. See my answer in the IDE group. If that doesn't answer your question, then ask here for further information.
Re: problems of saving a registry key to a file
Author: Remy Lebeau (TeamB)
Date: Mon, 28 May 2007 21:33
Date: Mon, 28 May 2007 21:33
62 lines
2922 bytes
2922 bytes
"han" <hanzhangusa@yahoo.com> wrote in message
news:465a6397@newsgroups.borland.com...
> I am trying to save a key to a file using SaveKey, but I always
> fail. The open key is successfull, but saveKey is not successful,
> The file is generated, but the size of the file is zero. The
following
> is my code.
The key you are specifying has no leading '\\' in it, so it is going
to be treated as relative to whatever the currently active key is.
When you call OpenKey(), there is no active key yet, so your key
string is relative to the RootKey and thus succeeds in being opened if
it exists. But when you then call SaveKey() afterwards, TRegistry
tries to open your key again, but relative to the key you already
opened, and thus fails to find the new key. In other words, SaveKey()
is looking for
"Software\\XXXCompany\\Settings\\ViewForms\\Software\\XXXCompany\\Sett
ings\\ViewForms", which obviously does not exist.
The best solution is to get rid of the OpenKey() altogether, since
SaveKey() opens the key anyway:
TRegistry *myRegistry = new TRegistry;
try
{
if(
!myRegistry->SaveKey("Software\\XXXCompany\\Settings\\ViewForms",
"tempReg") )
Application->MessageBox("save key error", NULL, MB_OK);
}
__finally
{
delete myRegistry;
}
If you want to use OpenKey() to validate the key's existance
beforehand, then specify an empty string when calling SaveKey() so
that it obtains a second handle to the same key that is already open:
TRegistry *myRegistry = new TRegistry;
try
{
if(
myRegistry->OpenKey("Software\\XXXCompany\\Settings\\ViewForms",
false) )
{
if( !myRegistry->SaveKey("", "tempReg") )
Application->MessageBox("save key error", NULL,
MB_OK);
}
else
Application->MessageBox("open key error", NULL, MB_OK);
}
__finally
{
delete myRegistry;
}
Gambit
Re: problems of saving a registry key to a file
Author: han
Date: Tue, 29 May 2007 02:38
Date: Tue, 29 May 2007 02:38
74 lines
3471 bytes
3471 bytes
Remy,
Thank you so much for your help. I did not have openkey function before. I add this later because savekey gives me error. So I thought I am going to see if it open key OK. I tried your way, it is still the same. I tried directly using savekey without using openkey function, still the same problem. I alse make sure the file does not exist before I am running the code.
Let me know if you have any other suggestion. Thanks.
han zhang
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"han" <hanzhangusa@yahoo.com> wrote in message
>news:465a6397@newsgroups.borland.com...
>
>> I am trying to save a key to a file using SaveKey, but I always
>> fail. The open key is successfull, but saveKey is not successful,
>> The file is generated, but the size of the file is zero. The
>following
>> is my code.
>
>The key you are specifying has no leading '\\' in it, so it is going
>to be treated as relative to whatever the currently active key is.
>When you call OpenKey(), there is no active key yet, so your key
>string is relative to the RootKey and thus succeeds in being opened if
>it exists. But when you then call SaveKey() afterwards, TRegistry
>tries to open your key again, but relative to the key you already
>opened, and thus fails to find the new key. In other words, SaveKey()
>is looking for
>"Software\\XXXCompany\\Settings\\ViewForms\\Software\\XXXCompany\\Sett
>ings\\ViewForms", which obviously does not exist.
>
>The best solution is to get rid of the OpenKey() altogether, since
>SaveKey() opens the key anyway:
>
> TRegistry *myRegistry = new TRegistry;
> try
> {
> if(
>!myRegistry->SaveKey("Software\\XXXCompany\\Settings\\ViewForms",
>"tempReg") )
> Application->MessageBox("save key error", NULL, MB_OK);
> }
> __finally
> {
> delete myRegistry;
> }
>
>If you want to use OpenKey() to validate the key's existance
>beforehand, then specify an empty string when calling SaveKey() so
>that it obtains a second handle to the same key that is already open:
>
> TRegistry *myRegistry = new TRegistry;
> try
> {
> if(
>myRegistry->OpenKey("Software\\XXXCompany\\Settings\\ViewForms",
>false) )
> {
> if( !myRegistry->SaveKey("", "tempReg") )
> Application->MessageBox("save key error", NULL,
>MB_OK);
> }
> else
> Application->MessageBox("open key error", NULL, MB_OK);
> }
> __finally
> {
> delete myRegistry;
> }
>
>
>Gambit
>
>
Re: problems of saving a registry key to a file
Author: Remy Lebeau (TeamB)
Date: Tue, 29 May 2007 09:28
Date: Tue, 29 May 2007 09:28
18 lines
1652 bytes
1652 bytes
"han" <hanzhangusa@yahoo.com> wrote in message news:465b9222$2@newsgroups.borland.com... > I tried your way, it is still the same. I tried directly using savekey > without using openkey function, still the same problem. I alse make > sure the file does not exist before I am running the code. Did you try calling GetLastError() after SaveKey() fails? I suggest you go to http://www.deja.com and search through the newsgroup archives. Discussions regarding SaveKey() and LoadKey(), and their cooresponding RegSaveKey() and RegLoadKey() API functions, have been posted many times before. Gambit
Re: problems of saving a registry key to a file
Author: han
Date: Tue, 29 May 2007 19:53
Date: Tue, 29 May 2007 19:53
26 lines
1734 bytes
1734 bytes
Thanks for the help. I tried using GetLastError(), it says the "the Operation completed successfully". It is so strange. han zhang "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote: > >"han" <hanzhangusa@yahoo.com> wrote in message >news:465b9222$2@newsgroups.borland.com... > >> I tried your way, it is still the same. I tried directly using >savekey >> without using openkey function, still the same problem. I alse make >> sure the file does not exist before I am running the code. > >Did you try calling GetLastError() after SaveKey() fails? > >I suggest you go to http://www.deja.com and search through the >newsgroup archives. Discussions regarding SaveKey() and LoadKey(), >and their cooresponding RegSaveKey() and RegLoadKey() API functions, >have been posted many times before. > > >Gambit > >
Re: problems of saving a registry key to a file
Author: Remy Lebeau (TeamB)
Date: Tue, 29 May 2007 19:59
Date: Tue, 29 May 2007 19:59
39 lines
2098 bytes
2098 bytes
"han" <hanzhangusa@yahoo.com> wrote in message
news:465c84a0$1@newsgroups.borland.com...
> I tried using GetLastError(), it says the "the Operation completed
> successfully". It is so strange.
That means that GetLastError() is being cleared before SaveKey()
exits, probably by RegCloseKey(). So you will have to do what dhoke
suggested in the .ide newsgroup - use the Win32 API directly instead
of TRegistry. For example:
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 )
{
dwErr = GetLastError();
Application->MessageBox(("save key error: " +
AnsiString(dwErr)).c_str(), NULL, MB_OK);
}
RegCloseKey(hKey);
}
else
{
dwErr = GetLastError();
Application->MessageBox(("open key error: " +
AnsiString(dwErr)).c_str(), NULL, MB_OK);
}
Gambit
Re: problems of saving a registry key to a file
Author: han
Date: Tue, 29 May 2007 21:20
Date: Tue, 29 May 2007 21:20
47 lines
2371 bytes
2371 bytes
I got save key error :0, which I believe it means the operation completed successfully.
han zhang
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"han" <hanzhangusa@yahoo.com> wrote in message
>news:465c84a0$1@newsgroups.borland.com...
>
>> I tried using GetLastError(), it says the "the Operation completed
>> successfully". It is so strange.
>
>That means that GetLastError() is being cleared before SaveKey()
>exits, probably by RegCloseKey(). So you will have to do what dhoke
>suggested in the .ide newsgroup - use the Win32 API directly instead
>of TRegistry. For example:
>
> 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 )
> {
> dwErr = GetLastError();
> Application->MessageBox(("save key error: " +
>AnsiString(dwErr)).c_str(), NULL, MB_OK);
> }
> RegCloseKey(hKey);
> }
> else
> {
> dwErr = GetLastError();
> Application->MessageBox(("open key error: " +
>AnsiString(dwErr)).c_str(), NULL, MB_OK);
> }
>
>
>Gambit
>
>
Re: problems of saving a registry key to a file
Author: Remy Lebeau (TeamB)
Date: Tue, 29 May 2007 21:38
Date: Tue, 29 May 2007 21:38
32 lines
2044 bytes
2044 bytes
"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
Re: problems of saving a registry key to a file
Author: 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
>
>
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads