c++读取注册表详解

来源:网络时间:2012-04-05

  1.读注册表

  // 读取注册表

  CString ReadRegisteTable(CString root,CString path, CString key)

  {

  HKEY hAppKey;

  LPCTSTR WINDS_SERVICE_REGISTRY_KEY=path;

  LPCTSTR DATA_FILE_SUB_KEY=key;

  char szDataFile[80];

  if(root=="HKEY_LOCAL_MACHINE")

  {

  if (ERROR_SUCCESS == RegOpenKeyEx (

  HKEY_LOCAL_MACHINE,

  WINDS_SERVICE_REGISTRY_KEY,

  0,

  KEY_READ,

  &hAppKey))

  {

  ULONG cbSize = MAX_PATH*sizeof(TCHAR);

  DWORD dwFlag = RegQueryValueEx (

  hAppKey,

  DATA_FILE_SUB_KEY,

  NULL,

  NULL,

  (LPBYTE)szDataFile,&cbSize);

  RegCloseKey (hAppKey);

  if (ERROR_SUCCESS == dwFlag)

  {

  CString strDate=szDataFile; //MessageBox(strDate);

  if(strDate.GetLength()>0) return strDate;

  else return "";

  }

  return "";

  }

  }

  if(root=="HKEY_CURRENT_USER")

  {

  if (ERROR_SUCCESS == RegOpenKeyEx (

  HKEY_CURRENT_USER,

  WINDS_SERVICE_REGISTRY_KEY,

  

  0,

  KEY_READ,

  &hAppKey))

  {

  ULONG cbSize = MAX_PATH*sizeof(TCHAR);

  DWORD dwFlag = RegQueryValueEx (

  hAppKey,

  DATA_FILE_SUB_KEY,

  NULL,

  NULL,

  (LPBYTE)szDataFile,&cbSize);

  RegCloseKey (hAppKey);

  if (ERROR_SUCCESS == dwFlag)

  {

  CString strDate=szDataFile; //MessageBox(strDate);

  if(strDate.GetLength()>0) return strDate;

  else return "";

  }

  return "";

  }

  }

  return "";

  }[nextqage]

  2.写注册表

  // 修改注册表

  BOOL ModifyRegisteTable(CString root,CString path, CString key, CString value)

  {

  HKEY hAppKey;

  DWORD dwDisposition = MAX_PATH*sizeof(TCHAR);

  LPCTSTR WINDS_SERVICE_REGISTRY_KEY=path;

  LPCTSTR DATA_FILE_SUB_KEY=key;

  char szDataFile[80]; strcpy(szDataFile,value);

  if(root=="HKEY_LOCAL_MACHINE")

  {

  if (ERROR_SUCCESS != RegCreateKeyEx (

  HKEY_LOCAL_MACHINE,

  WINDS_SERVICE_REGISTRY_KEY,

  0,

  NULL,

  REG_OPTION_NON_VOLATILE,

  KEY_WRITE,

  NULL,

  &hAppKey,

  &dwDisposition))

  {

  return false;

  }

  else

  {

  if (ERROR_SUCCESS != RegSetValueEx (hAppKey,

  DATA_FILE_SUB_KEY,

  0,

  REG_SZ,

  (LPBYTE)szDataFile,

  (lstrlen (szDataFile)+1)*sizeof(TCHAR)))

  {

  return false;

  }

  RegCloseKey (hAppKey);

  }

  }

  if(root=="HKEY_CURRENT_USER")

  {

  if (ERROR_SUCCESS != RegCreateKeyEx (

  HKEY_CURRENT_USER,

  WINDS_SERVICE_REGISTRY_KEY,

  0,

  NULL,

  REG_OPTION_NON_VOLATILE,

  KEY_WRITE,

  NULL,

  &hAppKey,

  &dwDisposition))

  {

  return false;

  }

  else

  {

  if (ERROR_SUCCESS != RegSetValueEx (hAppKey,

  DATA_FILE_SUB_KEY,

  0,

  REG_SZ,

  (LPBYTE)szDataFile,

  (lstrlen (szDataFile)+1)*sizeof(TCHAR)))

  {

  return false;

  }

  RegCloseKey (hAppKey);

  }

  }

  return true;

  }

  

发表评论

最新评论(共2条)

2345市网友

看到这样的代码格式,就不想看了,一垃圾

2013-07-05 0

回复@2345网友:

  • 取消

2345市网友

是不是?是不是?C++要英语很棒的的基本上就没有困难  对不?

2012-06-16 1

回复@2345网友:

  • 取消