CheckDog.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. CheckDogThreadStart();
  34. return 0;
  35. }
  36. int UnInitDog(void)
  37. {
  38. CheckDogThreadEnd();
  39. if( g_hDogLib != NULL )
  40. {
  41. AfxFreeLibrary(g_hDogLib);
  42. }
  43. return 0;
  44. }
  45. BOOL CheckDogThreadStart()
  46. {
  47. DWORD dwThreadId = 0;
  48. MTVERIFY( g_hReadDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadDogThreadProc, NULL, 0, &dwThreadId) );
  49. #if IS_RAND_WRITE_DOG
  50. MTVERIFY( g_hWriteDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteDogThreadProc, NULL, 0, &dwThreadId) );
  51. #endif
  52. return TRUE;
  53. }
  54. BOOL CheckDogThreadEnd()
  55. {
  56. // 关闭线程
  57. if (NULL != g_hReadDogThread)
  58. {
  59. MTVERIFY( WaitForSingleObject( g_hReadDogThread, INFINITE ) != WAIT_FAILED );
  60. MTVERIFY( CloseHandle( g_hReadDogThread ) );
  61. g_hReadDogThread = NULL;
  62. }
  63. #if IS_RAND_WRITE_DOG
  64. if (NULL != g_hWriteDogThread)
  65. {
  66. MTVERIFY( WaitForSingleObject( g_hWriteDogThread, INFINITE ) != WAIT_FAILED );
  67. MTVERIFY( CloseHandle( g_hWriteDogThread ) );
  68. g_hWriteDogThread = NULL;
  69. }
  70. #endif
  71. return TRUE;
  72. }
  73. DWORD WINAPI ReadDogThreadProc(LPVOID lpParameter)
  74. {
  75. do
  76. {
  77. DWORD dwStatus; // 读狗或写狗返回状态
  78. DWORD dwAddr; // 读狗或写狗的起始地址
  79. DWORD dwBytes; // 读狗或写狗的操作字节数据
  80. DWORD dwData = 0; // 写狗的数据内容
  81. char szData[10]; // 读狗的数据内容存放在这里,暂定为最多读10个字节
  82. memset(szData, 0, sizeof(szData));
  83. dwAddr = 6;
  84. dwBytes = 1;
  85. dwStatus = DogRead( dwBytes, dwAddr, szData );
  86. szData[1] = 0;
  87. if( szData[0] == (char)0xFA ) //250为正式版本,只检测公司信息
  88. {
  89. dwAddr = 0;
  90. dwBytes = 6;
  91. dwStatus = DogRead( dwBytes, dwAddr, szData );
  92. int nTmp = strcmp((char *)szData, "SToneU");
  93. if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
  94. {
  95. LOG4C((LOG_NOTICE, "read company information lost!"));
  96. TerminateLastProcess();
  97. return 0;
  98. }
  99. }
  100. else // 试用版本
  101. {
  102. dwAddr = 0;
  103. dwBytes = 6;
  104. dwStatus = DogRead( dwBytes, dwAddr, szData );
  105. int nTmp = strcmp((char *)szData, "SToneU");
  106. if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
  107. {
  108. LOG4C((LOG_NOTICE, "read company information lost!"));
  109. TerminateLastProcess();
  110. return 0;
  111. }
  112. // 是否已过使用期限
  113. dwAddr = 13;
  114. dwBytes = 1;
  115. memset(szData, 0, sizeof(szData));
  116. dwStatus = DogRead( dwBytes, dwAddr, szData );
  117. g_nUseTimeLimit = szData[0];
  118. #if 0
  119. if( 1 == g_nUseTimeLimit )
  120. {
  121. TerminateLastProcess();
  122. return 0;
  123. }
  124. #endif
  125. // 读取年
  126. dwAddr = 8;
  127. dwBytes = 2;
  128. memset(szData, 0, sizeof(szData));
  129. dwStatus = DogRead( dwBytes, dwAddr, szData );
  130. short nDogYear = *(short*)szData;
  131. // 读取月
  132. dwAddr = 10;
  133. dwBytes = 1;
  134. memset(szData, 0, sizeof(szData));
  135. dwStatus = DogRead( dwBytes, dwAddr, szData );
  136. int nDogMonth = szData[0];
  137. // 读取日
  138. dwAddr = 11;
  139. dwBytes = 1;
  140. memset(szData, 0, sizeof(szData));
  141. dwStatus = DogRead( dwBytes, dwAddr, szData );
  142. int nDogDay = szData[0];
  143. // 读取试用月数
  144. dwAddr = 7;
  145. dwBytes = 1;
  146. memset(szData, 0, sizeof(szData));
  147. dwStatus = DogRead( dwBytes, dwAddr, szData );
  148. int nUseDay = szData[0];
  149. CTime ctNow = CTime::GetCurrentTime();
  150. int nYear, nMonth, nDay;
  151. nYear = ctNow.GetYear();
  152. nMonth = ctNow.GetMonth();
  153. nDay = ctNow.GetDay();
  154. // 判断系统时间是否被修改
  155. if( nDogYear > nYear )
  156. {
  157. LOG4C((LOG_NOTICE, "system time edited!"));
  158. TerminateLastProcess();
  159. return 0;
  160. }
  161. else if( nDogYear == nYear )
  162. {
  163. if( nDogMonth > nMonth )
  164. {
  165. LOG4C((LOG_NOTICE, "system time edited!"));
  166. TerminateLastProcess();
  167. return 0;
  168. }
  169. else if( nDogMonth == nMonth )
  170. {
  171. if( nDogDay > nDay )
  172. {
  173. LOG4C((LOG_NOTICE, "system time edited!"));
  174. TerminateLastProcess();
  175. return 0;
  176. }
  177. }
  178. }
  179. //LOG4C((LOG_NOTICE, "nYear = %d, nMonth = %d, nDay = %d, nDogYear = %d, nDogMonth = %d, nDogDay = %d",
  180. // nYear, nMonth, nDay, nDogYear, nDogMonth, nDogDay));
  181. int nUsed = 0;
  182. if( nMonth > nDogMonth )
  183. {
  184. nUsed = ( nYear - nDogYear ) * 12 +
  185. ( nMonth - nDogMonth );
  186. }
  187. else
  188. {
  189. nUsed = ( nYear - nDogYear ) * 12 - nDogMonth + nMonth;
  190. }
  191. //LOG4C((LOG_NOTICE, "nUsed = %d, nUseDay = %d", nUsed, nUseDay));
  192. if( nUsed > nUseDay && 0 == g_nUseTimeLimit )
  193. {
  194. dwAddr = 13;
  195. dwBytes = 1;
  196. memset(szData, 0, sizeof(szData));
  197. szData[0] = 1;
  198. DogWrite( dwBytes, dwAddr, szData );
  199. g_nUseTimeLimit = 1;
  200. #if 0
  201. TerminateLastProcess();
  202. return 0;
  203. #endif
  204. }
  205. }
  206. }while( WaitForSingleObject(g_hRunObject, 5000L) == WAIT_TIMEOUT );
  207. return 0;
  208. }
  209. #if IS_RAND_WRITE_DOG
  210. DWORD WINAPI WriteDogThreadProc(LPVOID lpParameter)
  211. {
  212. INT iIvrIndex = 0;
  213. CString strError;
  214. for (; ;)
  215. {
  216. Sleep(10000);
  217. if (m_bWriteDogTerminate)
  218. break;
  219. char szData[10] ;
  220. DWORD dwAddr, dwBytes, dwData;
  221. DWORD dwStatus;
  222. srand((int)time(0));
  223. int nRand = random(8);
  224. dwAddr = 90 + nRand + 1;
  225. nRand = 99 - dwAddr;
  226. nRand = random( nRand );
  227. dwBytes = nRand + 1;
  228. szData[0] = dwAddr;
  229. szData[1] = dwBytes;
  230. szData[2] = 0;
  231. dwStatus = DogWrite( dwBytes, dwAddr, szData );
  232. if( dwStatus != 0 )
  233. {
  234. strError.LoadString(IDS_PROMPT_DOG_ERROR );
  235. strError.Format( strError, dwStatus );
  236. //AddToPrintQueue( MSG_WARNING, MSG_DOG_MD, (char*)(LPCTSTR)strError, strError.GetLength() );
  237. Sleep( 2000 );
  238. TerminateLastProcess();
  239. }
  240. }
  241. m_hWriteDogThread = NULL;
  242. m_dwWriteDogThreadId = 0;
  243. return 0;
  244. }
  245. #endif