vc中注册表的相关操作

RegisterKey.h

// RegisterKey.h: interface for the CRegisterKey class.
//
//

#if !defined(AFX_REGISTERKEY_H__2C89754E_9EAB_4366_BD9A_5BF3FB10CCFF__INCLUDED_)
#define AFX_REGISTERKEY_H__2C89754E_9EAB_4366_BD9A_5BF3FB10CCFF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include   <winreg.h>   

class CRegisterKey  
{
public:
	CRegisterKey();
	virtual ~CRegisterKey();
	LONG   Read(LPCTSTR pszKey,BYTE *pData,DWORD &dwLength);   
	LONG   Read(LPCTSTR pszKey,CString &sVal);   
	LONG   Read(LPCTSTR pszKey,DWORD &dwVal);   
	LONG   Write(LPCTSTR pszKey,const BYTE *pData,DWORD dwLength);   
	LONG   Write(LPCTSTR pszKey,LPCTSTR pszVal);   
	LONG   Write(LPCTSTR pszKey,DWORD dwVal);   
	void   Close();   
	LONG   Open(HKEY hKeyRoot,LPCTSTR pszPath,REGSAM dwAccess = KEY_ALL_ACCESS);
protected:   
	CString m_sPath;   
	HKEY m_hKey;   	
};

#endif // !defined(AFX_REGISTERKEY_H__2C89754E_9EAB_4366_BD9A_5BF3FB10CCFF__INCLUDED_)

CRegisterKey.cpp

在这里插入代码片
// RegisterKey.cpp: implementation of the CRegisterKey class.
//
//

#include "stdafx.h"
#include "RegisterKey.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

CRegisterKey::CRegisterKey()
{
	m_hKey = NULL; 
}

CRegisterKey::~CRegisterKey()
{
	Close();  
}

LONG CRegisterKey::Open(HKEY hKeyRoot,LPCTSTR pszPath,REGSAM dwAccess)   
{   
	DWORD dw;   
	m_sPath = pszPath;    
	return RegCreateKeyEx(hKeyRoot,pszPath,0L,NULL,REG_OPTION_NON_VOLATILE,dwAccess,NULL, &m_hKey,&dw);   创建指定的注册表项。如果键已经存在,函数将打开它。
}   

void  CRegisterKey::Close()   
{   
	if(m_hKey)   
	{   
		RegCloseKey(m_hKey);   
		m_hKey = NULL;   
	}   
}   

LONG  CRegisterKey::Write(LPCTSTR pszKey,DWORD dwVal)   
{   
	ASSERT(m_hKey);   
	ASSERT(pszKey);       
	return  RegSetValueEx(m_hKey,pszKey,0L,REG_DWORD,(CONST BYTE*)&dwVal,sizeof(DWORD));   
}   

LONG CRegisterKey::Write(LPCTSTR pszKey,LPCTSTR pszVal)   
{   
	ASSERT(m_hKey);   
	ASSERT(pszKey);   
	ASSERT(pszVal);
	ASSERT(AfxIsValidAddress(pszVal,strlen(pszVal),FALSE));       
	return RegSetValueEx(m_hKey,pszKey,0L,REG_SZ,(CONST BYTE*)pszVal,strlen(pszVal)+1);   
}   

LONG CRegisterKey::Write(LPCTSTR pszKey,const BYTE *pData,DWORD dwLength)   
{   
	ASSERT(m_hKey);   
	ASSERT(pszKey);   
	ASSERT(pData && dwLength>0);   
	ASSERT(AfxIsValidAddress(pData,dwLength,FALSE));    
	return RegSetValueEx(m_hKey,pszKey,0L,REG_BINARY,pData,dwLength);   
}   

LONG CRegisterKey::Read(LPCTSTR pszKey,DWORD &dwVal)   
{   
	ASSERT(m_hKey);   
	ASSERT(pszKey);   
    
	DWORD dwType;   
	DWORD dwSize = sizeof(DWORD);   
	DWORD dwDest;   
    
	LONG lRet = RegQueryValueEx(m_hKey,pszKey,NULL,&dwType,(LPBYTE) &dwDest,&dwSize);     
	if(lRet == ERROR_SUCCESS)
	{
		dwVal = dwDest;   
    }
	return lRet;   
}   

