-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandLottery.cpp
480 lines (413 loc) · 12 KB
/
RandLottery.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// RandLottery.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "RandLottery.h"
#include "RandLotteryDlg.h"
#include "GetHardSoftInfo.h"
#include "MD5Checksum.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRandLotteryApp
BEGIN_MESSAGE_MAP(CRandLotteryApp, CWinApp)
//{{AFX_MSG_MAP(CRandLotteryApp)
//}}AFX_MSG_MAP
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRandLotteryApp construction
CRandLotteryApp::CRandLotteryApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
m_bIsRegedited = FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CRandLotteryApp object
CRandLotteryApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CRandLotteryApp initialization
BOOL CRandLotteryApp::InitInstance()
{
#ifdef USE_MACHINE_SERIAL_REGEDIT
if (!IsRegedited()) //如果还没有注册
{
//不能开始抽奖
m_bIsRegedited = FALSE;
}
else //如果有注册
{
//1.读出注册码
HKEY MSR_hHey;
char data[32];
DWORD size=32;
DWORD type=REG_SZ;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,MSR_SUB_KEY,
REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,&MSR_hHey) != ERROR_SUCCESS)
{
AfxMessageBox("打开注册表失败!");
m_bIsRegedited = FALSE;
}
if(RegQueryValueEx(MSR_hHey,MSR_KEY_VALUE_NAME_2,0,&type,(BYTE*)data,&size) != ERROR_SUCCESS)
{
AfxMessageBox("查询注册表失败2!");
m_bIsRegedited = FALSE;
}
else
{
m_strReadRegSerial = data;
CString strRegSerial(_T("")),strMachineSerial(_T(""));
CGetMachineInfo m_DiskInfo;
strMachineSerial = m_DiskInfo.str_DN_Serial;
strMachineSerial.TrimLeft();
strMachineSerial.MakeUpper();
strMachineSerial=strMachineSerial.Mid(HD_FIRST,HD_COUNT);
strMachineSerial=theApp.GetCPUSerial(CPU_FIRST,CPU_COUNT)+strMachineSerial;
strRegSerial =CMD5Checksum::GetMD5((BYTE*)(const char*)strMachineSerial.GetBuffer(0),strMachineSerial.GetLength());
strRegSerial.MakeUpper(); //注册码大写
strRegSerial = strRegSerial.Mid(2,20); //取第二位开始后面的20个数
//2.根据机器码,计算注册码,对比注册表中的注册码
if (strcmp(m_strReadRegSerial.Right(2*(CPU_COUNT+HD_COUNT)),strRegSerial) == 0)
{
m_bIsRegedited = TRUE;
}
else
{
m_bIsRegedited = FALSE;
}
}
}
#endif
#ifdef TEST_VESION
CString strReserv;
int nYearCur,nMonthCur,nDayCur,nHourCur; //当前时间
CTime CurTime = CTime::GetCurrentTime();
nYearCur = CurTime.GetYear();
nMonthCur =CurTime.GetMonth();
nDayCur = CurTime.GetDay();
nHourCur = CurTime.GetHour();
LONG lDayCur = (((nYearCur*12)+nMonthCur) * 365 +nDayCur)*24+nHourCur; //全部换算成小时
CFileFind fFind;
BOOL bFileFined;
if(!(bFileFined = fFind.FindFile(FILE_PATH)))
{
CreatFisrtUseTimeFile(nYearCur,nMonthCur,nDayCur,nHourCur);
}
else
{
CString strTime;
GetTimeFromFile(strTime);
LONG lDay = _ttol(strTime);
if (lDayCur < lDay)
{
AfxMessageBox("试用版已到期,请联系购买一个正版使用",MB_OK);
return 1;
}
else
{
DOUBLE dDayReserv = (lDayCur - lDay)/24.0;
if(dDayReserv >= USE_DAY)
{
AfxMessageBox("试用版已到期,请联系购买一个正版使用",MB_OK);
return 1;
}
else
{
int i = (int)(USE_DAY-dDayReserv);
strReserv.Format("你正在使用的是试用版,试用期限还剩%d天",i);
//AfxMessageBox(str,MB_OK);
}
}
}
//写入注册表
//检查软件是否第一次运行,如果是则设定限制时间
//我们这儿设定软件在当前时间两分分钟后禁用
if (!IsInstalled())
{
//CTime t = CurTime;// + CTimeSpan(USE_DAY,0,0,0); //2分钟的使用期限
SetLimitTime(CurTime.GetYear(),CurTime.GetMonth(),CurTime.GetDay(),
CurTime.GetHour());
}
else
{
//如果软件不是第一次运行,
//则读取注册表中的时间值,与当前时间比较,
//如果当前时间值超过了软件使用期限,则退出,否则正常启动
CTime FirstUseTime;
GetLimitTime(FirstUseTime);
int nYearOld,nMonthOld,nDayOld,nHourOld; //保存的时间
nYearOld = FirstUseTime.GetYear();
nMonthOld =FirstUseTime.GetMonth();
nDayOld = FirstUseTime.GetDay();
nHourOld = FirstUseTime.GetHour();
LONG lDayOld = (((nYearOld*12)+nMonthOld) * 365 +nDayOld)*24+nHourOld; //全部换算成小时
if (lDayCur < lDayOld)
{
AfxMessageBox("试用版已到期,请联系购买一个正版使用3",MB_OK);
return 1;
}
else
{
DOUBLE dDayReserv = (lDayCur - lDayOld)/24.0;
if(dDayReserv >= USE_DAY)
{
AfxMessageBox("试用版已到期,请联系购买一个正版使用4",MB_OK);
return 1;
}
else
{
//CString str;
int i = (int)(USE_DAY-dDayReserv);
strReserv.Format("你正在使用的是试用版,试用期限还剩%d天",i);
//AfxMessageBox(str,MB_OK);
}
}
}
AfxMessageBox(strReserv,MB_OK);
#endif
AfxEnableControlContainer();
#if 1
if( !AfxOleInit() ){
AfxMessageBox("初始化Ole出错!");
return FALSE;
}
#else
if (CoInitialize(NULL)!=0)
{
AfxMessageBox("初始化COM支持库失败!");
exit(1);
}
#endif
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#if _MSC_VER <= 1200 // MFC 6.0 or earlier
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
#endif
// 此程序只能运行一次,用互斥量来判断程序是否已运行 //Koby_Ou 2013.12.13
HANDLE m_hMutex=CreateMutex(NULL,TRUE, m_pszAppName);
if(GetLastError()==ERROR_ALREADY_EXISTS){ return FALSE; }
CRandLotteryDlg dlg;
m_pMainWnd = &dlg;
//SetDialogBkColor(RGB(255,255,255), RGB(255, 255, 255));//土黄色
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
#ifdef TEST_VESION
BOOL CRandLotteryApp::CreatFisrtUseTimeFile(DWORD nYear, DWORD nMonth, DWORD nDay, DWORD nHour, DWORD nMinute, DWORD nSecond)
{
LONG lDayOld = (((nYear*12)+nMonth) * 365 +nDay)*24+nHour; //全部换算成小时
CString strDayOld;
strDayOld.Format("%d",lDayOld);
CStdioFile file;// //
file.Open(FILE_PATH,CFile::modeCreate | CFile::modeWrite); //如果没有此文件就创建一个文件
file.WriteString(strDayOld);
file.Close();
return TRUE;
}
BOOL CRandLotteryApp::GetTimeFromFile(CString &strTime)
{
CStdioFile file;// //
file.Open(FILE_PATH, CFile::modeRead);
file.ReadString(strTime);
file.Close();
return TRUE;
}
BOOL CRandLotteryApp::IsInstalled()
{
BOOL bInstalled = FALSE;
DWORD dwDisposition;
HKEY m_hKey;
//打开注册表
if(RegCreateKeyEx (HKEY_LOCAL_MACHINE, SUB_KEY, 0L, NULL,REG_OPTION_VOLATILE,
KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisposition) != ERROR_SUCCESS)
{
return FALSE;//打开注册表失败
}
//从注册表中读入特征值
DWORD dwVal,dwType,dwLen;
if(RegQueryValueEx(m_hKey, KEY_VALUE_NAME, NULL,
&dwType,(unsigned char*)&dwVal,&dwLen) != ERROR_SUCCESS)
{
return FALSE; //读取数据失败
}
//检查读到的特征值是否为1,如果是,则表明软件以前已运行过
bInstalled = (dwVal == 1);
//关闭注册表
RegCloseKey(m_hKey);
return bInstalled;
}
BOOL CRandLotteryApp::GetLimitTime(CTime &t)
{
DWORD dwDisposition;
HKEY m_hKey;
//打开注册表
if(RegCreateKeyEx (HKEY_LOCAL_MACHINE, SUB_KEY, 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisposition) != ERROR_SUCCESS)
{
return FALSE;//打开注册表失败
}
//从注册表中读入试用期限数据
DWORD nYear,nMonth,nDay,nHour,nMinute,nSecond;
DWORD dwType;
DWORD dwLen;
if(RegQueryValueEx (m_hKey, "Year", NULL, //读取年份
&dwType,(unsigned char*)&nYear,&dwLen) != ERROR_SUCCESS)
{
return FALSE; //读取数据失败
}
if(RegQueryValueEx (m_hKey, "Month", NULL, //读取月份
&dwType,(unsigned char*)&nMonth,&dwLen) != ERROR_SUCCESS)
{
return FALSE; //读取数据失败
}
if(RegQueryValueEx (m_hKey, "Day", NULL, //读取天
&dwType,(unsigned char*)&nDay,&dwLen) != ERROR_SUCCESS)
{
return FALSE; //读取数据失败
}
if(RegQueryValueEx (m_hKey, "Hour", NULL, //读取小时
&dwType,(unsigned char*)&nHour,&dwLen) != ERROR_SUCCESS)
{
return FALSE; //读取数据失败
}
if(RegQueryValueEx (m_hKey, "Minute", NULL, //读取分钟
&dwType,(unsigned char*)&nMinute,&dwLen) != ERROR_SUCCESS)
{
return FALSE;
}
if(RegQueryValueEx (m_hKey, "Second", NULL, //读取秒
&dwType,(unsigned char*)&nSecond,&dwLen) != ERROR_SUCCESS)
{
return FALSE;
}
//关闭注册表
RegCloseKey(m_hKey);
t = CTime(nYear,nMonth,nDay,nHour,nMinute,nSecond);
return TRUE;
}
BOOL CRandLotteryApp::SetLimitTime(DWORD nYear, DWORD nMonth, DWORD nDay, DWORD nHour, DWORD nMinute, DWORD nSecond)
{
DWORD dwDisposition;
HKEY m_hKey;
//打开注册表
if(RegCreateKeyEx (HKEY_LOCAL_MACHINE, SUB_KEY, 0L, NULL,REG_OPTION_VOLATILE,
KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisposition) != ERROR_SUCCESS)
{
return FALSE;//打开注册表失败
}
//向注册表中写入试用期限数据
if(RegSetValueEx (m_hKey, "Year", NULL, //写入年
REG_DWORD,(CONST BYTE*)&nYear,sizeof(DWORD)) != ERROR_SUCCESS)
{
return FALSE;//写入数据失败
}
if(RegSetValueEx (m_hKey, "Month", NULL, //写入月份
REG_DWORD,(CONST BYTE*)&nMonth,sizeof(DWORD)) != ERROR_SUCCESS)
{
return FALSE;//写入数据失败
}
if(RegSetValueEx (m_hKey, "Day", NULL, //写入天
REG_DWORD,(CONST BYTE*)&nDay,sizeof(DWORD)) != ERROR_SUCCESS)
{
return FALSE;//写入数据失败
}
if(RegSetValueEx (m_hKey, "Hour", NULL, //写入小时
REG_DWORD,(CONST BYTE*)&nHour,sizeof(DWORD)) != ERROR_SUCCESS)
{
return FALSE;//写入数据失败
}
if(RegSetValueEx (m_hKey, "Minute", NULL, //读取年份数据
REG_DWORD,(CONST BYTE*)&nMinute,sizeof(DWORD)) != ERROR_SUCCESS)
{
return FALSE;//写入数据失败
}
if(RegSetValueEx (m_hKey, "Second", NULL, //写入秒
REG_DWORD,(CONST BYTE*)&nSecond,sizeof(DWORD)) != ERROR_SUCCESS)
{
return FALSE;//写入数据失败
}
//写入特征数据
DWORD dwVal = 1;
if(RegSetValueEx (m_hKey, KEY_VALUE_NAME, NULL, //写入特征值
REG_DWORD,(CONST BYTE*)&dwVal,sizeof(DWORD)) != ERROR_SUCCESS)
{
return FALSE; //写入数据失败
}
//关闭注册表
RegCloseKey(m_hKey);
return TRUE;
}
#endif
BOOL CRandLotteryApp::IsRegedited()
{
BOOL bRegedit = FALSE;
HKEY MSR_hHey;
//创建或打开注册表项
if(RegCreateKey(HKEY_LOCAL_MACHINE,MSR_SUB_KEY,&MSR_hHey) != ERROR_SUCCESS)
{
AfxMessageBox("创建注册表失败!");
return FALSE;
}
//从注册表中读入特征值
/* DWORD dwVal,dwType,dwLen;*/
char data[2];
DWORD size=2;
DWORD type=REG_SZ;
if(RegQueryValueEx(MSR_hHey,MSR_KEY_VALUE_NAME_1,0,&type,(BYTE*)data,&size) != ERROR_SUCCESS)
{
return FALSE; //读取数据失败
}
//检查读到的特征值是否为1,如果是,则表明软件以前已经注册过
bRegedit = (data[0] == '1');
//关闭注册表
RegCloseKey(MSR_hHey);
return bRegedit;
}
CString CRandLotteryApp::GetCPUSerial(int nFirst, int nCount)
{
//获取CPU序列号
unsigned long s1,s2;
char sel;
sel='1';
CString CpuID,CPUID1,CPUID2;
__asm{
mov eax,01h
xor edx,edx
cpuid
mov s1,edx
mov s2,eax
}
CPUID1.Format("%08X%08X",s1,s2);
__asm{
mov eax,03h
xor ecx,ecx
xor edx,edx
cpuid
mov s1,edx
mov s2,ecx
}
CPUID2.Format("%08X%08X",s1,s2);
CpuID=CPUID1+CPUID2;
return CpuID.Mid(nFirst,nCount);
}