MFC操作注册表

使用CWinApp类的成员函数

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

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"));

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

猜你喜欢

转载自www.cnblogs.com/htj10/p/12228073.html