🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1872

Re: problems of saving a registry key to a file

#1872
From: Remy Lebeau (TeamB)
Date: Mon, 28 May 2007 21:33
62 lines
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
Technical Details
Message-ID: <465b4af1$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.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
References: <465a6397@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: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <465a6397@newsgroups.borland.com>
Subject: Re: problems of saving a registry key to a file
Date: Mon, 28 May 2007 14:33:41 -0700
Lines: 64
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.82.252.126
Message-ID: <465b4af1$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1180388081 75.82.252.126 (28 May 2007 14:34:41 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.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