(原)保存ini文件,注册表

.h

 /*******************************************************
 模块名:   CParams
 模块描述:
 做成者: 
tsys2000@gmail.com
 做成时间: 2005/11/08
 *******************************************************/

 class CParams
 {
 public:
  CParams(void);
  ~CParams(void);
  static string* GetExecutePath(void);
 protected:
  string path;
 public:
  void InitialPath(void);
 };

 /*******************************************************
 模块名:   CParams_ini_api
 模块描述: 利用api保存ini文件
 做成者:
tsys2000@gmail.com
 做成时间: 2005/11/08
 *******************************************************/

 class CParams_ini_api :
  public CParams
 {
 public:
  CParams_ini_api(void);
  ~CParams_ini_api(void);
  void SetPath(LPCTSTR dir, LPCTSTR file);
  void SetPath(LPCTSTR dirfile);
  BOOL IsHavePath();
  void WriteStruct(LPCTSTR Section, LPCTSTR key, BYTE* pstruct, int size);
  void ReadStruct(LPCTSTR Section, LPCTSTR key, BYTE* pstruct, int size);
  void WriteStr(LPCTSTR Section, LPCTSTR key, LPCTSTR str);
  void ReadStr(LPCTSTR Section, LPCTSTR key, LPTSTR str,int size);
  void WriteInt(LPCTSTR Section, LPCTSTR key, int i);
  void ReadInt(LPCTSTR Section, LPCTSTR key, int *i);
  void WriteDouble(LPCTSTR Section, LPCTSTR key,double d);
  void ReadDouble(LPCTSTR Section, LPCTSTR key,double *d);
 };

 /*******************************************************
 模块名:   CParams_reg
 模块描述:
 做成者:
tsys2000@gmail.com
 做成时间: 2005/11/08
 *******************************************************/

 class CParams_reg
 {
 public:
  CParams_reg(void);
  CParams_reg(LPCTSTR execute_name);
  ~CParams_reg(void);
  void InitialExeName(LPCTSTR execute_name);
 protected:
  string path;
  enum {reg_string = 1,reg_int,reg_double,reg_byte};
  BOOL Set(LPCTSTR dir,int keytype,LPCTSTR key,BYTE* value,int szie);
  BOOL Get(LPCTSTR dir,int keytype,LPCTSTR key,BYTE* value,int *szie
);
 public: 
  void SetInt(LPCTSTR dir,LPCTSTR key,int i);
  void SetDouble(LPCTSTR dir,LPCTSTR key,double d);
  void SetString(LPCTSTR dir,LPCTSTR key,LPCTSTR str);
  void SetBytes(LPCTSTR dir,LPCTSTR key,void* bytes,int size); 

  void GetInt(LPCTSTR dir,LPCTSTR key,int *i);
  void GetDouble(LPCTSTR dir,LPCTSTR key,double *d);
  void GetString(LPCTSTR dir,LPCTSTR key,LPTSTR str);
  void GetBytes(LPCTSTR dir,LPCTSTR key,void* bytes,int size);
 };

