diff options
author | Ian C <ianc@noddybox.co.uk> | 2005-04-17 21:58:06 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2005-04-17 21:58:06 +0000 |
commit | d496cf2407129524c50169a56b6a296afa34dbd4 (patch) | |
tree | 606fad38a6a47e07bf4bf9a81ba65ab203b46950 /src/registry.cpp | |
parent | 7740ebb87b8aa440f10095a77fea5d781844dd97 (diff) |
Forgot to add registry code in previous checkin
Diffstat (limited to 'src/registry.cpp')
-rw-r--r-- | src/registry.cpp | 192 |
1 files changed, 192 insertions, 0 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 |