🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages Started by Chris Sat, 29 Apr 2006 09:05
Registry Problems
#3716
Author: Chris
Date: Sat, 29 Apr 2006 09:05
123 lines
5374 bytes
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 
Re: Registry Problems
#3722
Author: Michel Leunen
Date: Sat, 29 Apr 2006 15:07
42 lines
2224 bytes
Chris wrote:

> 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.


>  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/
----------------------------------------
Re: Registry Problems
#3724
Author: Clayton Arends
Date: Sat, 29 Apr 2006 15:40
56 lines
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 
Re: Registry Problems
#3733
Author: Chris
Date: Sun, 30 Apr 2006 10:26
8 lines
1131 bytes
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 
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