Article View: borland.public.cppbuilder.nativeapi
Article #3724Re: Registry Problems
From: Clayton Arends
Date: Sat, 29 Apr 2006 15:40
Date: Sat, 29 Apr 2006 15:40
56 lines
2896 bytes
2896 bytes
Chris,
Have you tried to read back the registry entries immediately after writing
them? Perhaps they're not really getting written.
I do have a tiny suggestion about memory management for you. It is
advantageous for a C++ programmer to use the STL class "std::auto_ptr" to
handle the destruction of a pointer that has been allocated on the heap. It
simplifies code and is exception safe which means that you don't have to
code try...catch or try...finally blocks just to handle memory. Here is
your code modified to use auto_ptr.
#include <memory> // this is where auto_ptr is
String __fastcall TfrmServerAdmin::MyGetRegVal(String KeyToOpen,
String KeyToRead, String & Default, int RootKey)
{
TRegistry *thisreg = new TRegistry;
std::auto_ptr<TRegistry> auto_thisreg(thisreg);
thisreg->RootKey = (void*)RootKey; // Registry Section
if (thisreg->OpenKeyReadOnly(KeyToOpen))
if (thisreg->ValueExists(KeyToRead))
return thisreg->ReadString(KeyToRead);
return Default;
}
bool __fastcall TfrmServerAdmin::SetRegVal(String KeyToOpen,
String KeyToWrite, String & StrToWrite, bool CreateKey,
int RootKey)
{
TRegistry *thisreg = new TRegistry;
std::auto_ptr<TRegistry> auto_thisreg(thisreg);
thisreg->RootKey = (void*)RootKey; // Registry Section
if (thisreg->OpenKey(KeyToOpen,CreateKey))
{
thisreg->WriteString(KeyToWrite,StrToWrite);
return true;
}
return false;
}
bool __fastcall TfrmServerAdmin::DeleteRegVal(String KeyToOpen,
String KeyToDelete, int RootKey)
{
TRegistry *thisreg = new TRegistry;
std::auto_ptr<TRegistry> auto_thisreg(thisreg);
thisreg->RootKey = (void*)RootKey; // Registry Section
return
thisreg->OpenKey(KeyToOpen,false) &&
thisreg->DeleteValue(KeyToDelete);
}
Another suggestion. The 'RootKey' parameter is type HKEY. Why not specify
that in your argument list instead of "int" to avoid confusion?
- Clayton
Technical Details
Message-ID:
<44538907$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<44532c52$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b99e639ffcfc4f13 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Clayton Arends" <nospam_claytonarends@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <44532c52$1@newsgroups.borland.com> Subject: Re: Registry Problems Date: Sat, 29 Apr 2006 08:40:59 -0700 Lines: 58 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 X-RFC2646: Format=Flowed; Response NNTP-Posting-Host: 71.83.244.156 Message-ID: <44538907$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1146325255 71.83.244.156 (29 Apr 2006 08:40:55 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail