summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/registry.cpp192
-rw-r--r--src/test/w32dtst.cpp4
-rw-r--r--src/w32dlib/registry.h147
3 files changed, 342 insertions, 1 deletions
diff --git a/src/registry.cpp b/src/registry.cpp
new file mode 100644
index 0000000..e16d109
--- /dev/null
+++ b/src/registry.cpp
@@ -0,0 +1,192 @@
+// w32dlib - Win32 Dialog Helpers
+//
+// Copyright (C) 2005 Ian Cowburn (ianc@noddybox.demon.co.uk)
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// -------------------------------------------------------------------------
+//
+#include <cstring>
+#include "w32dlib/registry.h"
+
+namespace W32DLib
+{
+
+// ------------------------------------------------------------
+//
+Registry::Registry(HKEY root, const char *path, bool permanent)
+{
+ m_openres=RegCreateKeyEx(root,
+ path,
+ 0,
+ 0,
+ permanent ? REG_OPTION_NON_VOLATILE:
+ REG_OPTION_VOLATILE,
+ KEY_WRITE|KEY_READ,
+ 0,
+ &m_key,
+ 0);
+}
+
+
+// ------------------------------------------------------------
+//
+Registry::Registry(HKEY root, const char *path)
+{
+ m_openres=RegOpenKeyEx(root,path,0,KEY_READ,&m_key);
+}
+
+
+// ------------------------------------------------------------
+//
+Registry::~Registry()
+{
+ if (m_openres==ERROR_SUCCESS)
+ {
+ RegCloseKey(m_key);
+ }
+}
+
+
+// ------------------------------------------------------------
+//
+bool Registry::IsOpen()
+{
+ return m_openres==ERROR_SUCCESS;
+}
+
+
+// ------------------------------------------------------------
+//
+bool Registry::Read(const char *name, bool& val)
+{
+ unsigned i=0;
+ bool res;
+
+ res=Read(name,i);
+
+ if (res)
+ {
+ val=(i!=0);
+ }
+
+ return res;
+}
+
+
+// ------------------------------------------------------------
+//
+bool Registry::Read(const char *name, unsigned& val)
+{
+ DWORD size=sizeof(val);
+ DWORD type;
+ unsigned i;
+
+ if (RegQueryValueEx(m_key,name,0,&type,(LPBYTE)&i,&size)==ERROR_SUCCESS)
+ {
+ if (type==REG_DWORD)
+ {
+ val=i;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+// ------------------------------------------------------------
+//
+bool Registry::Read(const char *name, std::string& val)
+{
+ bool res=false;
+ DWORD size;
+ DWORD type;
+
+ if (RegQueryValueEx(m_key,name,0,&type,0,&size)==ERROR_SUCCESS)
+ {
+ if (type==REG_SZ || type==REG_EXPAND_SZ)
+ {
+ char *buff=new char[size];
+
+ if (RegQueryValueEx(m_key,name,0,0,(LPBYTE)buff,&size)
+ ==ERROR_SUCCESS)
+ {
+ res=true;
+ val=buff;
+ }
+
+ delete[] buff;
+ }
+ }
+
+ return res;
+}
+
+
+// ------------------------------------------------------------
+//
+bool Registry::Write(const char *name, bool val)
+{
+ unsigned i=val?1:0;
+
+ return Write(name,i);
+}
+
+
+// ------------------------------------------------------------
+//
+bool Registry::Write(const char *name, unsigned val)
+{
+ return RegSetValueEx(m_key,
+ name,
+ 0,
+ REG_DWORD,
+ (LPBYTE)&val,
+ sizeof(val))==ERROR_SUCCESS;
+}
+
+
+// ------------------------------------------------------------
+//
+bool Registry::Write(const char *name, const char *val)
+{
+ return RegSetValueEx(m_key,
+ name,
+ 0,
+ REG_SZ,
+ (LPBYTE)val,
+ std::strlen(val)+1)==ERROR_SUCCESS;
+}
+
+
+
+// ------------------------------------------------------------
+//
+bool Registry::Write(const char *name, const std::string& val)
+{
+ return RegSetValueEx(m_key,
+ name,
+ 0,
+ REG_SZ,
+ (LPBYTE)val.c_str(),
+ val.length()+1)==ERROR_SUCCESS;
+}
+
+
+
+}; // namespace W32DLib
+
+// END OF FILE
diff --git a/src/test/w32dtst.cpp b/src/test/w32dtst.cpp
index bc5e6a6..e940bcd 100644
--- a/src/test/w32dtst.cpp
+++ b/src/test/w32dtst.cpp
@@ -336,7 +336,7 @@ private:
static void ReadInt(const char *mode, W32DLib::Registry& r, const char *name)
{
- unsigned i;
+ unsigned i=0;
bool res=r.Read(name,i);
std::cout << "ReadInt(mode=" << mode
@@ -389,6 +389,8 @@ int WINAPI WinMain (HINSTANCE hInstance,
ReadStr("rw",rw,"str");
ReadInt("ro",rd,"int");
ReadStr("ro",rd,"str");
+ ReadStr("rw",rw,"int");
+ ReadInt("rw",rw,"str");
WriteInt("rw",rw,"int");
WriteStr("rw",rw,"str");
WriteInt("ro",rd,"int");
diff --git a/src/w32dlib/registry.h b/src/w32dlib/registry.h
new file mode 100644
index 0000000..7dc38f0
--- /dev/null
+++ b/src/w32dlib/registry.h
@@ -0,0 +1,147 @@
+// w32dlib - Win32 Dialog Helpers
+//
+// Copyright (C) 2005 Ian Cowburn (ianc@noddybox.demon.co.uk)
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// -------------------------------------------------------------------------
+//
+#ifndef W32DLIB_REGISTRY_H
+
+#define W32DLIB_REGISTRY_H "$Id$"
+
+#include "w32dlib/base.h"
+
+namespace W32DLib
+{
+
+ /// \brief The Registry class.
+ ///
+ /// Gives basic registry access.
+ ///
+ class Registry
+ {
+ public:
+
+ /// \brief Opens the supplied registry key read/write
+ ///
+ /// \param root The root of the key (use Windows HKEY_xxx constants).
+ /// \param path The path to the key.
+ /// \param permanent If false then the key is created volatile (ie. not
+ /// saved to disk). This is ignored by Windows for Win9X.
+ ///
+ Registry(HKEY root, const char *path, bool permanent);
+
+ /// \brief Opens the supplied registry key read only
+ ///
+ /// \param root The root of the key (use Windows HKEY_xxx constants).
+ /// \param path The path to the key.
+ ///
+ Registry(HKEY root, const char *path);
+
+ /// \brief Destructor.
+ ///
+ /// This closes the registry key.
+ ///
+ virtual ~Registry();
+
+
+ /// \brief Sees whether the key was opened.
+ ///
+ /// \return True if the key was created/opened OK.
+ ///
+ bool IsOpen();
+
+
+ /// \brief Reads a boolean value from the registry.
+ ///
+ /// Booleans are implemented as a DWORD entry with a value 1 or 0.
+ ///
+ /// \param name The name of the value.
+ /// \param val A reference to a place to store the result.
+ /// \return True if the value could be read.
+ ///
+ bool Read(const char *name, bool& val);
+
+
+ /// \brief Reads an integer value from the registry.
+ ///
+ /// \param name The name of the value.
+ /// \param val A reference to a place to store the result.
+ /// \return True if the value could be read.
+ ///
+ bool Read(const char *name, unsigned& val);
+
+
+ /// \brief Reads a string value from the registry.
+ ///
+ /// \param name The name of the value.
+ /// \param val A reference to a place to store the result.
+ /// \return True if the value could be read.
+ ///
+ bool Read(const char *name, std::string& val);
+
+
+ /// \brief Writes a boolean value to the registry.
+ ///
+ /// Booleans are implemented as a DWORD entry with a value 1 or 0.
+ ///
+ /// \param name The name of the value.
+ /// \param val The value.
+ /// \return True if the value could be read.
+ ///
+ bool Write(const char *name, bool val);
+
+
+ /// \brief Writes an integer value to the registry.
+ ///
+ /// \param name The name of the value.
+ /// \param val The value.
+ /// \return True if the value was written.
+ ///
+ bool Write(const char *name, unsigned val);
+
+
+ /// \brief Writes a string value to the registry.
+ ///
+ /// \param name The name of the value.
+ /// \param val The value.
+ /// \return True if the value was written.
+ ///
+ bool Write(const char *name, const char *val);
+
+
+ /// \brief Writes a string value to the registry.
+ ///
+ /// \param name The name of the value.
+ /// \param val The value.
+ /// \return True if the value was written.
+ ///
+ bool Write(const char *name, const std::string& val);
+
+
+ private:
+
+ HKEY m_key;
+ LONG m_openres;
+
+ }; // class Registry
+
+}; // namespace w32dlib
+
+#endif // W32DLIB_REGISTRY_H
+
+
+// END OF FILE