C++对注册表的操作
打开与关闭
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;
}