• MFC操作注册表


    使用CWinApp类的成员函数

    1. 在App类中的InitInstance()中
        // TODO:  应适当修改该字符串,
        // 例如修改为公司或组织名
        SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
    //在注册表中的位置是 HKEY_CURRENT_USERSoftware应用程序向导生成的本地应用程序<application name>

    用VC++的向导建立MFC项目之后,在InitInstance中可以看到这样的语句:
    SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
    该函数将为以上提到的几个方法建立工作环境,此时如果用WriteProfileInt写入数据,将会
    被写入到如下注册表位置:
    HKEY_CURRENT_USERSoftware应用程序向导生成的本地应用程序应用程序名称
    
    如果在InitInstance中不执行SetRegistryKey函数,则用WriteProfileInt写入数据时,将写入到
    %windir%应用程序名称.ini中。即 C:Windows/<application>.ini
    ————————————————
    原文链接:https://blog.csdn.net/hczhiyue/article/details/8179923


    2.在其他地方,像Dialog类中
    BOOL CtestDialogDlg::OnInitDialog()
    {
        CWinApp* pApp = AfxGetApp();
        //写注册表
        BOOL bRet = pApp->WriteProfileStringW(_T("section1"), _T("name"), _T("value"));
        bRet = pApp->WriteProfileInt(_T("section1"), _T("numb"), 10);
        POINT pt = { 99, 90 };
        bRet = pApp->WriteProfileBinary(_T("section1"), _T("bin"), (LPBYTE)&pt, sizeof(pt));
        //读注册表
        CString str = pApp->GetProfileStringW(_T("section1"), _T("name"), _T(""));
        int n = pApp->GetProfileIntW(_T("section1"), _T("numb"), -1);
        LPBYTE pData; UINT nBytes;
        bRet = pApp->GetProfileBinary(_T("section1"), _T("bin"), &pData, &nBytes);
        POINT retPt = *(POINT*)(pData);
        delete[] pData;//注意:free
           ....

    }


    使用Windows API函数


    RegSetValueEx
    The RegSetValueEx function sets the data and type of a specified value under a registry key.

    LONG RegSetValueEx(
      HKEY hKey,           // handle to key
      LPCTSTR lpValueName, // value name
      DWORD Reserved,      // reserved
      DWORD dwType,        // value type
      CONST BYTE *lpData,  // value data
      DWORD cbData         // size of value data
    );

    RegQueryValueEx
    The RegQueryValueEx function retrieves the type and data for a specified value name associated with an open registry key.

    LONG RegQueryValueEx(
      HKEY hKey,            // handle to key
      LPCTSTR lpValueName,  // value name
      LPDWORD lpReserved,   // reserved
      LPDWORD lpType,       // type buffer
      LPBYTE lpData,        // data buffer
      LPDWORD lpcbData      // size of data buffer
    );

    RegDeleteKey     Deletes a subkey.
    RegDeleteValue   Removes a named value from the specified registry key.


    例子:
        //使用API函数
        HKEY hKey;
        //Create Key 若无创建,若有打开
        if (ERROR_SUCCESS != ::RegCreateKey(HKEY_CURRENT_USER, _T("Software\应用程序向导生成的本地应用程序\testDialog\section2"), &hKey))
            return FALSE;//失败
        TCHAR buf[] = _T("Harry");
        if (ERROR_SUCCESS != ::RegSetValueEx(hKey, _T("name"), 0, REG_SZ, (const BYTE*)buf, sizeof(buf)))
            return FALSE;//失败
        BYTE data[512] = { 0 };
        DWORD dwBytes = _countof(data);// in/out
        if (0 != ::RegQueryValueEx(hKey, _T("name"), 0, NULL, data, &dwBytes))
            return FALSE;//失败
        CString strValue = (TCHAR*)data;

        ::RegDeleteValue(hKey, _T("name"));//delete name-value


        if (ERROR_SUCCESS != ::RegCreateKey(HKEY_CURRENT_USER, _T("Software\应用程序向导生成的本地应用程序\testDialog"), &hKey))
            return FALSE;//失败
        LSTATUS nRet = ::RegDeleteKey(hKey, _T("section2"));
    //或者
        LSTATUS nRet = ::RegDeleteKey(HKEY_CURRENT_USER, _T("Software\应用程序向导生成的本地应用程序\testDialog\section2"));

    使用MFC的CRegKey类

        //使用CRegKey
        CRegKey regkey;
        //Create Key 若无创建,若有打开
        if (ERROR_SUCCESS != regkey.Create(HKEY_CURRENT_USER, _T("Software\应用程序向导生成的本地应用程序\testDialog\section2")))
            return FALSE;//失败
        //
        LSTATUS nRet = regkey.SetDWORDValue(_T("numb"), 1239);
        nRet = regkey.SetStringValue(_T("string"), _T("stringValue"));
        //regkey.SetBinaryValue()
    
        //
        TCHAR buf[512] = { 0 };
        ULONG nSize = _countof(buf);//in/out
        nRet = regkey.QueryStringValue(_T("string"), buf, &nSize);
        DWORD dwRet;
        nRet = regkey.QueryDWORDValue(_T("numb"), dwRet);
    
        //删除 name-value 对
        nRet = regkey.DeleteValue(_T("string"));
        //删除 subKey
        if (ERROR_SUCCESS != regkey.Open(HKEY_CURRENT_USER, _T("Software\应用程序向导生成的本地应用程序\testDialog")))
            return FALSE;//失败
        nRet = regkey.DeleteSubKey(_T("section2"));

    ***************

    常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。

    昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。
  • 相关阅读:
    MySQL-[--001--]-MySQL涉及的算法题
    Python3-2020-测试开发-25- 操作数据库(MySQL)
    Python3-2020-测试开发-24- os模块及os.path
    Python3-接口自动化-7-读取Excel封装方法
    Charles-2020-抓取Https包
    Python3-2020-测试开发-23- 文件操作
    Django模板(Template)系统
    Django视图系统
    Django路由系统
    Django框架
  • 原文地址:https://www.cnblogs.com/htj10/p/12228073.html
Copyright © 2020-2023  润新知