在程序初始APP的初始化中加入:
//先获得当前的系统时间
CTime CurTime = CTime::GetCurrentTime();
//检查软件是否第一次运行,如果是则设定限制时间
//我们这儿设定软件在当前时间两分分钟后禁用
if (!IsInstalled())
{
CTime t = CurTime + CTimeSpan(0,0,2,0);
SetLimitTime(t.GetYear(),t.GetMonth(),t.GetDay(),
t.GetHour(),t.GetMinute(),t.GetSecond());
}
else
{
//如果软件不是第一次运行,
//则读取注册表中的时间值,与当前时间比较,
//如果当前时间值超过了软件使用期限,则退出,否则正常启动
CTime LimitTime;
GetLimitTime(LimitTime);
if (CurTime>LimitTime)
{
AfxMessageBox("试用版已到期,请联系购买一个正版使用",MB_OK);
return FALSE;
}
}
相关的函数如下:
BOOL IsInstalled()
{
BOOL bInstalled = FALSE;
DWORD dwDisposition;
HKEY m_hKey;
//打开注册表
LONG ret = RegCreateKeyEx (HKEY_LOCAL_MACHINE, "SOFTWARE//TimeLimit", 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisposition);
if (ret != ERROR_SUCCESS) //打开注册表失败
return FALSE;
//从注册表中读入特征值
DWORD dwVal,dwType,dwLen;
ret = RegQueryValueEx(m_hKey, "Installed", NULL,
&dwType,(unsigned char*)&dwVal,&dwLen);
if (ret != ERROR_SUCCESS) //读取数据失败
return FALSE;
//检查读到的特征值是否为1,如果是,则表明软件以前已运行过
bInstalled = (dwVal == 1);
//关闭注册表
RegCloseKey(m_hKey);
return bInstalled;
}
BOOL GetLimitTime(CTime& t)
{
DWORD dwDisposition;
HKEY m_hKey;
//打开注册表
LONG ret = RegCreateKeyEx (HKEY_LOCAL_MACHINE, "SOFTWARE//TimeLimit", 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisposition);
if (ret != ERROR_SUCCESS) //打开注册表失败
return FALSE;
//从注册表中读入试用期限数据
DWORD nYear,nMonth,nDay,nHour,nMinute,nSecond;
DWORD dwType;
DWORD dwLen;
ret = RegQueryValueEx (m_hKey, "Year", NULL, //读取年份
&dwType,(unsigned char*)&nYear,&dwLen);
if (ret != ERROR_SUCCESS) //读取数据失败
return FALSE;
ret = RegQueryValueEx (m_hKey, "Month", NULL, //读取月份
&dwType,(unsigned char*)&nMonth,&dwLen);
if (ret != ERROR_SUCCESS) //读取数据失败
return FALSE;
ret = RegQueryValueEx (m_hKey, "Day", NULL, //读取天
&dwType,(unsigned char*)&nDay,&dwLen);
if (ret != ERROR_SUCCESS) //读取数据失败
return FALSE;
ret = RegQueryValueEx (m_hKey, "Hour", NULL, //读取小时
&dwType,(unsigned char*)&nHour,&dwLen);
if (ret != ERROR_SUCCESS) //读取数据失败
return FALSE;
ret = RegQueryValueEx (m_hKey, "Minute", NULL, //读取分钟
&dwType,(unsigned char*)&nMinute,&dwLen);
if (ret != ERROR_SUCCESS) //读取数据失败
return FALSE;
ret = RegQueryValueEx (m_hKey, "Second", NULL, //读取秒
&dwType,(unsigned char*)&nSecond,&dwLen);
if (ret != ERROR_SUCCESS) //读取数据失败
return FALSE;
//关闭注册表
RegCloseKey(m_hKey);
t = CTime(nYear,nMonth,nDay,nHour,nMinute,nSecond);
return TRUE;
}
BOOL SetLimitTime(DWORD nYear,DWORD nMonth,DWORD nDay,
DWORD nHour,DWORD nMinute,DWORD nSecond)
{
DWORD dwDisposition;
HKEY m_hKey;
//打开注册表
LONG ret = RegCreateKeyEx (HKEY_LOCAL_MACHINE, "SOFTWARE//TimeLimit", 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisposition);
if (ret != ERROR_SUCCESS) //打开注册表失败
return FALSE;
//向注册表中写入试用期限数据
ret = RegSetValueEx (m_hKey, "Year", NULL, //写入年
REG_DWORD,(CONST BYTE*)&nYear,sizeof(DWORD));
if (ret != ERROR_SUCCESS) //写入数据失败
return FALSE;
ret = RegSetValueEx (m_hKey, "Month", NULL, //写入月份
REG_DWORD,(CONST BYTE*)&nMonth,sizeof(DWORD));
if (ret != ERROR_SUCCESS) //写入数据失败
return FALSE;
ret = RegSetValueEx (m_hKey, "Day", NULL, //写入天
REG_DWORD,(CONST BYTE*)&nDay,sizeof(DWORD));
if (ret != ERROR_SUCCESS) //写入数据失败
return FALSE;
ret = RegSetValueEx (m_hKey, "Hour", NULL, //写入小时
REG_DWORD,(CONST BYTE*)&nHour,sizeof(DWORD));
if (ret != ERROR_SUCCESS) //写入数据失败
return FALSE;
ret = RegSetValueEx (m_hKey, "Minute", NULL, //读取年份数据
REG_DWORD,(CONST BYTE*)&nMinute,sizeof(DWORD));
if (ret != ERROR_SUCCESS) //写入数据失败
return FALSE;
ret = RegSetValueEx (m_hKey, "Second", NULL, //写入秒
REG_DWORD,(CONST BYTE*)&nSecond,sizeof(DWORD));
if (ret != ERROR_SUCCESS) //写入数据失败
return FALSE;
//写入特征数据
DWORD dwVal = 1;
ret = RegSetValueEx (m_hKey, "Installed", NULL, //写入特征值
REG_DWORD,(CONST BYTE*)&dwVal,sizeof(DWORD));
if (ret != ERROR_SUCCESS) //写入数据失败
return FALSE;
//关闭注册表
RegCloseKey(m_hKey);
return TRUE;
}