123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- #include "stdafx.h"
- #include "CheckDog.h"
- #include <time.h> //头文件
- #include "resource.h"
- HANDLE g_hReadDogThread = NULL;
- #if IS_RAND_WRITE_DOG
- HANDLE g_hWriteDogThread = NULL;
- #endif
- DOGWRITE DogWrite = NULL;
- DOGREAD DogRead = NULL;
- HINSTANCE g_hDogLib;
- VOID TerminateLastProcess();
- //初始化软件狗
- int InitDog( void )
- {
- CHAR strFile[MAX_LIST_LENGTH + 1] = "";
- g_hDogLib = NULL;
- wsprintf(strFile, "%s\\Dll\\Win32dll.dll", g_strDirectory);
- g_hDogLib = AfxLoadLibrary(strFile);
- if (g_hDogLib < (HINSTANCE)HINSTANCE_ERROR)
- {
- LOG4C((LOG_NOTICE, "Load Dog Dll failed!"));
- return ERR_CODE_DOG_INIT_FAULT;
- }
- DogRead = (DOGREAD)GetProcAddress(g_hDogLib, _T("DogRead"));
- DogWrite = (DOGWRITE)GetProcAddress(g_hDogLib, _T("DogWrite"));
- if (DogRead == NULL || DogWrite == NULL)
- {
- LOG4C((LOG_NOTICE, "Get interface failed!"));
- AfxFreeLibrary(g_hDogLib);
- return ERR_CODE_DOG_INIT_FAULT;
- }
- CString sMsg;
- if( 0 != ReadDogInfo(sMsg) )
- {
- LOG4C((LOG_NOTICE, "%s", sMsg));
- g_sPassDueInfo = sMsg;
- return -1;
- }
- //CheckDogThreadStart();
- return 0;
- }
- int UnInitDog(void)
- {
- CheckDogThreadEnd();
- if( g_hDogLib != NULL )
- {
- AfxFreeLibrary(g_hDogLib);
- }
- return 0;
- }
- int ReadDogInfo( CString &sMsg )
- {
- DWORD dwStatus; // 读狗或写狗返回状态
- DWORD dwAddr; // 读狗或写狗的起始地址
- DWORD dwBytes; // 读狗或写狗的操作字节数据
- DWORD dwData = 0; // 写狗的数据内容
- char szData[10]; // 读狗的数据内容存放在这里,暂定为最多读10个字节
- memset(szData, 0, sizeof(szData));
- dwAddr = 6;
- dwBytes = 1;
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- szData[1] = 0;
- if( szData[0] == (char)0xFA ) //250为正式版本,只检测公司信息
- {
- sMsg = "狗的版本为正式版,谢谢使用!!";
- dwAddr = 0;
- dwBytes = 6;
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nTmp = strcmp((char *)szData, "SToneU");
- if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
- {
- sMsg = "read company information lost,soft exit!";
- g_nUseTimeLimit = 1;
- return -1;
- }
- }
- else // 试用版本
- {
- dwAddr = 0;
- dwBytes = 6;
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nTmp = strcmp((char *)szData, "SToneU");
- if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
- {
- sMsg = "read company information lost,soft exit!";
- g_nUseTimeLimit = 1;
- return -1;
- }
- //是否已过使用期限
- dwAddr = 13;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- g_nUseTimeLimit = szData[0];
- if( 1 == g_nUseTimeLimit )
- {
- sMsg = "狗已经过期了!!";
- g_nUseTimeLimit = 1;
- return -1;
- }
- // 读取年
- dwAddr = 8;
- dwBytes = 2;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- short nDogYear = *(short*)szData;
- // 读取月
- dwAddr = 10;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nDogMonth = szData[0];
- // 读取日
- dwAddr = 11;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nDogDay = szData[0];
- // 读取试用月数
- dwAddr = 7;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nUseDay = szData[0];
- CTime ctNow = CTime::GetCurrentTime();
- int nYear, nMonth, nDay;
- nYear = ctNow.GetYear();
- nMonth = ctNow.GetMonth();
- nDay = ctNow.GetDay();
- // 判断系统时间是否被修改
- if( nDogYear > nYear )
- {
- sMsg = "system time edited,soft exit!";
- g_nUseTimeLimit = 1;
- return -1;
- }
- else if( nDogYear == nYear )
- {
- if( nDogMonth > nMonth )
- {
- sMsg = "system time edited,soft exit!";
- g_nUseTimeLimit = 1;
- return -1;
- }
- else if( nDogMonth == nMonth )
- {
- if( nDogDay > nDay )
- {
- sMsg = "system time edited,soft exit!";
- g_nUseTimeLimit = 1;
- return -1;
- }
- }
- }
- int nUsed = 0;
- if( nMonth > nDogMonth )
- {
- nUsed = ( nYear - nDogYear ) * 12 + ( nMonth - nDogMonth );
- }
- else
- {
- nUsed = ( nYear - nDogYear ) * 12 - nDogMonth + nMonth;
- }
- if( nUsed > nUseDay && 0 == g_nUseTimeLimit )
- {
- sMsg.Format( "狗过期了 nUsed = %s,nUseDay = ",nUsed,nUseDay );
- dwAddr = 13;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- szData[0] = 1;
- DogWrite( dwBytes, dwAddr, szData );
- g_nUseTimeLimit = 1;
- return -1;
- }
- }
- return 0;
- }
- BOOL CheckDogThreadStart()
- {
- DWORD dwThreadId = 0;
- MTVERIFY( g_hReadDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadDogThreadProc, NULL, 0, &dwThreadId) );
- #if IS_RAND_WRITE_DOG
- MTVERIFY( g_hWriteDogThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteDogThreadProc, NULL, 0, &dwThreadId) );
- #endif
- return TRUE;
- }
- BOOL CheckDogThreadEnd()
- {
- // 关闭线程
- if (NULL != g_hReadDogThread)
- {
- MTVERIFY( WaitForSingleObject( g_hReadDogThread, INFINITE ) != WAIT_FAILED );
- MTVERIFY( CloseHandle( g_hReadDogThread ) );
- g_hReadDogThread = NULL;
- }
- #if IS_RAND_WRITE_DOG
- if (NULL != g_hWriteDogThread)
- {
- MTVERIFY( WaitForSingleObject( g_hWriteDogThread, INFINITE ) != WAIT_FAILED );
- MTVERIFY( CloseHandle( g_hWriteDogThread ) );
- g_hWriteDogThread = NULL;
- }
- #endif
- return TRUE;
- }
- DWORD WINAPI ReadDogThreadProc(LPVOID lpParameter)
- {
- do
- {
- DWORD dwStatus; // 读狗或写狗返回状态
- DWORD dwAddr; // 读狗或写狗的起始地址
- DWORD dwBytes; // 读狗或写狗的操作字节数据
- DWORD dwData = 0; // 写狗的数据内容
- char szData[10]; // 读狗的数据内容存放在这里,暂定为最多读10个字节
- memset(szData, 0, sizeof(szData));
- dwAddr = 6;
- dwBytes = 1;
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- szData[1] = 0;
- if( szData[0] == (char)0xFA ) //250为正式版本,只检测公司信息
- {
- LOG4C((LOG_NOTICE, "狗的版本为正式版,谢谢使用!!"));
- return 0;
- dwAddr = 0;
- dwBytes = 6;
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nTmp = strcmp((char *)szData, "SToneU");
- if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
- {
- LOG4C((LOG_NOTICE, "read company information lost,soft exit!"));
- TerminateLastProcess();
- return 0;
- }
- }
- else // 试用版本
- {
- dwAddr = 0;
- dwBytes = 6;
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nTmp = strcmp((char *)szData, "SToneU");
- if( (dwStatus != 0) || ( nTmp != 0 ) ) // 读公司信息错误,则退出
- {
- LOG4C((LOG_NOTICE, "read company information lost,soft exit!"));
- TerminateLastProcess();
- return 0;
- }
- // 是否已过使用期限
- dwAddr = 13;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- g_nUseTimeLimit = szData[0];
- #if 0
- TerminateLastProcess();
- #endif
- if( 1 == g_nUseTimeLimit )
- {
- LOG4C((LOG_NOTICE, "狗已经过期,谢谢使用!!"));
- return 0;
- }
- // 读取年
- dwAddr = 8;
- dwBytes = 2;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- short nDogYear = *(short*)szData;
- // 读取月
- dwAddr = 10;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nDogMonth = szData[0];
- // 读取日
- dwAddr = 11;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nDogDay = szData[0];
- // 读取试用月数
- dwAddr = 7;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- dwStatus = DogRead( dwBytes, dwAddr, szData );
- int nUseDay = szData[0];
- CTime ctNow = CTime::GetCurrentTime();
- int nYear, nMonth, nDay;
- nYear = ctNow.GetYear();
- nMonth = ctNow.GetMonth();
- nDay = ctNow.GetDay();
- // 判断系统时间是否被修改
- if( nDogYear > nYear )
- {
- LOG4C((LOG_NOTICE, "system time edited,soft exit!"));
- TerminateLastProcess();
- return 0;
- }
- else if( nDogYear == nYear )
- {
- if( nDogMonth > nMonth )
- {
- LOG4C((LOG_NOTICE, "system time edited,soft exit!"));
- TerminateLastProcess();
- return 0;
- }
- else if( nDogMonth == nMonth )
- {
- if( nDogDay > nDay )
- {
- LOG4C((LOG_NOTICE, "system time edited,soft exit!"));
- TerminateLastProcess();
- return 0;
- }
- }
- }
- //LOG4C((LOG_NOTICE, "nYear = %d, nMonth = %d, nDay = %d, nDogYear = %d, nDogMonth = %d, nDogDay = %d",
- // nYear, nMonth, nDay, nDogYear, nDogMonth, nDogDay));
- int nUsed = 0;
- if( nMonth > nDogMonth )
- {
- nUsed = ( nYear - nDogYear ) * 12 + ( nMonth - nDogMonth );
- }
- else
- {
- nUsed = ( nYear - nDogYear ) * 12 - nDogMonth + nMonth;
- }
- //LOG4C((LOG_NOTICE, "nUsed = %d, nUseDay = %d", nUsed, nUseDay));
- if( nUsed > nUseDay && 0 == g_nUseTimeLimit )
- {
- LOG4C((LOG_NOTICE, "狗过期 nUsed = %s,nUseDay = ",nUsed,nUseDay ));
- dwAddr = 13;
- dwBytes = 1;
- memset(szData, 0, sizeof(szData));
- szData[0] = 1;
- DogWrite( dwBytes, dwAddr, szData );
- g_nUseTimeLimit = 1;
- #if 0
- TerminateLastProcess();
- #endif
- return 0;
- }
- }
- }while( WaitForSingleObject(g_hRunObject, 5000L) == WAIT_TIMEOUT );
- return 0;
- }
- #if IS_RAND_WRITE_DOG
- DWORD WINAPI WriteDogThreadProc(LPVOID lpParameter)
- {
- INT iIvrIndex = 0;
- CString strError;
- for (; ;)
- {
- Sleep(10000);
- if (m_bWriteDogTerminate)
- break;
- char szData[10] ;
- DWORD dwAddr, dwBytes, dwData;
- DWORD dwStatus;
- srand((int)time(0));
- int nRand = random(8);
- dwAddr = 90 + nRand + 1;
- nRand = 99 - dwAddr;
- nRand = random( nRand );
- dwBytes = nRand + 1;
- szData[0] = dwAddr;
- szData[1] = dwBytes;
- szData[2] = 0;
- dwStatus = DogWrite( dwBytes, dwAddr, szData );
- if( dwStatus != 0 )
- {
- strError.LoadString(IDS_PROMPT_DOG_ERROR );
- strError.Format( strError, dwStatus );
- //AddToPrintQueue( MSG_WARNING, MSG_DOG_MD, (char*)(LPCTSTR)strError, strError.GetLength() );
- Sleep( 2000 );
- TerminateLastProcess();
- }
- }
- m_hWriteDogThread = NULL;
- m_dwWriteDogThreadId = 0;
- return 0;
- }
- #endif
|