BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Registry Problems

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Chris
Guest





PostPosted: Sat Apr 29, 2006 10:03 am    Post subject: Registry Problems Reply with quote



Hi

On one of my customers PCs (Win XP) I have the following problem (which I
cannot reproduce).

The user starts the PC and says he does not have to log on so I guess they
don't have different users etc.

My app is started via a desktop link. It has no problems reading and
writing to the registry (it stores all registry info in the one place under
HKCU\Software\Jewesloft\Onyx). When the user wants to restore the database
(it's an invoicing system) my app starts another app that takes care of
stoping the server, replacing the files etc and then closes.

It starts this app using ShellExecute and this new app has no problems
reading and writing to the registry. Once the database has been restored
this second app now restarts the first app (again using ShellExecute) but
now the primary app doesn't see the data in the registry that it could a
moment or two before (i.e. the 'Please register now' screen is displayed
etc). The user can re-register and the setting is saved so I guess that the
app can still read and write to the registry. It's as if my registry keys
are deleted.

This second app reads from various keys that the first app sets (all in the
HKCU/software/jewelsoft/onyx location) and has code to flag errors so I am
sure that the second app can also see the current registry settings, the
last thing it does is set a key and delete another one from the HKLM area
(as shown below) and I believe this is what triggers the problem.


//set autorun last as this is the only thing that could fail if we don't
//have the correct admin privileges
if (ckAutostart->Checked)
{DeleteRegVal("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run","GenInv
Nexus Server",(int)HKEY_LOCAL_MACHINE); //This key may not exist
SetRegVal("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run","Nexus
Server",ServerWorkingDir + "nxServer.exe",true,(int)HKEY_LOCAL_MACHINE);
}
else
{DeleteRegVal("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run","GenInv
Nexus Server",(int)HKEY_LOCAL_MACHINE); //This key may not exist
DeleteRegVal("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run","Nexus
Server",(int)HKEY_LOCAL_MACHINE);
}


These are the functions
//---------------------------------------------------------------------------
String __fastcall TfrmServerAdmin::MyGetRegVal(String KeyToOpen, String
KeyToRead, String & Default, int RootKey)
{
String RetVal;

TRegistry *thisreg = new TRegistry;
thisreg->RootKey = (void*)RootKey; // Registry Section
thisreg->OpenKey(KeyToOpen,false);
RetVal = thisreg->ReadString(KeyToRead);
delete thisreg;
if (RetVal != "")
return RetVal;
else
return Default;
}
//-----------------------------------------------------------
bool __fastcall TfrmServerAdmin::SetRegVal(String KeyToOpen, String
KeyToWrite, String & StrToWrite, bool CreateKey,int RootKey)
{
bool RetVal = false;
TRegistry *thisreg = new TRegistry;
thisreg->RootKey = (void*)RootKey; // Registry Section
try
{
if (thisreg->OpenKey(KeyToOpen,CreateKey))
{thisreg->WriteString(KeyToWrite,StrToWrite);
RetVal = true;
}
else
RetVal = false;
}
__finally
{
delete thisreg;
return RetVal;
}
}
//-----------------------------------------------------
bool __fastcall TfrmServerAdmin::DeleteRegVal(String KeyToOpen, String
KeyToDelete, int RootKey)
{
bool RetVal = false;
TRegistry *thisreg = new TRegistry;
thisreg->RootKey = (void*)RootKey; // Registry Section
try
{
if (thisreg->OpenKey(KeyToOpen,false))
{thisreg->DeleteValue(KeyToDelete);
RetVal = true;
}
else
RetVal = false;
}
__finally
{
delete thisreg;
return RetVal;
}
}
//-----------------------------------------------------

I also know that this user is logged on as an administrator as he is able to
run my setup program (inno) which is set to check for admin users.

Does anyone have any idea why the app seems to lose the data stored in the
registry?

Could Windows be doing something in the background? Could it be
anti-spyware/anti-virus software? I don't think any other software is
affected (only mine) but the second app definately does not set any registry
keys other than that mentioned above.

Best regards

Chris
Back to top
Michel Leunen
Guest





PostPosted: Sat Apr 29, 2006 4:03 pm    Post subject: Re: Registry Problems Reply with quote



Chris wrote:

Quote:
String __fastcall TfrmServerAdmin::MyGetRegVal(String KeyToOpen, String
KeyToRead, String & Default, int RootKey)
{
String RetVal;

TRegistry *thisreg = new TRegistry;
thisreg->RootKey = (void*)RootKey; // Registry Section
thisreg->OpenKey(KeyToOpen,false);
RetVal = thisreg->ReadString(KeyToRead);
delete thisreg;
if (RetVal != "")
return RetVal;
else
return Default;
}

You don't check for errors in this function. Openkey return false on
error and ReadString may throw.


Quote:
Could it be
anti-spyware/anti-virus software?

There are applications that prevent from changing the
HKLM/SOFTWARE/Microsoft/Windows/CurrentVersion/Run key like
anti-spyware, anti-virus or stand alone software monitoring the run key.
For instance:
http://www.mlin.net/StartupMonitor.shtml
Maybe, your customer is running such an application. If I'm not
mistaken, Bit-Defender, the Microsoft anti-spyware does this and most of
the antivirus programs.

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Back to top
Clayton Arends
Guest





PostPosted: Sat Apr 29, 2006 4:03 pm    Post subject: Re: Registry Problems Reply with quote



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
Back to top
Chris
Guest





PostPosted: Sun Apr 30, 2006 11:03 am    Post subject: Re: Registry Problems Reply with quote

Thanks for the suggestions (and fixes <blush>).

I particularily like the smart pointers - I have tried them before but I
just didn't realise I could use them in this way.

Anyway, many thanks

Chris
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.