读写注册表

12 篇文章 0 订阅

int get_app_path(char *path)
 {
     char buff[MAX_PATH];
 
     memset(buff, 0, sizeof(buff));
     if (GetModuleFileName(NULL, buff, MAX_PATH) == 0)
     {
         return -1;
     }
     memcpy(path, buff, strlen(buff));
 
     return 0;
 }
 
  int reg_ora_client(const char *path)
 {
     HKEY hreg;
     char dir[MAX_PATH];
     char paths[4096];
     unsigned long size, len;
 
     if (path == NULL || (len = strlen(path)) < 2)
     {
         return -1;
     }
 
     len --;
     memset(dir, 0, sizeof(dir));
     strcpy(dir, path);
 
     while (dir[len] == '\\')
     {
         dir[len --] = 0;
     }
 
     if(RegOpenKey(HKEY_LOCAL_MACHINE, \
                   "SYSTEM\\ControlSet001\\Control\\Session Manager\\Environment", \
                   &hreg) != ERROR_SUCCESS)
     {
         fprintf(stderr, "设置ORACLE环境 --- 无法打开键!\n");
         return -1;
     }
 
     if (RegSetValueEx(hreg, "TNS_ADMIN", 0, REG_SZ, \
                       (const unsigned char *)dir, strlen(dir)) != ERROR_SUCCESS)
     {
         fprintf(stderr, "设置ORACLE环境 --- 无法设置TNS_ADMIN!\n");
         RegCloseKey(hreg);
         return -1;
     }
 
     size = 4096;
     memset(paths, 0, sizeof(paths));
 
     if (RegQueryValueEx(hreg, "PATH", NULL, NULL, (unsigned char *)paths, &size) != ERROR_SUCCESS)
     {
         RegCloseKey(hreg);
         return -1;
     }
 
     if (stristr(paths, dir) == NULL)
     {
         len = strlen(paths);
 
         if (paths[-- len] != ';')
         {
             strcat(paths, ";");
         }
 
         strcat(paths, dir);
 
         if (RegSetValueEx(hreg, "PATH", 0, REG_EXPAND_SZ, \
                           (const unsigned char *)paths, strlen(paths)) != ERROR_SUCCESS)
         {
             fprintf(stderr, "设置ORACLE环境 --- 无法设置PATH!\n");
             RegCloseKey(hreg);
             return -1;
         }
     }
 
     if (RegSetValueEx(hreg, "NLS_LANG", 0, REG_SZ, \
                       (const unsigned char *)"SIMPLIFIED CHINESE_CHINA.ZHS16GBK", 33) != ERROR_SUCCESS)
     {
         fprintf(stderr, "设置ORACLE环境 --- 无法设置NLS_LANG!\n");
         RegCloseKey(hreg);
         return -1;
     }
 
     RegCloseKey(hreg);
     return 0;
 }
 
 
  int rdb_init()
 {
     char dir[MAX_PATH];
     char *p;
 
     memset(dir, 0, sizeof(dir));
 
     if (get_app_path(dir) == -1)
     {
         return -1;
     }
 
     if ((p = strrchr(dir, '\\')) == NULL)
     {
         return -1;
     }
 
     memset(p, 0, sizeof(dir) - (p - dir));
 
     if (reg_ora_client(dir) == -1)
     {
         return -1;
     }
 
     return 0;
 }
更多
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用微软提供的CReg类可以方便地读写注册表,具体用法如下: ```cpp #include <creg.hxx> // 读取注册表 CRegKey reg; if (reg.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"), KEY_READ) == ERROR_SUCCESS) { DWORD dwType = REG_SZ; TCHAR szValue[1024] = { 0 }; ULONG ulCount = 1024; if (reg.QueryStringValue(_T("ProgramFilesDir"), szValue, &ulCount) == ERROR_SUCCESS) { // 读取成功 // szValue中存储了ProgramFilesDir的值 } reg.Close(); } // 写入注册表 CRegKey reg; if (reg.Create(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\MyCompany\\MyApp")) == ERROR_SUCCESS) { if (reg.SetStringValue(_T("MyValue"), _T("Hello, World!")) == ERROR_SUCCESS) { // 写入成功 } reg.Close(); } ``` 另外,在VB中,可以使用自身的SaveSetting、GetSetting函数读写注册表,也可以使用API函数来实现注册表任意读写。具体用法可以参考以下代码: ```vb ' 读取注册表 Dim hKey As Long Dim dwType As Long Dim dwSize As Long Dim sValue As String hKey = OpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", KEY_READ) If hKey <> 0 Then dwType = REG_SZ sValue = String$(1024, 0) dwSize = Len(sValue) If QueryValueEx(hKey, "ProgramFilesDir", 0, dwType, ByVal sValue, dwSize) = ERROR_SUCCESS Then ' 读取成功 ' sValue中存储了ProgramFilesDir的值 End If RegCloseKey hKey End If ' 写入注册表 hKey = CreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\MyCompany\MyApp") If hKey <> 0 Then If SetValueEx(hKey, "MyValue", 0, REG_SZ, "Hello, World!", Len("Hello, World!")) = ERROR_SUCCESS Then ' 写入成功 End If RegCloseKey hKey End If ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值