#include "StdAfx.h"
#include "registerex.h"
#include <stdlib.h>
#pragma warning ( disable : 4267 )
#define MAX_BUFFER 2048
char buffer[MAX_BUFFER];
CString pt;
CRegisterEx::CRegisterEx(CString path)
{
pt = path;
}
CRegisterEx::~CRegisterEx(void)
{
}
// Writing strings to the register.
void CRegisterEx::WriteString(CString str, CString subPath, CString Key)
{
HKEY hk;
TCHAR szBuf[2048];
CString insidePath = pt;
if (Key)
{
insidePath = insidePath + "\\" + subPath;
}
if (RegCreateKey(HKEY_CURRENT_USER, _T(insidePath), &hk))
{
// Woops, you don't have privileges
TRACE0("Could not create the registry key.\n\nDo you have the right privileges?\n");
}
strcpy(szBuf, str);
if (RegSetValueEx(hk, _T(Key), 0, REG_EXPAND_SZ, (LPBYTE)szBuf, strlen(str) + 1))
{
// Hmm, you did something wrong
TRACE0("Could not set the given String.\n\nDo you have the right privileges?\n");
}
RegCloseKey(hk);
}
// Reading strings from the register
CString CRegisterEx::ReadString(CString subPath, CString Key)
{
HKEY hk;
CString insidePath = pt;
TCHAR str[MAX_BUFFER];
DWORD length = MAX_BUFFER;
if (subPath)
{
insidePath = insidePath + "\\" + subPath;
}
if (RegOpenKey(HKEY_CURRENT_USER, _T(insidePath), &hk))
{
// Hey there, you're regpath doesn't exist (I don't give an error, but just return NULL)
return NULL;
}
if (RegQueryValueEx(hk, _T(Key), NULL, NULL, (LPBYTE)str, &length))
{
// woops, can't find the Key
return NULL;
}
RegCloseKey(hk);
return CString(str);
}
// Writing integers to the register
void CRegisterEx::WriteInteger(int n, CString subPath, CString Key)
{
_itoa(n, buffer, 10);
CString str = (LPCSTR)buffer;
WriteString(str, subPath, Key);
}
// Reading integers from the register
int CRegisterEx::ReadInteger(CString subPath, CString Key)
{
CString str = ReadString(subPath, Key);
int n = atoi(str);
return n;
}
// writing POINT structures to the register
void CRegisterEx::WritePoint(CPoint pt, CString subPath, CString Key)
{
CString keyX, keyY;
keyX = Key + "_X";
keyY = Key + "_Y";
WriteInteger(pt.x, subPath, keyX);
WriteInteger(pt.y, subPath, keyY);
}
// reading POINT structures from the register
CPoint CRegisterEx::ReadPoint(CString subPath, CString Key)
{
CString keyX, keyY;
keyX = Key + "_X";
keyY = Key + "_Y";
int x = ReadInteger(subPath, keyX);
int y = ReadInteger(subPath, keyY);
return CPoint(x, y);
}
// writing LPCRECT structures to the register
void CRegisterEx::WriteRect(CRect rect, CString subPath, CString Key)
{
CString keyC, keyD;
keyC = Key + "_C";
keyD = Key + "_D";
WritePoint(CPoint(rect.top, rect.left), subPath, keyC);
WritePoint(CPoint(rect.bottom, rect.right), subPath, keyD);
}
// reading LPCRECT structures from the register
CRect CRegisterEx::ReadRect(CString subPath, CString Key)
{
CString keyC, keyD;
keyC = Key + "_C";
keyD = Key + "_D";
CPoint ptC = ReadPoint(subPath, keyC);
CPoint ptD = ReadPoint(subPath, keyD);
return CRect(ptC, ptD);
}
// writing double values to the register
void CRegisterEx::WriteDouble(double d, CString subPath, CString Key)
{
_gcvt(d, 100, buffer);
CString str = (LPCSTR)buffer;
CString doubleKey = Key + "_d";
WriteString(str, subPath, doubleKey);
}
// reading double values from the register
double CRegisterEx::ReadDouble(CString subPath, CString Key)
{
CString doubleKey = Key + "_d";
CString str = ReadString(subPath, doubleKey);
double d = atof(str);
return d;
}