Main program test_registry.cpp
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
--
#include "stdafx.h"
#include "CFRegistry.h"
int main(int argc, char* argv[])
{
DWORD dw;
CFRegistry *obj = new CFRegistry(NULL,NULL,NULL)
;
obj->Write("key","value",N
ULL);
obj->Read("key",NULL,dw,NU
LL);
printf("Read %s\n",dw);
printf("Hello World!\n");
return 0;
}
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----
CFRegistry.h contains the declaration of the class CFRegistry
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----
#ifndef CFREGISTRY_H
#define CFREGISTRY_H
#include "SysDef.h"
/*************************
**********
**********
**********
**********
*********
CFRegistry class definition
**************************
**********
**********
**********
**********
********/
class CFRegistry
{
protected:
LPVOID mhKey;
BOOL CopyString(LPTSTR lpDest, DWORD dwDestSize, LPTSTR lpSrc);
public:
CFRegistry(LPTSTR lpName=NULL, LPTSTR lpSection=NULL, LPTSTR lpVendor=NULL);
CFRegistry(CFRegistry* pParent, LPTSTR lpName);
~CFRegistry(void);
BOOL Write(LPTSTR lpEntry, DWORD dwValue, LPTSTR lpSection=NULL);
BOOL Read(LPTSTR lpEntry, LPDWORD lpdwValue, DWORD dwDefault, LPTSTR lpSection=NULL);
BOOL Write(LPTSTR lpEntry, WORD wValue, LPTSTR lpSection=NULL);
BOOL Read(LPTSTR lpEntry, LPWORD lpwValue, WORD wDefault, LPTSTR lpSection=NULL);
BOOL Write(LPTSTR lpEntry, BOOL bValue, LPTSTR lpSection=NULL);
BOOL Read(LPTSTR lpEntry, BOOL* lpbValue, BOOL bDefault, LPTSTR lpSection=NULL);
BOOL Write(LPTSTR lpEntry, LPTSTR lpValue, LPTSTR lpSection=NULL);
BOOL Read(LPTSTR lpEntry, DWORD dwStringBuffSize, LPTSTR lpValue, LPTSTR lpDefault, LPTSTR lpSection=NULL);
};
#endif
/* eof */
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
CFRegistry.cpp contains the body of the class CFRegistry
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
---------
#include "CFRegistry.h"
/*************************
**********
**********
**********
**********
**********
** CFRegistry **
**************************
**********
**********
**********
**********
*********/
CFRegistry::CFRegistry(LPT
STR lpName, LPTSTR lpSection, LPTSTR lpVendor)
{
LPTSTR lpTemp = NULL;
DWORD dwLen = 0;
if(lpName)
dwLen = lstrlen(lpName);
if(lpSection)
dwLen += lstrlen(lpSection);
if(lpVendor)
dwLen += lstrlen(lpVendor);
lpTemp = new TCHAR[dwLen+255];
if(!lpTemp)
mhKey = NULL;
else
{
DWORD dwStatus;
DWORD dwDummy;
if(!lpVendor)
lstrcpy(lpTemp, TEXT("SOFTWARE\\FINDANT\\"
));
else
{
lstrcpy(lpTemp, TEXT("SOFTWARE\\"));
lstrcat(lpTemp, lpVendor);
lstrcat(lpTemp, TEXT("\\"));
}
if(lpName)
{
lstrcat(lpTemp, lpName);
lstrcat(lpTemp, TEXT("\\"));
}
if(lpSection)
{
lstrcat(lpTemp, lpSection);
lstrcat(lpTemp, TEXT("\\"));
}
dwStatus = RegCreateKeyEx(HKEY_LOCAL_
MACHINE,
lpTemp,
NULL, TEXT(""),
REG_OPTION_NON_VOLATILE, (KEY_READ|KEY_WRITE),
NULL, (HKEY*)&mhKey, &dwDummy);
delete lpTemp;
if(dwStatus!=ERROR_SUCCESS
)
mhKey = NULL;
}
}
/*************************
**********
**********
**********
**********
**********
** CFRegistry **
**************************
**********
**********
**********
**********
*********/
CFRegistry::CFRegistry(CFR
egistry* pParent, LPTSTR lpName)
{
DWORD dwStatus;
DWORD dwDummy;
dwStatus = RegCreateKeyEx((HKEY)pPare
nt->mhKey,
lpName, NULL, TEXT(""),
REG_OPTION_NON_VOLATILE, (KEY_READ|KEY_WRITE),
NULL, (HKEY*)&mhKey, &dwDummy);
if(dwStatus!=ERROR_SUCCESS
)
mhKey = NULL;
}
/*************************
**********
**********
**********
**********
**********
** ~CFRegistry **
**************************
**********
**********
**********
**********
*********/
CFRegistry::~CFRegistry(vo
id)
{
if(mhKey)
RegCloseKey((HKEY)mhKey);
}
/*************************
**********
**********
**********
**********
**********
** Write **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::Write(LPTSTR lpEntry, DWORD dwValue, LPTSTR lpSection)
{
BOOL bSuccess = TRUE;
if(!mhKey)
bSuccess = FALSE;
else if(lpSection)
{
CFRegistry cfReg(this, lpSection);
bSuccess = cfReg.Write(lpEntry, dwValue, NULL);
}
else if(RegSetValueEx((HKEY)mhK
ey, lpEntry, NULL, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue))!=ERROR_SU
CCESS)
bSuccess = FALSE;
return bSuccess;
}
/*************************
**********
**********
**********
**********
**********
** Read **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::Read(LPTSTR lpEntry, LPDWORD lpdwValue, DWORD dwDefault, LPTSTR lpSection)
{
BOOL bSuccess = TRUE;
BYTE bBuff[4];
DWORD dwType;
DWORD dwSize = sizeof(bBuff);
if(!mhKey)
{
*lpdwValue = dwDefault;
bSuccess = FALSE;
}
else if(lpSection)
{
CFRegistry cfReg(this, lpSection);
bSuccess = cfReg.Read(lpEntry, lpdwValue, dwDefault, NULL);
}
else if(RegQueryValueEx((HKEY)m
hKey, lpEntry, NULL, &dwType, bBuff, &dwSize)!=ERROR_SUCCESS)
*lpdwValue = dwDefault;
else if(dwType==REG_DWORD)
*lpdwValue = *(LPDWORD)&bBuff;
else
bSuccess = FALSE;
return bSuccess;
}
/*************************
**********
**********
**********
**********
**********
** Write **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::Write(LPTSTR lpEntry, WORD wValue, LPTSTR lpSection)
{
BOOL bSuccess = TRUE;
if(!mhKey)
bSuccess = FALSE;
else if(lpSection)
{
CFRegistry cfReg(this, lpSection);
bSuccess = cfReg.Write(lpEntry, wValue, NULL);
}
else if(RegSetValueEx((HKEY)mhK
ey, lpEntry, NULL, REG_DWORD, (LPBYTE)&wValue, sizeof(wValue))!=ERROR_SUC
CESS)
bSuccess = FALSE;
return bSuccess;
}
/*************************
**********
**********
**********
**********
**********
** Read **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::Read(LPTSTR lpEntry, LPWORD lpwValue, WORD wDefault, LPTSTR lpSection)
{
BOOL bSuccess = TRUE;
BYTE bBuff[4];
DWORD dwType;
DWORD dwSize = sizeof(bBuff);
if(!mhKey)
{
*lpwValue = wDefault;
bSuccess = FALSE;
}
else if(lpSection)
{
CFRegistry cfReg(this, lpSection);
bSuccess = cfReg.Read(lpEntry, lpwValue, wDefault, NULL);
}
else if(RegQueryValueEx((HKEY)m
hKey, lpEntry, NULL, &dwType, bBuff, &dwSize)!=ERROR_SUCCESS)
*lpwValue = wDefault;
else if(dwType==REG_DWORD)
*lpwValue = (WORD)(*(LPDWORD)&bBuff);
else
bSuccess = FALSE;
return bSuccess;
}
/*************************
**********
**********
**********
**********
**********
** Write **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::Write(LPTSTR lpEntry, BOOL bValue, LPTSTR lpSection)
{
return CFRegistry::Write(lpEntry,
(bValue?1:0), lpSection);
}
/*************************
**********
**********
**********
**********
**********
** Read **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::Read(LPTSTR lpEntry, BOOL* lpbValue, BOOL bDefault, LPTSTR lpSection)
{
BOOL bSuccess;
DWORD dwValue = 0;
bSuccess = CFRegistry::Read(lpEntry, &dwValue, (bDefault?1:0), lpSection);
if(bSuccess)
*lpbValue = (dwValue!=0);
return bSuccess;
}
/*************************
**********
**********
**********
**********
**********
** Write **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::Write(LPTSTR lpEntry, LPTSTR lpValue, LPTSTR lpSection)
{
BOOL bSuccess = TRUE;
if(!mhKey)
bSuccess = FALSE;
else if(lpSection)
{
CFRegistry cfReg(this, lpSection);
bSuccess = cfReg.Write(lpEntry, lpValue, NULL);
}
else if(RegSetValueEx((HKEY)mhK
ey, lpEntry, NULL, REG_SZ, (LPBYTE)lpValue, lstrlen(lpValue))!=ERROR_S
UCCESS)
bSuccess = FALSE;
return bSuccess;
}
/*************************
**********
**********
**********
**********
**********
** Read **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::Read(LPTSTR lpEntry, DWORD dwStringBuffSize, LPTSTR lpValue, LPTSTR lpDefault, LPTSTR lpSection)
{
BOOL bSuccess = TRUE;
BYTE bBuff[256];
DWORD dwType;
DWORD dwSize = sizeof(bBuff);
lpValue[0] = 0;
if(!mhKey)
{
CopyString(lpValue, dwStringBuffSize, lpDefault);
bSuccess = FALSE;
}
else if(lpSection)
{
CFRegistry cfReg(this, lpSection);
bSuccess = cfReg.Read(lpEntry, dwStringBuffSize, lpValue, lpDefault, NULL);
}
else if(RegQueryValueEx((HKEY)m
hKey, lpEntry, NULL, &dwType, bBuff, &dwSize)!=ERROR_SUCCESS)
CopyString(lpValue, dwStringBuffSize, lpDefault);
else if(dwType==REG_SZ)
CopyString(lpValue, dwStringBuffSize, (LPTSTR)bBuff);
else
bSuccess = FALSE;
lpValue[dwStringBuffSize-1
] = 0;
return bSuccess;
}
/*************************
**********
**********
**********
**********
**********
** CopyString **
**************************
**********
**********
**********
**********
*********/
BOOL CFRegistry::CopyString(LPT
STR lpDest, DWORD dwDestSize, LPTSTR lpSrc)
{
BOOL bSuccess = TRUE;
if((lpDest==NULL) || (lpSrc==NULL) || (dwDestSize<=sizeof(TCHAR)
))
bSuccess = FALSE;
else
{
DWORD dwSrcSize = (lstrlen(lpSrc)*sizeof(TCH
AR))+sizeo
f(TCHAR);
DWORD dwToCopy = dwSrcSize;
if(dwToCopy>=dwDestSize)
dwToCopy = dwDestSize - sizeof(TCHAR);
memcpy(lpDest, lpSrc, dwToCopy);
lpDest[dwToCopy] = 0;
}
return bSuccess;
}
/* eof */
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
When I do rebuild all, it gives the following error
d:\jags\find ant\test_registry\test_reg
istry.cpp(
12) : warning C4700: local variable 'dw' used without having been initialized
Linking...
Test_registry.obj : error LNK2001: unresolved external symbol "public: int __thiscall CFRegistry::Read(char *,unsigned long *,unsigned long,char *)" (?Read@CFRegistry@@QAEHPAD
PAKK0@Z)
Test_registry.obj : error LNK2001: unresolved external symbol "public: int __thiscall CFRegistry::Write(char *,char *,char *)" (?Write@CFRegistry@@QAEHPA
D00@Z)
Test_registry.obj : error LNK2001: unresolved external symbol "public: __thiscall CFRegistry::CFRegistry(cha
r *,char *,char *)" (??0CFRegistry@@QAE@PAD00@
Z)
Debug/Test_registry.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
The object files test_registry.obj, CFRegistry.obj, Stdafx.obj are created after the compilation
How Can I get out of this?
Any help is admired
Thanks,
Duragaprasad
Start Free Trial