注册表写入

#include <iostream>  
#include <algorithm>  
#include <cmath>  
#include <vector>  
#include <string>  
#include <cstring>
#include <atlbase.h>
#include <Windows.h>
#include <atlstr.h>
#pragma warning(disable:4996)  
using namespace std;

bool write_reg_sz(LPCTSTR SubKey, LPCTSTR Key, CString Value)
{
    HKEY hkey;
    HKEY hTempKey;
    DWORD nLength = (Value.GetLength() + 1) * sizeof(TCHAR);    // 定义数据长度
    LPBYTE lpbName = new BYTE[nLength * sizeof(TCHAR)];    // 有可能是 Ansi 或 Unicode
    memcpy(lpbName, (void*)(LPCTSTR)Value, (Value.GetLength() + 1) * sizeof(TCHAR));
    LPCTSTR data_set = _T("");
    if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CLASSES_ROOT, data_set, 0, KEY_SET_VALUE, &hkey))
    {
        if (ERROR_SUCCESS == ::RegCreateKey(hkey, SubKey, &hTempKey))
        {
            if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, Key, 0, REG_SZ, lpbName, nLength))
            {
                cout << "写入错误" << endl;
            }
        }
        ::RegCloseKey(hTempKey);
    }
    ::RegCloseKey(hkey);
    return true;
}

int main()
{   
    write_reg_sz(_T("hahah"), _T("URL Protocol"),_T(""));
    write_reg_sz(_T("hahah"), _T(""), _T("hahah Protocol"));
    write_reg_sz(_T("hahah\DefaultIcon"), _T(""), _T("XXXX路径"));
    write_reg_sz(_T("hahah\shell"), _T(""), _T(""));
    write_reg_sz(_T("hahah\shell\open"), _T(""), _T(""));
    write_reg_sz(_T("hahah\shell\open\command"), _T(""), _T("XXXXXX路径 %1"));
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/Galesaur-wcy/p/15141344.html