c++讀取註冊表詳解

來源:互聯網
上載者:User

   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;

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.