C++对注册表的操作

时间:2020-06-07 17:23:13   收藏:0   阅读:42

 

打开与关闭

 
RegOpenKeyEx RegCloseKey
 

#include <iostream>
#include <windows.h>
#include<stdlib.h>
using namespace std;
 int main()
 {
    HKEY hkey = NULL;
    const TCHAR * subkey = TEXT("Software\\Classes\\ms-settings\\shell\\open\\command");
    LONG iret = RegOpenKeyEx(HKEY_CURRENT_USER,subkey,0,KEY_ALL_ACCESS,&hkey);
    if(iret == ERROR_SUCCESS)
    {
        printf("OK");
    }
    system("pause");
     RegCloseKey(hkey);
    return 0;
 }

 

创建与删除

 

#include <iostream>
#include <windows.h>
#include<stdlib.h>
using namespace std;
int main()
{
    HKEY hsubkey = NULL;
    const TCHAR * subkey = TEXT("Software\\Classes\\ms-settings\\shell\\open\\Mikasa");
    LONG iret = RegCreateKeyEx(HKEY_CURRENT_USER,subkey,0,NULL,0,KEY_ALL_ACCESS,NULL,&hsubkey,NULL);
    if(iret == ERROR_SUCCESS)
    {
        printf("OK");
    }
    system("pause");
    return 0;
}

 
创建成功
 
删除
 

#include <iostream>
#include <windows.h>
#include<stdlib.h>
using namespace std;
int main()
{
    const TCHAR * subkey = TEXT("Software\\Classes\\ms-settings\\shell\\open\\Mikasa");

    LONG iret = RegDeleteKey(HKEY_CURRENT_USER,subkey);
    if(iret == ERROR_SUCCESS)
    {
        printf("OK");
    }
    system("pause");
    return 0;
    return 0;
}

对键中的相关操作

 
设置键值
 
RegSetValueEx
 

 
设置为REG_SZ的值
 

#include <iostream>
#include <windows.h>
#include <atlstr.h>
#include<stdlib.h>
using namespace std;
int main()
{
    HKEY hkey= NULL;

    const TCHAR * subkey = TEXT("Software\\Classes\\ms-settings\\shell\\open\\command");
    LONG iret = RegOpenKeyEx(HKEY_CURRENT_USER,subkey,0,KEY_ALL_ACCESS,&hkey);
    if(iret == ERROR_SUCCESS)
    {
        CString strPath= TEXT("C:\\Windows\\System32\\cmd.exe");
        RegSetValueEx(hkey,TEXT(""),0,REG_SZ,(LPBYTE)strPath.GetBuffer(),strPath.GetLength()*sizeof(TCHAR));
    }
    else
    {
        printf("Error");
    }
    RegCloseKey(hkey);
    system("pause");
    return 0;
}

 
设置为DW的值
 

#include <iostream>
#include <windows.h>
#include <atlstr.h>
#include<stdlib.h>
#pragma comment(lib,"winmm.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
using namespace std;
int main()
{
    HKEY hkey= NULL;

    const TCHAR * subkey = TEXT("Software\\Classes\\ms-settings\\shell\\open\\command");
    LONG iret = RegOpenKeyEx(HKEY_CURRENT_USER, subkey,0,KEY_ALL_ACCESS,&hkey);
    if(iret == ERROR_SUCCESS)
    {
        DWORD test=0;
        CString strPath= TEXT("C:\\Tools\\test.exe");
        RegSetValueEx(hkey,TEXT(""),0,REG_SZ,(LPBYTE)strPath.GetBuffer(),strPath.GetLength()*sizeof(TCHAR));
        RegSetValueEx(hkey,TEXT("DelegateExecute"),0,REG_DWORD,(BYTE*)&test,sizeof(DWORD));
    }
    else
    {
        printf("Error");
    }
    RegCloseKey(hkey);
    return 0;
}

 

原文:https://www.cnblogs.com/Mikasa-Ackerman/p/Cdui-zhu-ce-biao-de-cao-zuo.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!