.cpp

 /*******************************************************
 模块名:   CParams
 模块描述:
 做成者:
tsys2000@gmail.com
 做成时间: 2005/11/08
 *******************************************************/

 CParams::CParams(void)
 {
 }

 CParams::~CParams(void)
 {
 }

 /*CParams pms;
 string *pstr = pms.GetExecutePath();
 *pstr += "/n";
 OutputDebugString(pstr->c_str());
 delete pstr;*/
 string* CParams::GetExecutePath(void)
 { 
  // 返回h:/Visual Studio Projects/Dzg/Dzg/debug/ ;
  TCHAR sz[MAX_PATH];
  memset(sz,0,MAX_PATH);
  GetModuleFileName(  GetModuleHandle(NULL),sz,MAX_PATH  ); 
  string str(sz);
  int pos = str.rfind(_T("//"));
  pos++;
  /*
  stringstream ss;
  ss<<pos;
  OutputDebugString(ss.str().c_str());
  */
  string *pstr = new string(str.substr(0,pos));
  return pstr;
 }

 void CParams::InitialPath(void)
 {
  path = * GetExecutePath();
 }


 /*******************************************************
 模块名:   CParams_ini_api
 模块描述: 利用api保存ini文件
 做成者:
tsys2000@gmail.com
 做成时间: 2005/11/08
 *******************************************************/

 CParams_ini_api::CParams_ini_api(void)
 {
 }

 CParams_ini_api::~CParams_ini_api(void)
 {
 }

 void CParams_ini_api::SetPath(LPCTSTR dir, LPCTSTR file)
 {
  InitialPath();
  if(dir == NULL)
  {
   path += file;
  }
  else
  { 
   path = path + dir + _T("//") + file;
  }
 }

 void CParams_ini_api::SetPath(LPCTSTR dirfile)
 {
  path = dirfile;
 }

 BOOL CParams_ini_api::IsHavePath()
 {
  DWORD dwAttr = GetFileAttributes(path.c_str());
  if(dwAttr == (DWORD) -1)
  {
   return FALSE;
  }
  return TRUE;
 }

 /*
 //[sectoin]
 //key=0A000000140000001E
 //[section]
 //key2=大家好
 //key3=120
 //key4=1904560E2D12604060
 struct aaa
 {
 int a;
 int b;
 } mya;
 mya.a = 10;
 mya.b = 20;
 CParams_ini_api pmsini;
 pmsini.SetPath(NULL,"aa.txt");
 //struct
 pmsini.WriteStruct("sectoin","key",(BYTE*)&mya,sizeof(mya));
 //string
 pmsini.WriteStr("section","key2","大家好");
 //int
 pmsini.WriteInt("section","key3",120);
 //double
 pmsini.WriteDouble("section","key4",128.568);
 */
 void CParams_ini_api::WriteStruct(LPCTSTR Section, LPCTSTR key, BYTE* pstruct, int size)
 {
  WritePrivateProfileStruct(Section,key,pstruct,size,path.c_str());
 }

 /*
 struct aaa
 {
 struct aaa
 {
 int a;
 int b;
 } mya;
 memset(&mya,0,sizeof(mya));
 CParams_ini_api pmsini;
 pmsini.SetPath(NULL,"aa.txt");
 //struct
 pmsini.ReadStruct("sectoin","key",(BYTE*)&mya,sizeof(mya));
 stringstream ss;
 ss << mya.a <<'-'<< mya.b<<endl;
 OutputDebugString(ss.str().c_str());
 //string
 TCHAR sz[MAX_PATH] = "拉拉";   
 pmsini.ReadStr("section","key2",sz,MAX_PATH );
 ss.str("");
 ss << sz<<endl;
 OutputDebugString(ss.str().c_str());
 //int
 int i = 0;
 pmsini.ReadInt("section","key3",&i);
 ss.str("");
 ss << i<<endl;
 OutputDebugString(ss.str().c_str());
 //double
 double d = 0;
 pmsini.ReadDouble("section","key4",&d);
 ss.str("");
 ss << d<<endl;
 OutputDebugString(ss.str().c_str());
 */
 void CParams_ini_api::ReadStruct(LPCTSTR Section, LPCTSTR key, BYTE* pstruct, int size)
 {
  GetPrivateProfileStruct(Section,key,pstruct,size,path.c_str());
 }

 void CParams_ini_api::WriteStr(LPCTSTR Section, LPCTSTR key, LPCTSTR str)
 {
  WritePrivateProfileString(Section,key,str,path.c_str());
 }
 void CParams_ini_api::ReadStr(LPCTSTR Section, LPCTSTR key, LPTSTR str,int size)
 {
  string str2(str);
  GetPrivateProfileString(Section,key,str2.c_str(),str,size,path.c_str());
 }
 void CParams_ini_api::WriteInt(LPCTSTR Section, LPCTSTR key, int i)
 {
  stringstream ss;
  ss<<i;
  WritePrivateProfileString(Section,key,ss.str().c_str(),path.c_str());
 }
 void CParams_ini_api::ReadInt(LPCTSTR Section, LPCTSTR key, int *i)
 {
  *i = GetPrivateProfileInt(Section,key,*i,path.c_str());
 }

 void CParams_ini_api::WriteDouble(LPCTSTR Section, LPCTSTR key,double d)
 {
  WritePrivateProfileStruct(Section,key,(BYTE*) &d,sizeof(double),path.c_str());

 }

 void CParams_ini_api::ReadDouble(LPCTSTR Section, LPCTSTR key,double *d)
 {
  GetPrivateProfileStruct(Section,key,(BYTE*) d,sizeof(double),path.c_str());
 }


 /*******************************************************
 模块名:   CParams_reg
 模块描述:
 做成者:
tsys2000@gmail.com
 做成时间: 2005/11/08
 *******************************************************/

 CParams_reg::CParams_reg(void)
 {
 }

 CParams_reg::~CParams_reg(void)
 {
 }

 CParams_reg::CParams_reg(LPCTSTR execute_name)
 {
  InitialExeName(execute_name);
 }

 void CParams_reg::InitialExeName(LPCTSTR execute_name)
 {
  path.clear();
  path = "SOFTWARE//YsSoft//";
  path += execute_name;
  path += "//";
 }

 /* EG 1
 CParams_reg reg("电子柜");
 string str("你好");
 reg.Set(NULL,1,"字符串",(BYTE*)str.c_str(),str.size());
 int i = 1250;
 reg.Set(NULL,2,"int",(BYTE*) &i ,sizeof(int));
 double d = 125.864;
 reg.Set(NULL,3,"double",(BYTE*) &d,sizeof(double));
 struct aaa
 {
 int a;
 int b;
 } mya;
 mya.a = 10;
 mya.b = 20;
 reg.Set(NULL,4,"bytes",(BYTE*)&mya,sizeof(aaa));
 */
 BOOL CParams_reg::Set(LPCTSTR dir,int keytype,LPCTSTR key,BYTE* value,int szie)
 {
  HKEY   hKey;
  DWORD  dwDisposition;
  LONG   lRetCode;
  string str(path);
  if(dir != NULL)
   str += dir;
  lRetCode = RegCreateKeyEx ( HKEY_LOCAL_MACHINE, str.c_str(),
   0,  NULL,REG_OPTION_NON_VOLATILE,  KEY_WRITE,
   NULL, &hKey,  &dwDisposition);
  if (lRetCode != ERROR_SUCCESS)
  {
   return FALSE;
  }

  int type = 0;
  switch(keytype)
  {
  case reg_string:
   type = REG_SZ;
   break;
  case reg_int:
   type = REG_DWORD;
   break;
  case reg_double:
   type = REG_BINARY;
   break;
  case reg_byte:
   type = REG_BINARY;
   break;
  default:
   RegCloseKey( hKey );
   return FALSE;
  }

  lRetCode = RegSetValueEx ( hKey, key,  0,  type, value, szie);
  RegCloseKey( hKey );
  if (lRetCode != ERROR_SUCCESS)   
   return FALSE;
  return TRUE;

 }

 /*
 CParams_reg reg("电子柜");
 reg.SetInt(NULL,"int",1259);
 reg.SetDouble(NULL,"double",125.235);
 reg.SetString(NULL,"string","我的上帝");
 struct
 {
 double a;
 int b;
 } data;
 data.a = 568.4;
 data.b = 1258;
 reg.SetBytes(NULL,"bytes",&data,sizeof(data));
 */
 void CParams_reg::SetInt(LPCTSTR dir,LPCTSTR key,int i)
 {
  Set(dir,reg_int,key,(BYTE*) &i ,sizeof(int));
 }
 void CParams_reg::SetDouble(LPCTSTR dir,LPCTSTR key,double d)
 {
  Set(dir,reg_double,key,(BYTE*) &d,sizeof(double));
 }
 void CParams_reg::SetString(LPCTSTR dir,LPCTSTR key,LPCTSTR str)
 {
  string str2(str);
  Set(dir,reg_string,key,(BYTE*) str,str2.size());
 }
 void CParams_reg::SetBytes(LPCTSTR dir,LPCTSTR key,void* bytes,int size)
 {
  Set(dir,reg_byte,key,(BYTE*)bytes,size);
 }


 /*
 CParams_reg reg("电子柜");   
 //int
 int i = 0;
 int size = sizeof(int);
 reg.Get(NULL,2,"int",(BYTE*)&i,&size);
 stringstream ss;
 ss << i <<endl;
 OutputDebugString(ss.str().c_str());
 //double
 double d = 0;
 size = sizeof(double);
 reg.Get(NULL,3,"double",(BYTE*)&d,&size);
 ss.str("");
 ss << d <<endl;
 OutputDebugString(ss.str().c_str());
 //string
 TCHAR sz[MAX_PATH] = {0};
 size = MAX_PATH;
 reg.Get(NULL,1,"string",(BYTE*)&sz,&size);
 ss.str("");
 ss << sz <<endl;
 OutputDebugString(ss.str().c_str());
 //bytes
 struct
 {
 double a;
 int b;
 } data;
 memset(&data,0,sizeof(data));
 size = sizeof(data);
 reg.Get(NULL,1,"bytes",(BYTE*)&data,&size);
 ss.str("");
 ss << data.a <<'-'<<data.b <<endl;
 OutputDebugString(ss.str().c_str());
 */

 BOOL CParams_reg::Get(LPCTSTR dir,int keytype,LPCTSTR key,BYTE* value,int *szie)
 {
  HKEY   hKey;
  LONG   lRetCode;
  string str(path);
  if(dir != NULL)
   str += dir;
  lRetCode = RegOpenKeyEx ( HKEY_LOCAL_MACHINE, str.c_str(),
   REG_OPTION_NON_VOLATILE,  KEY_READ, &hKey);
  if (lRetCode != ERROR_SUCCESS)
  {
   return FALSE;
  }

  DWORD type = 0;
  switch(keytype)
  {
  case reg_string:
   type = REG_SZ;
   break;
  case reg_int:
   type = REG_DWORD;
   break;
  case reg_double:
   type = REG_BINARY;
   break;
  case reg_byte:
   type = REG_BINARY;
   break;
  default:
   RegCloseKey( hKey );
   return FALSE;
  }

  lRetCode = RegQueryValueEx ( hKey, key,  NULL,(LPDWORD) &type, value,(LPDWORD) szie);
  RegCloseKey( hKey );
  if (lRetCode != ERROR_SUCCESS)   
   return FALSE;
  return TRUE;
 }

 /*
 CParams_reg reg("电子柜");   
 //int
 int i = 0;  
 reg.GetInt(NULL,"int",&i);
 stringstream ss;
 ss << i <<endl;
 OutputDebugString(ss.str().c_str());
 //double
 double d = 0;
 reg.GetDouble(NULL,"double",&d);
 ss.str("");
 ss << d <<endl;
 OutputDebugString(ss.str().c_str());
 //string
 TCHAR sz[MAX_PATH] = {0};  
 reg.GetString(NULL,"string",sz);
 ss.str("");
 ss << sz <<endl;
 OutputDebugString(ss.str().c_str());
 //bytes
 struct
 {
 double a;
 int b;
 } data;
 memset(&data,0,sizeof(data));   
 reg.GetBytes(NULL,"bytes",&data,sizeof(data));
 ss.str("");
 ss << data.a <<'-'<<data.b <<endl;
 OutputDebugString(ss.str().c_str());
 */
 void CParams_reg::GetInt(LPCTSTR dir,LPCTSTR key,int *i)
 {
  int size = sizeof(int);
  Get(dir,reg_int,key,(BYTE*) i,&size);
 }
 void CParams_reg::GetDouble(LPCTSTR dir,LPCTSTR key,double *d)
 {
  int size = sizeof(double);
  Get(dir,reg_double,key,(BYTE*) d,&size);
 }
 void CParams_reg::GetString(LPCTSTR dir,LPCTSTR key,LPTSTR str)
 {
  int size = MAX_PATH;
  Get(dir,reg_string,key,(BYTE*) str,&size);
 }
 void CParams_reg::GetBytes(LPCTSTR dir,LPCTSTR key,void* bytes,int size)
 {
  Get(dir,reg_byte,key,(BYTE*) bytes,&size);
 }

 转载请注明 tsys2000@gmail.com 

阅读更多
上一篇(原)win32 计时
下一篇(原)异常处理,调试辅助
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