萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> c++讀取注冊表詳解

c++讀取注冊表詳解

   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;

  }

copyright © 萬盛學電腦網 all rights reserved