RegistryUtil.cpp

/****************************************************************************/

/**                                                                        **/

/** Copyright (c) 2005 Diodia Software          (http://www.diodia.com/)   **/

/**                                                                        **/

/** Use in any form with or without modification for commercial and non-   **/

/** commercial purposes is permitted.                                      **/

/**                                                                        **/

/****************************************************************************/

#include "stdafx.h"

 

#include "Helper.h"

 

/****************************************************************************/

/*

/* DESCRIPTION : Save a string value in the registry.

/*

/* RETURN VALUE: true - if OK

/*               false - if error

/*

/****************************************************************************/

bool RegistrySaveString(HKEY MainKey, const char *SubKey, const char *Name, const char *Value)

{

   HKEY         Key;

   LPBYTE       Data = NULL;

   DWORD        Size = 0;

   LONG         Res;

 

   Res = RegCreateKeyEx(MainKey, SubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &Key, NULL);

   if (Res != ERROR_SUCCESS)

      return false;

 

   Data = (LPBYTE) strdup(Value ? Value : "");

   Size = (DWORD) strlen((const char *) Data) + 1;

 

   Res = RegSetValueEx(Key, Name, NULL, REG_SZ, Data, Size);

 

   free(Data);

   RegCloseKey(Key);

 

   return true;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Read a string value from the registry.

/*

/* RETURN VALUE: Value - if OK

/*               Empty string object - otherwise

/*

/****************************************************************************/

std::string RegistryReadString(HKEY MainKey, const char *SubKey, const char *Name)

{

   HKEY         Key;

   LONG         Res;

   DWORD        RegType;

   char        *Buffer;

   DWORD        Size;

   std::string  Value;

 

   Res = RegOpenKeyEx(MainKey, SubKey, 0,  KEY_ALL_ACCESS, &Key);

   if (Res != ERROR_SUCCESS)

      return Value;

 

   Res = RegQueryValueEx(Key, Name, NULL, &RegType, NULL, NULL);

   if (Res != ERROR_SUCCESS || RegType != REG_SZ)

   {

      RegCloseKey(Key);

      return Value;

   }

 

   Res = RegQueryValueEx(Key, Name, NULL, &RegType, NULL, &Size);

   if (Res != ERROR_SUCCESS)

   {

      RegCloseKey(Key);

      return Value;

   }

 

   Buffer = (char *) malloc((Size + 1) * sizeof(char));

 

   Res = RegQueryValueEx(Key, Name, NULL, &RegType, (LPBYTE) Buffer, &Size);

   RegCloseKey(Key);

   if (Res != ERROR_SUCCESS)

   {

      free(Buffer);

      return Value;

   }

 

   Buffer[Size] = '\0';

   Value = Buffer;

   free(Buffer);

 

   return Value;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Save an integer value in the registry.

/*

/* RETURN VALUE: true - if OK

/*               false - if error

/*

/****************************************************************************/

bool RegistrySaveInt(HKEY MainKey, const char *SubKey, const char *Name, int Value)

{

   HKEY         Key;

   LPBYTE       Data = NULL;

   DWORD        Size = 0;

   LONG         Res;

 

   Res = RegCreateKeyEx(MainKey, SubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &Key, NULL);

   if (Res != ERROR_SUCCESS)

      return false;

 

   Res = RegSetValueEx(Key, Name, NULL, REG_DWORD, (LPBYTE) &Value, sizeof(DWORD));

 

   free(Data);

   RegCloseKey(Key);

 

   return true;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Read an integer value from the registry.

/*

/* RETURN VALUE: Value - if OK

/*               0 - otherwise

/*

/****************************************************************************/

int RegistryReadInt(HKEY MainKey, const char *SubKey, const char *Name)

{

   HKEY         Key;

   LONG         Res;

   DWORD        RegType;

   DWORD        Size = sizeof(DWORD);

   DWORD        Value;

 

   Res = RegOpenKeyEx(MainKey, SubKey, 0,  KEY_ALL_ACCESS, &Key);

   if (Res != ERROR_SUCCESS)

      return 0;

 

   Res = RegQueryValueEx(Key, Name, NULL, &RegType, NULL, NULL);

   if (Res != ERROR_SUCCESS || RegType != REG_DWORD)

   {

      RegCloseKey(Key);

      return 0;

   }

 

   Res = RegQueryValueEx(Key, Name, NULL, &RegType, (LPBYTE) &Value, &Size);

   RegCloseKey(Key);

   if (Res != ERROR_SUCCESS)

      return 0;

 

   return Value;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Read binary data from the registry.

/*

/* RETURN VALUE: Size of data - if OK

/*               0 - if error

/*

/****************************************************************************/

int RegistryReadBinary(HKEY MainKey, const char *SubKey, const char *Name, char **Buffer)

{

   HKEY         Key;

   LONG         Res;

   DWORD        RegType;

   DWORD        Size;

 

   *Buffer = NULL;

 

   Res = RegOpenKeyEx(MainKey, SubKey, 0,  KEY_QUERY_VALUE, &Key);

   if (Res != ERROR_SUCCESS)

      return 0;

 

   Res = RegQueryValueEx(Key, Name, NULL, &RegType, NULL, &Size);

   if (Res != ERROR_SUCCESS)

   {

      RegCloseKey(Key);

      return 0;

   }

 

   *Buffer = (char *) malloc((Size) * sizeof(char));

 

   Res = RegQueryValueEx(Key, Name, NULL, &RegType, (LPBYTE) *Buffer, &Size);

   RegCloseKey(Key);

   if (Res != ERROR_SUCCESS)

   {

      free(*Buffer);

      *Buffer = NULL;

      return 0;

   }

 

   return Size;

}

 





Source Files
   Commands.cpp
   Context.cpp
   CreatePath.cpp
   DateParser.cpp
   DlgEditFeedC.cpp
   DlgManageFeedsC.cpp
   Hook.cpp
   Iso8601.cpp
   MsXmlMisc.cpp
   MsXmlSelect.cpp
   RegistryNotify.cpp
   RegistryUtil.cpp
   RSS.cpp
   RSS.def
   RSS.idl
   RssAction.cpp
   RssDownloader.cpp
   RssFeed.cpp
   RssHelpers.cpp
   RssInit.cpp
   RssItem.cpp
   RssMultiFeed.cpp
   RssTimer.cpp
   RssXml.cpp
   StdAfx.cpp

Header Files
   Commands.h
   ContextC.h
   DateParser.h
   DlgEditFeedC.h
   DlgManageFeedsC.h
   Helper.h
   HookC.h
   RegistryNotify.h
   RegistryNotifyC.h
   Resource.h
   RssDownloaderC.h
   RssFeedC.h
   RssHelpers.h
   RssItemC.h
   RssMultiFeedC.h
   StdAfx.h
   ThreadObject.h
   XmlHelpers.h

Resource Files
   about.htm
   DiodiaLogoSmall.gif
   logo.bmp
   manifest.xml
   read.bmp
   RSS.rc
   RSS.rgs
   ToolBandLayout.xml
 

© 2002-2008 Diodia Software