CheckDog.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "stdafx.h"
  2. #include "CheckDog.h"
  3. #include "SNRegister.h"
  4. #include <time.h> //头文件
  5. #include "resource.h"
  6. HANDLE g_hReadDogThread = NULL;
  7. #if IS_RAND_WRITE_DOG
  8. HANDLE g_hWriteDogThread = NULL;
  9. #endif
  10. VOID TerminateLastProcess();
  11. // 初始化软件狗;
  12. int InitDog( void )
  13. {
  14. CHAR szSNName[MAX_LIST_LENGTH + 1] = "";
  15. wsprintf(szSNName, "%s\\register.sn", g_strDirectory);
  16. CFileStatus Sfile;
  17. if (CFile::GetStatus(szSNName,Sfile) == FALSE) {
  18. LOG4C((LOG_NOTICE,"SN文件不存在"));
  19. return -1;
  20. }
  21. CSNRegister snrig;
  22. if ( !snrig.itf_IsCorrectSN(szSNName)) {
  23. LOG4C((LOG_NOTICE,"无效SN文件"));
  24. return -1;
  25. }
  26. if ( strcmp(snrig.itf_returnSnCode(),g_strSNCode) != 0) {
  27. LOG4C((LOG_NOTICE,"末注册,试用."));
  28. // 试用前,判断注册表的剩余时间与校验码,是否与SN文件相同,防止利用备份SN文件获得长期使用;
  29. // 首次运行,创建注册信息;
  30. CheckDogThreadStart();
  31. return 0;
  32. }
  33. else{
  34. LOG4C((LOG_NOTICE,"已注册,正常使用."));
  35. return 0;
  36. }
  37. }
  38. int IsCollectCheck(CSNRegister &snrig,HWND hWnd)
  39. {
  40. HKEY hKey = NULL;
  41. CHAR strKeyName[50] = "";
  42. char strTimeKey[9] = ""; // 密文密钥;
  43. CHAR strLeftTime[50] = ""; // 剩余时间密文;
  44. char strCheckKey[9] = ""; // 密文密钥;
  45. CHAR strCheckCode[50] = ""; // 校验码密文;
  46. DWORD dwErrorCode = 0;
  47. INT iNumberOfDays = 0;
  48. DWORD dwSize = 0;
  49. DWORD dwType = 0;
  50. // 创建注册表,有该键则读取,无则创建;
  51. strcpy(strKeyName, "Software\\try to play");
  52. dwErrorCode = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  53. strKeyName,
  54. 0,
  55. NULL,
  56. REG_OPTION_NON_VOLATILE,
  57. KEY_ALL_ACCESS,
  58. NULL, //Security
  59. &hKey,
  60. NULL);
  61. dwType = REG_SZ;
  62. if (dwErrorCode == ERROR_SUCCESS)
  63. {
  64. // =================剩余时间明文获取=======================;
  65. dwSize = sizeof(strLeftTime);
  66. dwErrorCode = ::RegQueryValueEx(hKey, "DataT",NULL,&dwType,(BYTE*)strLeftTime, &dwSize );
  67. char szTemp[1024] = {0};
  68. ltoa(snrig.itf_returnLeftTime(),szTemp,10);
  69. // 获取明文剩余时间;
  70. if (dwErrorCode == ERROR_SUCCESS && strcmp(strLeftTime, ""))
  71. {
  72. // 首次使用,写入剩余时间明文;
  73. while( ::RegSetValueEx(hKey, "DataT", NULL, dwType, (BYTE *)szTemp, dwSize) );
  74. //dwErrorCode = RegSetValueEx(hKey, "DataT", NULL, REG_SZ, (BYTE *)szTemp, &dwSize);
  75. }
  76. else
  77. {
  78. // 已使用,读取剩余时间明文;
  79. if (strcmp(strLeftTime, szTemp) != 0)
  80. {
  81. if (NULL != hKey)
  82. RegCloseKey(hKey);
  83. hKey = NULL;
  84. return -1;
  85. }
  86. }
  87. // ======================校验码明文获取=========================;
  88. dwSize = sizeof(strCheckCode);
  89. dwErrorCode = ::RegQueryValueEx(hKey, "DataC",NULL,&dwType,(BYTE*)strCheckCode, &dwSize );
  90. memset(szTemp,0,1024);
  91. memcpy(szTemp, snrig.itf_returnSnCode(), strlen(snrig.itf_returnSnCode()) );
  92. // 获取明文剩余时间;
  93. if (dwErrorCode == ERROR_SUCCESS && strcmp(strCheckCode, ""))
  94. {
  95. // 首次使用,写入剩余时间明文;
  96. while( ::RegSetValueEx(hKey, "DataT", NULL, dwType, (BYTE *)szTemp, dwSize) );
  97. //dwErrorCode = RegSetValueEx(hKey, "DataT", NULL, REG_SZ, (BYTE *)szTemp, &dwSize);
  98. }
  99. else
  100. {
  101. // 已使用,读取剩余时间明文;
  102. if (strcmp(strCheckCode, szTemp) != 0)
  103. {
  104. if (NULL != hKey)
  105. RegCloseKey(hKey);
  106. hKey = NULL;
  107. return -1;
  108. }
  109. }
  110. }
  111. if (NULL != hKey)
  112. RegCloseKey(hKey);
  113. hKey = NULL;
  114. return 0;
  115. }
  116. int IsCollectLTime(CSNRegister &snrig)
  117. {
  118. return 0;
  119. }
  120. int UnInitDog(void)
  121. {
  122. CheckDogThreadEnd();
  123. return 0;
  124. }
  125. BOOL CheckDogThreadStart()
  126. {
  127. DWORD dwThreadId = 0;
  128. MTVERIFY( g_hReadDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadDogThreadProc, NULL, 0, &dwThreadId) );
  129. #if IS_RAND_WRITE_DOG
  130. MTVERIFY( g_hWriteDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteDogThreadProc, NULL, 0, &dwThreadId) );
  131. #endif
  132. return TRUE;
  133. }
  134. BOOL CheckDogThreadEnd()
  135. {
  136. // 关闭线程;
  137. if (NULL != g_hReadDogThread)
  138. {
  139. MTVERIFY( WaitForSingleObject( g_hReadDogThread, INFINITE ) != WAIT_FAILED );
  140. MTVERIFY( CloseHandle( g_hReadDogThread ) );
  141. g_hReadDogThread = NULL;
  142. }
  143. #if IS_RAND_WRITE_DOG
  144. if (NULL != g_hWriteDogThread)
  145. {
  146. MTVERIFY( WaitForSingleObject( g_hWriteDogThread, INFINITE ) != WAIT_FAILED );
  147. MTVERIFY( CloseHandle( g_hWriteDogThread ) );
  148. g_hWriteDogThread = NULL;
  149. }
  150. #endif
  151. return TRUE;
  152. }
  153. DWORD WINAPI ReadDogThreadProc(LPVOID lpParameter)
  154. {
  155. LOG4C((LOG_NOTICE,"线程运行"));
  156. char szSNName[MAX_PATH] = {0};
  157. sprintf(szSNName,"%s\\register.sn",g_strDirectory);
  158. CTimeSpan ts = NULL;
  159. CTime t_et = NULL;
  160. CTime t_ct = CTime::GetCurrentTime();
  161. CFileStatus Sfile;
  162. CSNRegister snrig;
  163. do
  164. {
  165. if (!CFile::GetStatus(szSNName,Sfile)){
  166. LOG4C((LOG_NOTICE,"SN文件不存在"));
  167. TerminateLastProcess();
  168. return 0;
  169. }
  170. if(!snrig.itf_IsCorrectSN(szSNName)){
  171. LOG4C((LOG_NOTICE,"SN文件无效"));
  172. TerminateLastProcess();
  173. return 0;
  174. }
  175. if ((long)snrig.itf_returnLeftTime() < 0){
  176. LOG4C((LOG_NOTICE,"试用期限到"));
  177. TerminateLastProcess();
  178. return 0;
  179. }
  180. t_et = CTime::GetCurrentTime();
  181. ts = t_et - t_ct;
  182. if (ts.GetSeconds() > 1){
  183. t_ct = t_et;
  184. snrig.itf_SavebyLTime(snrig.itf_returnLeftTime() - ts.GetSeconds(),szSNName);
  185. LOG4C((LOG_NOTICE,"时差 = %d",ts.GetSeconds()));
  186. }
  187. }while( WaitForSingleObject(g_hRunObject, 5000L) == WAIT_TIMEOUT );
  188. return 0;
  189. }
  190. #if IS_RAND_WRITE_DOG
  191. DWORD WINAPI WriteDogThreadProc(LPVOID lpParameter)
  192. {
  193. INT iIvrIndex = 0;
  194. CString strError;
  195. for (; ;){
  196. Sleep(10000);
  197. }
  198. return 0;
  199. }
  200. #endif