BOOL CEverTestDlg::CheckExpire(void)
{
COleDateTime expire = COleDateTime(2012,7,1,0,0,0);//过期时间
COleDateTime nowtime = COleDateTime::GetCurrentTime();//当前系统时间
//1.判断系统时间
if(nowtime >= expire)
{
return TRUE;
}
//2.判断
BYTE buf[256];
HKEY hKey;
HKEY mKey;
CString regPath = _T("SOFTWARE");
CString subKey = _T("0rz");
CString fullPath = regPath+_T("\\")+subKey;
CString regKey = _T("lic");
DWORD buflen = sizeof(buf);
//打开注册表项,没有则创建
LONG rt = RegOpenKeyEx(HKEY_LOCAL_MACHINE, fullPath, 0, KEY_ALL_ACCESS, &hKey);//打开全路径
if(rt != ERROR_SUCCESS)//打开失败
{
//准备创建
rt = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath, 0, KEY_ALL_ACCESS, &hKey);//打开上级路径
if(rt != ERROR_SUCCESS)
{
AfxMessageBox(_T("注册错误[1001]"));
exit(1);
}
DWORD dw;
rt = RegCreateKeyEx(hKey, subKey, 0, NULL, REG_OPTION_NON_VOLATILE, 0, NULL, &mKey, &dw);//创建子项
if(rt != ERROR_SUCCESS)
{
AfxMessageBox(_T("注册错误[1002]"));
exit(1);
}
RegCloseKey(hKey);
RegCloseKey(mKey);
hKey = NULL;
//hKey = mKey;
}
if(hKey == NULL)
{
//再次打开全路径
rt = RegOpenKeyEx(HKEY_LOCAL_MACHINE, fullPath, 0, KEY_ALL_ACCESS, &hKey);//打开全路径
if(rt != ERROR_SUCCESS)
{
AfxMessageBox(_T("注册错误[1003]"));
exit(1);
}
}
//查询
memset(buf, 0, buflen);
//rt = RegQueryValue(hKey, subKey, buf, &buflen);
DWORD type = REG_BINARY;
rt = RegQueryValueEx(hKey, regKey, NULL, &type, (LPBYTE)&buf, &buflen );
if(rt != ERROR_SUCCESS)//目标值不存在
{
//写入目标值
time_t t = time(NULL);
int size = sizeof(t);
memcpy(buf, &t, size);
int i;
BYTE sum = 0;
for(i=0; i<size; i++)
{
sum+=buf[i];
}
buf[size] = sum;
rt = RegSetValueEx(hKey, regKey, 0, REG_BINARY, buf, size+1);
if(rt != ERROR_SUCCESS)
{
AfxMessageBox(_T("注册错误[1004]"));
exit(1);
}
}
RegCloseKey(hKey);
//ColeDateTime regtime = a;
return FALSE;
}
test-reg
最新推荐文章于 2019-03-05 17:22:48 发布