zoukankan      html  css  js  c++  java
  • 7、注册表读写的一个例子

    注册表读写的一个例子
    #include "stdafx.h"
    #include
    "iostream"
    #include
    "string"
    #include
    "Windows.h"

    //注册表项
    #ifndef TEST_APP_REG
    #define TEST_APP_REG "Software\\MySoft\\Common\\InstallRoot"
    #endif

    //注册表中虚拟磁盘映射的盘符键
    #ifndef TEST_APP_REG_DRIVE
    #define TEST_APP_REG_DRIVE "Driver"
    #endif

    #ifndef RETURN_OK
    #define RETURN_OK 1
    #endif

    #ifndef RETURN_ERROR
    #define RETURN_ERROR 0
    #endif

    using namespace std;

    int GetRegStringValue(const wstring &wstrPath, const wstring &wstrName, wstring &wstrValue)
    {
    HKEY hkey;
    LONG lErrCode;
    DWORD dwPathSize
    = 512;
    BYTE bPath[
    512] = {0};
    lErrCode
    = RegOpenKeyEx(HKEY_LOCAL_MACHINE ,
    (LPCWSTR)(wstrPath.c_str()),
    NULL,
    KEY_QUERY_VALUE,
    &hkey);
    if (lErrCode != ERROR_SUCCESS)
    {
    return lErrCode;
    }

    lErrCode
    = RegQueryValueEx(hkey, (LPCWSTR)(wstrName.c_str()), NULL, NULL,
    (LPBYTE)bPath,
    &dwPathSize);
    if (ERROR_SUCCESS == lErrCode)
    {
    //lint -save -e534
    wstrValue.append((wchar_t *) bPath);
    //lint -restore
    }
    RegCloseKey(hkey);
    return lErrCode;
    }

    int CreateRegForSoftward()
    {
    LPCWSTR lpSubKey
    = L"Software\\MySoft\\Common\\InstallRoot";
    LPCWSTR lpSubKeyValue
    = L"Driver";
    CONST BYTE lpData[
    512] = {"Y"};

    HKEY hMyKey, hMyKey1;

    LONG lRet
    = RegCreateKeyEx(HKEY_LOCAL_MACHINE,lpSubKey,NULL,NULL,REG_OPTION_NON_VOLATILE,
    KEY_ALL_ACCESS,NULL,
    &hMyKey,NULL);
    if (ERROR_SUCCESS == lRet)
    {
    lRet
    = RegSetValueEx(hMyKey,lpSubKeyValue,NULL,REG_SZ,lpData,4);
    if (lRet == ERROR_SUCCESS)
    {
    RegCloseKey(hMyKey);
    return RETURN_OK;
    }
    }

    RegCloseKey(hMyKey);
    return RETURN_ERROR;
    }

    int main()
    {
    wstring wstrDrive, wstrPath
    = L"Y:\WINDDK";

    CreateRegForSoftward();

    if (0 != GetRegStringValue(TEXT(TEST_APP_REG), TEXT(TEST_APP_REG_DRIVE), wstrDrive))
    {
    return S_FALSE;
    }

    if (wstrPath.empty() || wstrDrive.empty())
    {
    return S_FALSE;
    }

    std::cout
    << _wcsnicmp(wstrPath.c_str(), wstrDrive.c_str(), 1);

    }

  • 相关阅读:
    备战考研算法笔记(四)快速排序
    VMware安装
    使用 Vagrant 打造跨平台开发环境
    类加载器,注解,动态代理
    IE兼容forEach/map/every/some等新方法
    不得不看的Java代码性能优化总结
    关于Oracle误操作--数据被Commit后的数据回退(闪回)
    JPA与Hibernate的关系
    Jenkins-GitHub-Gradle自动构建项目
    过滤器Filter
  • 原文地址:https://www.cnblogs.com/mydomain/p/1905030.html
Copyright © 2011-2022 走看看