LONG CRegisterKey::Read(LPCTSTR pszKey,CString &sVal)   
{   
	ASSERT(m_hKey);   
	ASSERT(pszKey);   
    
	DWORD dwType;   
	DWORD dwSize = 512;   
	char string[512];   
    
	LONG lRet = RegQueryValueEx(m_hKey,pszKey,NULL,&dwType,(LPBYTE)string,&dwSize);    
	if(lRet == ERROR_SUCCESS) 
	{
		sVal = string;   
    }
	return lRet;   
}   

LONG CRegisterKey::Read(LPCTSTR pszKey,BYTE *pData,DWORD &dwLength)   
{   
	ASSERT(m_hKey);   
	ASSERT(pszKey);     
	DWORD dwType;      
	return RegQueryValueEx(m_hKey,pszKey,NULL,&dwType,pData,&dwLength);   
}   


注册表的逻辑调用

在这里插入代码片
void CRegisterDlg::OnOK() 
{
//	// TODO: Add extra validation here
//	
	UpdateData(true);
	m_sRegisterCode.TrimLeft();
	m_sRegisterCode.TrimRight();
	if (m_sRegisterCode.GetLength() != 32)
	{
		AfxMessageBox("注册码输入不正确!");
		return;
	}
	m_sID.TrimLeft();
	m_sID.TrimRight();
	if(strcmp(m_sID,m_sRegisterCode)==0)
	{
		AfxMessageBox("注册码输入不正确!");
		return;
	}
	
	char chRegisterCode[17],chTemp[24];
	ZeroMemory(chRegisterCode,17);
	ZeroMemory(chTemp,24);
	m_MyAes.HexStr2CharStr(m_sRegisterCode,(unsigned char*)chRegisterCode,16);
	m_MyAes.MakeKey(m_MyAes.GetDefaultKey(1),"");
	m_MyAes.DecryptBlock(chRegisterCode,chTemp);
	CStringArray group;
	if (ApartString(chTemp,"&",group))
	{
		m_sEncryptData =m_sEncryptData.Left(m_sEncryptData.GetLength()-1);
		if (group.GetSize() < 2 || group.GetAt(0) != m_sEncryptData)
		{
			AfxMessageBox("注册失败,检查是否输入正确!");
			return;
		}
		if (group.GetAt(1).Left(1)!= "0")//使用次数限制
		{
			AfxMessageBox("当前注册码未做使用次数限制,请重新获取注册码");
			return;
		}

	}
	else
	{
		if (strcmp(chTemp,m_sEncryptData))
		{
			AfxMessageBox("注册失败,检查是否输入正确!");
			return;
		}

		AfxMessageBox("当前注册码未做使用次数限制,请重新获取注册码");
		return;

	}	
	//注册码写到注册表
	CRegisterKey key;
	long nResult;
	if (ERROR_SUCCESS != key.Open(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\SZ\\app")))
	{
		AfxMessageBox("打开注册表失败");
		key.Close();
		return;
	}
	nResult = key.Write("Register_Code",m_sRegisterCode);

	if (nResult != ERROR_SUCCESS)
	{
		AfxMessageBox("更新注册码失败");
		key.Close();
		return;
	}
	if (key.Write("Serial_Number",m_sID) != ERROR_SUCCESS)
	{
		AfxMessageBox("更新序列号失败");
		key.Close();
		return;
	}
	key.Close();
    CDialog::OnOK();
}

在这里插入代码片
vBOOL CMulPcDownloadDlg::bRegister(int InitSys)
{
	SetWindowText("深圳JHL H60-A(THM3622)终端重刷机多路应用非对称签名升级程序VJHLMIN_V.2.1_20200318 授权终端台数:");
	CString sRegisterCode = "",sSerialNumber = "";
	CRegisterKey key;
    if (ERROR_SUCCESS != key.Open(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\SZ\\MUDOWNLAOD")))
	{
		AfxMessageBox("打开注册表失败");
		key.Close();
		return false;
	}
	key.Read("Register_Code",sRegisterCode);
	key.Read("Serial_Number",sSerialNumber);
	bool bRegedit = true;
	//有些电脑不能注册的是因为取硬盘ID号不正确,所以用一个固定的系列号
	if (sSerialNumber == "A4963A786A29E3ADAFBABB9500A9D413" ||
		sSerialNumber == "99F55FE446F63890066075F506F5690F" ||
		sSerialNumber == "386B7B3504605867B657E2DCAB54EE6A" ||
		sSerialNumber == "597F27905FCC6CA9FC8E1E6C3832123A" ||
		sSerialNumber == "D1C2B7202D16222F5E4C0507084DD4D4" ||
		sSerialNumber == "CDBBAEF9974119153EDFE55B92505F71" ||
		sSerialNumber == "04F03941C345F050617C67C088B33B0E" ||
		sSerialNumber == "9433154848CF7E56F28E1F99B46D5800" ||
		sSerialNumber == "6BECFF096A533137C0E26F1D38B9131A" ||
		sSerialNumber == "B01F3BDA241A8F8A875DCE9FB1B9ECF8")
	{
	      return false; 
	}

	if (sRegisterCode.GetLength() != 32 || sSerialNumber.GetLength() != 32)	{
		
register_soft:
		CRegisterDlg RegisterDlg;
//		if (RegisterDlg.DoModal() == IDCANCEL) 
//		{	 
//			return false;
//		}
//		bRegister(0);
		
	}
	else
	{
		CMyAes myAes;
		char chTemp[17],chEncryptData[17],chSerialNumber[17];
		ZeroMemory(chTemp,17);
		ZeroMemory(chEncryptData,17);
		ZeroMemory(chSerialNumber,17);
		//获取序列号中的硬盘序列号
		myAes.MakeKey(myAes.GetDefaultKey(),"");
		myAes.HexStr2CharStr(sSerialNumber,(unsigned char*)chTemp,16);
		myAes.DecryptBlock(chTemp,chSerialNumber);
		//获取注册码中的加密数据
		myAes.MakeKey(myAes.GetDefaultKey(1),"");
		ZeroMemory(chTemp,17);
		myAes.HexStr2CharStr(sRegisterCode,(unsigned char*)chTemp,16);
		myAes.DecryptBlock(chTemp,chEncryptData);
		CPublicFunction fun;
	
		//获取本地硬盘序列号
		//DWORD dwLocalSerialNumber;
		CString sLocalSerialNumber;
		CStringArray StrAryTemp;
		//myAes.GetCDriveSerialNumber(dwLocalSerialNumber);
		//sLocalSerialNumber.Format("%08x",dwLocalSerialNumber);

 
		myAes.GetCDriveSerialNumber(sLocalSerialNumber);

	//	AfxMessageBox("本地硬盘序列号:"+sLocalSerialNumber);
		//AfxMessageBox(chEncryptData);
		//AfxMessageBox(chSerialNumber);

		//判断注册码中是否包含限制
		if (fun.ApartString(chEncryptData,"&",StrAryTemp))
		{
			CString  m_sEncryptData =chSerialNumber;
			m_sEncryptData =m_sEncryptData.Left(m_sEncryptData.GetLength()-1); 
					//判断注册码中的硬盘序列号和序列号中的硬盘序列号是否相同
			if (StrAryTemp.GetSize() < 2 || strcmp(StrAryTemp.GetAt(0),m_sEncryptData))
			{
				goto register_soft;
			}
			//判断序列号中的硬盘序列号是否和本地硬盘序列号相同
			/*
			if (sLocalSerialNumber != Temp.GetAt(0))
			{
				goto register_soft;
			}
			*/
			//判断限制类型
			if (StrAryTemp.GetAt(1).Left(1) == "0")//使用次数限制
			{
				int nUseTimes=atoi(StrAryTemp.GetAt(1).Right(StrAryTemp.GetAt(1).GetLength() - 1));
				if (nUseTimes > 0)//允许继续使用,更新注册码
				{
					CString sData;
					if (InitSys==1)
					{
				       nUseTimes --;  //启动不做递减
					}
					sData.Format("%s&0%d",StrAryTemp.GetAt(0),nUseTimes);
					ZeroMemory(chTemp,17);
					myAes.EncryptBlock(sData,chTemp);
					char chRegisterCode[33];
					ZeroMemory(chRegisterCode,33);
					myAes.CharStr2HexStr((unsigned char*)chTemp,chRegisterCode,16);
					sRegisterCode = chRegisterCode;
				
					key.Write("Register_Code",sRegisterCode);
					if (nUseTimes ==0)
					{
						  AfxMessageBox("软件已到期,请重新注册!!"); 
						  //CDialog::OnCancel();
						  goto register_soft;
					}
					if (InitSys !=-1)
					{
						sData.Format("%d",nUseTimes);   
					}
				}
			}
			
			
		}		
	}
	   return TRUE;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物联网小镇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值