CheckDog.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. CheckDogThreadStart();
  29. return 0;
  30. }
  31. else
  32. {
  33. LOG4C((LOG_NOTICE,"已注册,正常使用."));
  34. return 0;
  35. }
  36. //return 0;
  37. }
  38. int UnInitDog(void)
  39. {
  40. CheckDogThreadEnd();
  41. return 0;
  42. }
  43. BOOL CheckDogThreadStart()
  44. {
  45. DWORD dwThreadId = 0;
  46. MTVERIFY( g_hReadDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadDogThreadProc, NULL, 0, &dwThreadId) );
  47. #if IS_RAND_WRITE_DOG
  48. MTVERIFY( g_hWriteDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteDogThreadProc, NULL, 0, &dwThreadId) );
  49. #endif
  50. return TRUE;
  51. }
  52. BOOL CheckDogThreadEnd()
  53. {
  54. // 关闭线程
  55. if (NULL != g_hReadDogThread)
  56. {
  57. MTVERIFY( WaitForSingleObject( g_hReadDogThread, INFINITE ) != WAIT_FAILED );
  58. MTVERIFY( CloseHandle( g_hReadDogThread ) );
  59. g_hReadDogThread = NULL;
  60. }
  61. #if IS_RAND_WRITE_DOG
  62. if (NULL != g_hWriteDogThread)
  63. {
  64. MTVERIFY( WaitForSingleObject( g_hWriteDogThread, INFINITE ) != WAIT_FAILED );
  65. MTVERIFY( CloseHandle( g_hWriteDogThread ) );
  66. g_hWriteDogThread = NULL;
  67. }
  68. #endif
  69. return TRUE;
  70. }
  71. DWORD WINAPI ReadDogThreadProc(LPVOID lpParameter)
  72. {
  73. char szSNName[MAX_PATH] = {0};
  74. sprintf(szSNName,"%s\\register.sn",g_strDirectory);
  75. CTimeSpan ts = NULL;
  76. CTime t_et = NULL;
  77. CTime t_ct = CTime::GetCurrentTime();
  78. CFileStatus Sfile;
  79. CSNRegister snrig;
  80. do
  81. {
  82. if (!CFile::GetStatus(szSNName,Sfile))
  83. {
  84. LOG4C((LOG_NOTICE,"SN文件不存在"));
  85. TerminateLastProcess();
  86. return 0;
  87. }
  88. if(!snrig.itf_IsCorrectSN(szSNName))
  89. {
  90. LOG4C((LOG_NOTICE,"SN文件无效"));
  91. TerminateLastProcess();
  92. return 0;
  93. }
  94. if ((long)snrig.itf_returnLeftTime() < 0){
  95. LOG4C((LOG_NOTICE,"试用期限到"));
  96. TerminateLastProcess();
  97. return 0;
  98. }
  99. t_et = CTime::GetCurrentTime();
  100. ts = t_et - t_ct;
  101. if (ts.GetMinutes() > 5)
  102. {
  103. t_ct = t_et;
  104. snrig.itf_SavebyLTime(snrig.itf_returnLeftTime() - ts.GetMinutes(),szSNName);
  105. LOG4C((LOG_NOTICE,"时差 = %d",ts.GetMinutes()));
  106. }
  107. }while( WaitForSingleObject(g_hRunObject, 10000L) == WAIT_TIMEOUT );
  108. return 0;
  109. }
  110. #if IS_RAND_WRITE_DOG
  111. DWORD WINAPI WriteDogThreadProc(LPVOID lpParameter)
  112. {
  113. INT iIvrIndex = 0;
  114. CString strError;
  115. for (; ;)
  116. {
  117. Sleep(10000);
  118. }
  119. return 0;
  120. }
  121. #endif