windows 写读注册表 示例

来源:互联网 发布:网络电视剧十大排行榜 编辑:程序博客网 时间:2024/06/05 16:19

//////////////////////////////////////////////////////////////////////////////////////

///  把一个可执行程序的路径写入注册表,并读取出来。

///////////////////////////////////////////////////////////////////////////////////////


#include <Windows.h>
#include <WinReg.h>
#include <stdio.h>
#include <string>

int main(int argc, char *argv[])
{
 std::string path = "d:\\cfcheng\\ats.exe";

 long ret = 0;
 HKEY hk;
 DWORD dwData, dwDisp;
 TCHAR szBuf[MAX_PATH];

 if (ERROR_SUCCESS != RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\ATS",
  0, NULL, REG_OPTION_NON_VOLATILE,
  KEY_SET_VALUE, NULL, &hk, &dwDisp))
 {
  printf("Could not create the registry key.");
  return FALSE;
 }

 // Set the name of the message file.

 if (ERROR_SUCCESS != RegSetValueEx(hk,              // subkey handle
  "path",        // value name
  0,                         // must be zero
  REG_EXPAND_SZ,             // value type
  (LPBYTE)path.c_str(),        // pointer to value data
  (DWORD) path.length() + 1)) // length of value data
 {
  printf("Could not set the event message file.");
  RegCloseKey(hk);
  return FALSE;
 }

 char value[1024];
 HKEY hkey;
 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\ATS", 0, KEY_ALL_ACCESS, &hkey);
 if( ret != ERROR_SUCCESS )
 {
  printf("open key fail.\n");
  return -1;
 }

 DWORD size = sizeof(value);
 ret = RegQueryValueEx(hkey, "path", NULL, NULL, (LPBYTE)value, &size);
 if(ret == ERROR_SUCCESS )
 {
  printf("path value :%s\n", value);
 }
 else
  printf("query path value fail.\n");


 

 return 0;
};

原创粉丝点击