CheckDog.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include "stdafx.h"
  2. #include "CheckDog.h"
  3. #include <time.h> //头文件
  4. #include "resource.h"
  5. HANDLE g_hReadDogThread = NULL;
  6. #if IS_RAND_WRITE_DOG
  7. HANDLE g_hWriteDogThread = NULL;
  8. #endif
  9. DOGWRITE DogWrite = NULL;
  10. DOGREAD DogRead = NULL;
  11. HINSTANCE g_hDogLib;
  12. VOID TerminateLastProcess();
  13. //初始化软件狗
  14. int InitDog( void )
  15. {
  16. CHAR strFile[MAX_LIST_LENGTH + 1] = "";
  17. g_hDogLib = NULL;
  18. wsprintf(strFile, "%s\\Dll\\Win32dll.dll", g_strDirectory);
  19. g_hDogLib = AfxLoadLibrary(strFile);
  20. if (g_hDogLib < (HINSTANCE)HINSTANCE_ERROR)
  21. {
  22. LOG4C((LOG_NOTICE, "Load Dog Dll failed!"));
  23. return ERR_CODE_DOG_INIT_FAULT;
  24. }
  25. DogRead = (DOGREAD)GetProcAddress(g_hDogLib, _T("DogRead"));
  26. DogWrite = (DOGWRITE)GetProcAddress(g_hDogLib, _T("DogWrite"));
  27. if (DogRead == NULL || DogWrite == NULL)
  28. {
  29. LOG4C((LOG_NOTICE, "Get interface failed!"));
  30. AfxFreeLibrary(g_hDogLib);
  31. return ERR_CODE_DOG_INIT_FAULT;
  32. }
  33. CString sMsg;
  34. if( 0 != ReadDogInfo(sMsg) )
  35. {
  36. LOG4C((LOG_NOTICE, "%s", sMsg));
  37. g_sPassDueInfo = sMsg;
  38. return -1;
  39. }
  40. //CheckDogThreadStart();
  41. return 0;
  42. }
  43. int UnInitDog(void)
  44. {
  45. CheckDogThreadEnd();
  46. if( g_hDogLib != NULL )
  47. {
  48. AfxFreeLibrary(g_hDogLib);
  49. }
  50. return 0;
  51. }
  52. int ReadDogInfo( CString &sMsg )
  53. {
  54. DWORD dwStatus; // 读狗或写狗返回状态
  55. DWORD dwAddr; // 读狗或写狗的起始地址
  56. DWORD dwBytes; // 读狗或写狗的操作字节数据
  57. DWORD dwData = 0; // 写狗的数据内容
  58. char szData[10]; // 读狗的数据内容存放在这里,暂定为最多读10个字节
  59. memset(szData, 0, sizeof(szData));
  60. dwAddr = 6;
  61. dwBytes = 1;
  62. dwStatus = DogRead( dwBytes, dwAddr, szData );
  63. szData[1] = 0;
  64. if( szData[0] == (char)0xFA ) //250为正式版本,只检测公司信息
  65. {
  66. sMsg = "狗的版本为正式版,谢谢使用!!";
  67. dwAddr = 0;
  68. dwBytes = 6;
  69. dwStatus = DogRead( dwBytes, dwAddr, szData );
  70. int nTmp = strcmp((char *)szData, "SToneU");
  71. if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
  72. {
  73. sMsg = "read company information lost,soft exit!";
  74. g_nUseTimeLimit = 1;
  75. return -1;
  76. }
  77. }
  78. else // 试用版本
  79. {
  80. dwAddr = 0;
  81. dwBytes = 6;
  82. dwStatus = DogRead( dwBytes, dwAddr, szData );
  83. int nTmp = strcmp((char *)szData, "SToneU");
  84. if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
  85. {
  86. sMsg = "read company information lost,soft exit!";
  87. g_nUseTimeLimit = 1;
  88. return -1;
  89. }
  90. //是否已过使用期限
  91. dwAddr = 13;
  92. dwBytes = 1;
  93. memset(szData, 0, sizeof(szData));
  94. dwStatus = DogRead( dwBytes, dwAddr, szData );
  95. g_nUseTimeLimit = szData[0];
  96. if( 1 == g_nUseTimeLimit )
  97. {
  98. sMsg = "狗已经过期了!!";
  99. g_nUseTimeLimit = 1;
  100. return -1;
  101. }
  102. // 读取年
  103. dwAddr = 8;
  104. dwBytes = 2;
  105. memset(szData, 0, sizeof(szData));
  106. dwStatus = DogRead( dwBytes, dwAddr, szData );
  107. short nDogYear = *(short*)szData;
  108. // 读取月
  109. dwAddr = 10;
  110. dwBytes = 1;
  111. memset(szData, 0, sizeof(szData));
  112. dwStatus = DogRead( dwBytes, dwAddr, szData );
  113. int nDogMonth = szData[0];
  114. // 读取日
  115. dwAddr = 11;
  116. dwBytes = 1;
  117. memset(szData, 0, sizeof(szData));
  118. dwStatus = DogRead( dwBytes, dwAddr, szData );
  119. int nDogDay = szData[0];
  120. // 读取试用月数
  121. dwAddr = 7;
  122. dwBytes = 1;
  123. memset(szData, 0, sizeof(szData));
  124. dwStatus = DogRead( dwBytes, dwAddr, szData );
  125. int nUseDay = szData[0];
  126. CTime ctNow = CTime::GetCurrentTime();
  127. int nYear, nMonth, nDay;
  128. nYear = ctNow.GetYear();
  129. nMonth = ctNow.GetMonth();
  130. nDay = ctNow.GetDay();
  131. // 判断系统时间是否被修改
  132. if( nDogYear > nYear )
  133. {
  134. sMsg = "system time edited,soft exit!";
  135. g_nUseTimeLimit = 1;
  136. return -1;
  137. }
  138. else if( nDogYear == nYear )
  139. {
  140. if( nDogMonth > nMonth )
  141. {
  142. sMsg = "system time edited,soft exit!";
  143. g_nUseTimeLimit = 1;
  144. return -1;
  145. }
  146. else if( nDogMonth == nMonth )
  147. {
  148. if( nDogDay > nDay )
  149. {
  150. sMsg = "system time edited,soft exit!";
  151. g_nUseTimeLimit = 1;
  152. return -1;
  153. }
  154. }
  155. }
  156. int nUsed = 0;
  157. if( nMonth > nDogMonth )
  158. {
  159. nUsed = ( nYear - nDogYear ) * 12 + ( nMonth - nDogMonth );
  160. }
  161. else
  162. {
  163. nUsed = ( nYear - nDogYear ) * 12 - nDogMonth + nMonth;
  164. }
  165. if( nUsed > nUseDay && 0 == g_nUseTimeLimit )
  166. {
  167. sMsg.Format( "狗过期了 nUsed = %s,nUseDay = ",nUsed,nUseDay );
  168. dwAddr = 13;
  169. dwBytes = 1;
  170. memset(szData, 0, sizeof(szData));
  171. szData[0] = 1;
  172. DogWrite( dwBytes, dwAddr, szData );
  173. g_nUseTimeLimit = 1;
  174. return -1;
  175. }
  176. }
  177. return 0;
  178. }
  179. BOOL CheckDogThreadStart()
  180. {
  181. DWORD dwThreadId = 0;
  182. MTVERIFY( g_hReadDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadDogThreadProc, NULL, 0, &dwThreadId) );
  183. #if IS_RAND_WRITE_DOG
  184. MTVERIFY( g_hWriteDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteDogThreadProc, NULL, 0, &dwThreadId) );
  185. #endif
  186. return TRUE;
  187. }
  188. BOOL CheckDogThreadEnd()
  189. {
  190. // 关闭线程
  191. if (NULL != g_hReadDogThread)
  192. {
  193. MTVERIFY( WaitForSingleObject( g_hReadDogThread, INFINITE ) != WAIT_FAILED );
  194. MTVERIFY( CloseHandle( g_hReadDogThread ) );
  195. g_hReadDogThread = NULL;
  196. }
  197. #if IS_RAND_WRITE_DOG
  198. if (NULL != g_hWriteDogThread)
  199. {
  200. MTVERIFY( WaitForSingleObject( g_hWriteDogThread, INFINITE ) != WAIT_FAILED );
  201. MTVERIFY( CloseHandle( g_hWriteDogThread ) );
  202. g_hWriteDogThread = NULL;
  203. }
  204. #endif
  205. return TRUE;
  206. }
  207. DWORD WINAPI ReadDogThreadProc(LPVOID lpParameter)
  208. {
  209. do
  210. {
  211. DWORD dwStatus; // 读狗或写狗返回状态
  212. DWORD dwAddr; // 读狗或写狗的起始地址
  213. DWORD dwBytes; // 读狗或写狗的操作字节数据
  214. DWORD dwData = 0; // 写狗的数据内容
  215. char szData[10]; // 读狗的数据内容存放在这里,暂定为最多读10个字节
  216. memset(szData, 0, sizeof(szData));
  217. dwAddr = 6;
  218. dwBytes = 1;
  219. dwStatus = DogRead( dwBytes, dwAddr, szData );
  220. szData[1] = 0;
  221. if( szData[0] == (char)0xFA ) //250为正式版本,只检测公司信息
  222. {
  223. LOG4C((LOG_NOTICE, "狗的版本为正式版,谢谢使用!!"));
  224. return 0;
  225. dwAddr = 0;
  226. dwBytes = 6;
  227. dwStatus = DogRead( dwBytes, dwAddr, szData );
  228. int nTmp = strcmp((char *)szData, "SToneU");
  229. if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
  230. {
  231. LOG4C((LOG_NOTICE, "read company information lost,soft exit!"));
  232. TerminateLastProcess();
  233. return 0;
  234. }
  235. }
  236. else // 试用版本
  237. {
  238. dwAddr = 0;
  239. dwBytes = 6;
  240. dwStatus = DogRead( dwBytes, dwAddr, szData );
  241. int nTmp = strcmp((char *)szData, "SToneU");
  242. if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
  243. {
  244. LOG4C((LOG_NOTICE, "read company information lost,soft exit!"));
  245. TerminateLastProcess();
  246. return 0;
  247. }
  248. // 是否已过使用期限
  249. dwAddr = 13;
  250. dwBytes = 1;
  251. memset(szData, 0, sizeof(szData));
  252. dwStatus = DogRead( dwBytes, dwAddr, szData );
  253. g_nUseTimeLimit = szData[0];
  254. #if 0
  255. TerminateLastProcess();
  256. #endif
  257. if( 1 == g_nUseTimeLimit )
  258. {
  259. LOG4C((LOG_NOTICE, "狗已经过期,谢谢使用!!"));
  260. return 0;
  261. }
  262. // 读取年
  263. dwAddr = 8;
  264. dwBytes = 2;
  265. memset(szData, 0, sizeof(szData));
  266. dwStatus = DogRead( dwBytes, dwAddr, szData );
  267. short nDogYear = *(short*)szData;
  268. // 读取月
  269. dwAddr = 10;
  270. dwBytes = 1;
  271. memset(szData, 0, sizeof(szData));
  272. dwStatus = DogRead( dwBytes, dwAddr, szData );
  273. int nDogMonth = szData[0];
  274. // 读取日
  275. dwAddr = 11;
  276. dwBytes = 1;
  277. memset(szData, 0, sizeof(szData));
  278. dwStatus = DogRead( dwBytes, dwAddr, szData );
  279. int nDogDay = szData[0];
  280. // 读取试用月数
  281. dwAddr = 7;
  282. dwBytes = 1;
  283. memset(szData, 0, sizeof(szData));
  284. dwStatus = DogRead( dwBytes, dwAddr, szData );
  285. int nUseDay = szData[0];
  286. CTime ctNow = CTime::GetCurrentTime();
  287. int nYear, nMonth, nDay;
  288. nYear = ctNow.GetYear();
  289. nMonth = ctNow.GetMonth();
  290. nDay = ctNow.GetDay();
  291. // 判断系统时间是否被修改
  292. if( nDogYear > nYear )
  293. {
  294. LOG4C((LOG_NOTICE, "system time edited,soft exit!"));
  295. TerminateLastProcess();
  296. return 0;
  297. }
  298. else if( nDogYear == nYear )
  299. {
  300. if( nDogMonth > nMonth )
  301. {
  302. LOG4C((LOG_NOTICE, "system time edited,soft exit!"));
  303. TerminateLastProcess();
  304. return 0;
  305. }
  306. else if( nDogMonth == nMonth )
  307. {
  308. if( nDogDay > nDay )
  309. {
  310. LOG4C((LOG_NOTICE, "system time edited,soft exit!"));
  311. TerminateLastProcess();
  312. return 0;
  313. }
  314. }
  315. }
  316. //LOG4C((LOG_NOTICE, "nYear = %d, nMonth = %d, nDay = %d, nDogYear = %d, nDogMonth = %d, nDogDay = %d",
  317. // nYear, nMonth, nDay, nDogYear, nDogMonth, nDogDay));
  318. int nUsed = 0;
  319. if( nMonth > nDogMonth )
  320. {
  321. nUsed = ( nYear - nDogYear ) * 12 + ( nMonth - nDogMonth );
  322. }
  323. else
  324. {
  325. nUsed = ( nYear - nDogYear ) * 12 - nDogMonth + nMonth;
  326. }
  327. //LOG4C((LOG_NOTICE, "nUsed = %d, nUseDay = %d", nUsed, nUseDay));
  328. if( nUsed > nUseDay && 0 == g_nUseTimeLimit )
  329. {
  330. LOG4C((LOG_NOTICE, "狗过期 nUsed = %s,nUseDay = ",nUsed,nUseDay ));
  331. dwAddr = 13;
  332. dwBytes = 1;
  333. memset(szData, 0, sizeof(szData));
  334. szData[0] = 1;
  335. DogWrite( dwBytes, dwAddr, szData );
  336. g_nUseTimeLimit = 1;
  337. #if 0
  338. TerminateLastProcess();
  339. #endif
  340. return 0;
  341. }
  342. }
  343. }while( WaitForSingleObject(g_hRunObject, 5000L) == WAIT_TIMEOUT );
  344. return 0;
  345. }
  346. #if IS_RAND_WRITE_DOG
  347. DWORD WINAPI WriteDogThreadProc(LPVOID lpParameter)
  348. {
  349. INT iIvrIndex = 0;
  350. CString strError;
  351. for (; ;)
  352. {
  353. Sleep(10000);
  354. if (m_bWriteDogTerminate)
  355. break;
  356. char szData[10] ;
  357. DWORD dwAddr, dwBytes, dwData;
  358. DWORD dwStatus;
  359. srand((int)time(0));
  360. int nRand = random(8);
  361. dwAddr = 90 + nRand + 1;
  362. nRand = 99 - dwAddr;
  363. nRand = random( nRand );
  364. dwBytes = nRand + 1;
  365. szData[0] = dwAddr;
  366. szData[1] = dwBytes;
  367. szData[2] = 0;
  368. dwStatus = DogWrite( dwBytes, dwAddr, szData );
  369. if( dwStatus != 0 )
  370. {
  371. strError.LoadString(IDS_PROMPT_DOG_ERROR );
  372. strError.Format( strError, dwStatus );
  373. //AddToPrintQueue( MSG_WARNING, MSG_DOG_MD, (char*)(LPCTSTR)strError, strError.GetLength() );
  374. Sleep( 2000 );
  375. TerminateLastProcess();
  376. }
  377. }
  378. m_hWriteDogThread = NULL;
  379. m_dwWriteDogThreadId = 0;
  380. return 0;
  381. }
  382. #endif