Global.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. #pragma once
  2. #include "stdafx.h"
  3. #include <tlhelp32.h>
  4. #include "global.h"
  5. #define MAX_PATH_LENGTH MAX_PATH
  6. CRITICAL_SECTION g_csWrDataBase; //写日志和告警数据库操作
  7. //程序退出事件
  8. HANDLE g_hRunObject = NULL;
  9. //客户端数据
  10. INT g_nClientNum = 0;
  11. char g_strDirectory[256]={0};
  12. //DOG
  13. INT g_nUseTimeLimit = 0; //是否超过使用期限
  14. //WatchServer
  15. CHAR g_strWatchServerIP[MAX_EQUIP_IP + 1] = {0};
  16. CHAR g_strWatchServerPort[MAX_PORT + 1] = {0};
  17. //Database
  18. CHAR g_strConnectString[MAX_PATH + 1] = ""; // 数据连接字符串
  19. CHAR g_strDBType[MAX_SERVER_LENGTH + 1] = ""; // 数据库连接类型(如: SQL SERVER 、Access )
  20. CHAR g_strAccessFile[MAX_PATH_LENGTH + 1] = ""; // 如果是用Access数据库:Access文件名称
  21. // 如用其它数据库:为空
  22. CHAR g_strServerName[MAX_SERVER_LENGTH + 1] = ""; // 数据库服务器名称或IP
  23. CHAR g_strDataBaseName[MAX_DATABASE_LENGTH + 1] = ""; // 数据库名称
  24. CHAR g_strUserName[MAX_USER_LENGTH + 1] = ""; // 数据库用户名
  25. CHAR g_strPassword[MAX_PASSWORD_LENGTH + 1] = ""; // 数据库密码
  26. //用户信息
  27. CHAR g_strLoginUserID[MAX_USER_LENGTH + 1] = ""; // 当前使用该软件的用户名
  28. CHAR g_strLoginPwd[MAX_PASSWORD_LENGTH + 1] = ""; // 当前使用该软件的密码
  29. CHAR g_strDefaultUserID[MAX_USER_LENGTH + 1] = {0};
  30. CHAR g_strDefaultUserPwd[MAX_PASSWORD_LENGTH + 1] = {0};
  31. //系统设置
  32. BOOL g_bEnableTimeLimited = FALSE; // TRUE:时间限制 FALSE:正式版
  33. INT g_iAutoRunSystem = 0; // 是否开机自启动该系统
  34. CHAR g_strLanguage[MAX_LANG_LENGTH + 1]={0}; // 默认语言
  35. //日志
  36. //CHAR g_strDirectory[MAX_PATH_LENGTH + 1]; // 软件所在路径
  37. CHAR g_strAppName[MAX_PATH_LENGTH + 1] = {0}; // 软件名称
  38. CHAR g_strSummaryLogPath[MAX_PATH_LENGTH + 1] = {0}; // 日志存放目录
  39. //环境参数配置
  40. CHAR g_strSNCode[MAX_PATH];
  41. INT g_nLevelWav = 0; // 语音通知级别
  42. INT g_nLevelSms = 0; // 发送短信级别
  43. INT g_nLevelEmail = 0; // 发送Email级别
  44. INT g_nLevelDevInterrupt = 0; // 通讯中断级别
  45. INT g_nLevelDlg = 0; // 本地报警提示框级别
  46. INT g_nLevelLocalWav = 0; // 本地声音提示级别
  47. INT g_nDBRecordDate = 0; // 数据库记录时间
  48. //Sms
  49. INT g_nSendSms = 0; // 是否启动短信报警功能
  50. CHAR g_strSmsComPort[MAX_TEL_LENGTH + 1] = {0}; // 短信猫端口
  51. INT g_nSmsRate = 0; // 串口波特率
  52. INT g_nSmsDataBit = 8; // 数据位
  53. INT g_nSmsStopBit = 1; // 停止位
  54. INT g_nSmsParity = 0; // 检验位
  55. INT g_nSmsInterval = 100; // 间隔时间
  56. INT g_nSmsLanguageTrans = 0; // 是否需要繁简转换
  57. INT g_nSmsMaxChar = 70; // 短信猫支持最大字符个数
  58. INT g_nSmsMakeCall = 0; // 发送短信后,是否拔打电话提醒 本来是1,chn modify
  59. CHAR g_strSmsSMSC[MAX_TEL_LENGTH + 1] = {0}; // smsc号码
  60. INT g_nSmsTimOut = 0; // 短信发送超时
  61. INT g_nSmsCallTimes = 0;
  62. //Email
  63. INT g_nSendEmail = 0; // 是否启动Email报警功能
  64. CHAR g_strEmailSMTPSrv[MAX_MAILSERVER_LENGTH]={0}; // SMTPServer地址
  65. INT g_nEmailSMTPPort = 0; // SMTP端口
  66. INT g_nEmailIsNeed = 0; // 是否需要身份验证
  67. CHAR g_strEmailUserAcc[MAX_USER_LENGTH]={0}; // 用户名
  68. CHAR g_strEmailUserPwd[MAX_PASSWORD_LENGTH]={0}; // 密码
  69. INT g_nEmailTimeOut = 0; // 发送邮件超时
  70. CHAR g_strEmailFromAddr[MAX_MAILTO_LENGTH]={0}; // 发信人Email地址
  71. CHAR g_strEmailSubject[MAX_EMAIL_LENGTH]={0}; // Email标题
  72. INT g_nEmailCallTimes = 0;
  73. //语音通知
  74. INT g_nSendNotice = 0; // 是否启动语音通知功能
  75. INT g_nNoticeCardType = 1; // 板卡类型
  76. CHAR g_strNoticeTelPre[MAX_TEL_LENGTH + 1] = {0}; // 外线前缀
  77. INT g_nNoticeCallDelay = 0; // 两次呼叫间隔时间
  78. INT g_nNoticeCallTimes = 0; // 最大的呼叫次数
  79. //TTS参数配置
  80. INT g_nTTSType = 5; // TTS格式类型
  81. // 报警参数设置
  82. INT g_nAlarmModeIsDlg = 0; // 是否弹出报警提示框
  83. INT g_nAlarmModeShowDlgTimes = 0; // 提示框显示次数
  84. INT g_nAlarmModeIsSound = 0; // 是否本地播放报警提示音
  85. INT g_nAlarmModeSoundNum = 0; // 声音播放次数,0:代表无限播放,直到报警被确认
  86. //定时发送短信和电话参数配置
  87. INT g_nTimingEnable = 0;
  88. INT g_nTimingWeeks = 0;
  89. INT g_nTimingHours = 0;
  90. INT g_nTimingMinutes = 0;
  91. //是否启用Snmp参数配置
  92. INT g_nSnmpEnable = 0;
  93. CHAR g_strSnmpObjOid[MAX_OBJ_OID + 1] = {0};
  94. CHAR g_strSnmpIP[MAX_EQUIP_IP + 1] = {0};
  95. CHAR g_strSnmpField[MAX_EQUIP_DESC + 1] = {0};
  96. WORD AsciiToBYTE(BYTE btSrc);
  97. char ByteToAscii(BYTE btSrc);
  98. /*字符全转为大写*/
  99. void ByteToUpper(char *szMsg)
  100. {
  101. for (int i=0; i< strlen(szMsg); i++)
  102. {
  103. if ((szMsg[i]>='a') && (szMsg[i]<='z'))
  104. {
  105. szMsg[i] = szMsg[i] - 32;
  106. }
  107. else
  108. {
  109. szMsg[i] = szMsg[i];
  110. }
  111. }
  112. }
  113. WORD AsciiToBYTE(BYTE btSrc)
  114. {
  115. WORD chDest = (WORD)btSrc;
  116. if ((btSrc >= 'A')&&(btSrc <= 'F'))
  117. {
  118. chDest = chDest - 'A' + 10;
  119. }
  120. else if ((btSrc >= 'a')&&(btSrc <= 'f'))
  121. {
  122. chDest = chDest - 'a' + 10;
  123. }
  124. else if ((btSrc >= '0')&&(btSrc <= '9'))
  125. {
  126. chDest -= '0';
  127. }
  128. return chDest;
  129. }
  130. char lowercase2uppercase(BYTE btSrc)
  131. {
  132. if( btSrc >= 'a' && btSrc <= 'z' )
  133. {
  134. return btSrc - 'a' + 'A';
  135. }
  136. return btSrc;
  137. }
  138. char ByteToAscii(BYTE btSrc)
  139. {
  140. char chDest;
  141. if( btSrc < 10 )
  142. {
  143. chDest = (char)(btSrc % 10 + '0');
  144. chDest = lowercase2uppercase(chDest);
  145. return chDest;
  146. }
  147. else
  148. {
  149. chDest = ByteToAscii( btSrc / 10 ) + (char)( btSrc % 10 + '0' );
  150. chDest = lowercase2uppercase(chDest);
  151. return chDest;
  152. }
  153. }
  154. int DigitToBinary(WORD wdSource, char* pDes, int iBit)
  155. {
  156. char pTmpBuf[17] = {0};
  157. char chBuffer[17] = {0};
  158. //wdSource =htonl(wdSource);
  159. itoa(wdSource, pTmpBuf, 2);
  160. int iLen = strlen(pTmpBuf) - 1;
  161. char chValue[17] = {0};
  162. strcpy(chValue, pTmpBuf);
  163. for (int i =0; i<=iLen; i++)
  164. {
  165. pTmpBuf[i] = chValue[iLen - i];
  166. }
  167. for (int k = 0; k<iBit; k++)
  168. {
  169. if ( 0x00 == pTmpBuf[iBit - k - 1])
  170. chBuffer[k] = 0x30;
  171. else
  172. chBuffer[k] = pTmpBuf[iBit - k - 1];
  173. }
  174. memcpy(pDes, chBuffer, iBit);
  175. return 0;
  176. }
  177. int StrUpper(char *pSource, char *pDest)
  178. {
  179. int length, i;
  180. length = strlen(pSource);
  181. for (i = 0; i < length; i++) {
  182. if ((pSource[i] >= 'a') && (pSource[i] <= 'z')){
  183. pDest[i] = _toupper(pSource[i]);
  184. }
  185. else
  186. pDest[i] = pSource[i];
  187. }
  188. return 0;
  189. }
  190. int StrLower(char *pSource, char *pDest)
  191. {
  192. int length, i;
  193. length = strlen(pSource);
  194. for (i = 0; i < length; i++) {
  195. if ((pSource[i] >= 'A') && (pSource[i] <= 'Z')){
  196. pDest[i] = _tolower(pSource[i]);
  197. }
  198. else
  199. pDest[i] = pSource[i];
  200. }
  201. return 0;
  202. }
  203. char Hex16(char WillChangeNum[]) //该函数把四位二进制转换成十六进制数
  204. {
  205. int i;
  206. i = (WillChangeNum[3]) + (WillChangeNum[2] * 10) +
  207. (WillChangeNum[1] * 100) + (WillChangeNum[0] * 1000);
  208. switch(i)
  209. {
  210. case 0:
  211. return 0;
  212. case 1:
  213. return 1;
  214. case 10:
  215. return 2;
  216. case 11:
  217. return 3;
  218. case 100:
  219. return 4;
  220. case 101:
  221. return 5;
  222. case 110:
  223. return 6;
  224. case 111:
  225. return 7;
  226. case 1000:
  227. return 8;
  228. case 1001:
  229. return 9;
  230. case 1010:
  231. return 0x0A;
  232. case 1011:
  233. return 0x0B;
  234. case 1100:
  235. return 0x0C;
  236. case 1101:
  237. return 0x0D;
  238. case 1110:
  239. return 0x0E;
  240. case 1111:
  241. return 0x0F;
  242. }
  243. return -1;
  244. }
  245. BYTE TwoByteToByte(BYTE b1, BYTE b2)//两个字符转换为一个字符
  246. {
  247. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  248. BYTE b = 0;
  249. if(b1 >= 'A' && b1 <= 'F')
  250. b += (b1 - 'A' + 10)*16;
  251. else if(b1 >= 'a' && b1 <= 'f')
  252. b += (b1 - 'a' + 10)*16;
  253. else
  254. b += (b1 - '0')*16;
  255. if(b2 >= 'A' && b2 <= 'F')
  256. b += b2 - 'A' + 10;
  257. else if(b2 >= 'a' && b2 <= 'f')
  258. b += b2 - 'a' + 10;
  259. else
  260. b += b2 - '0';
  261. return b;
  262. }
  263. void ByteToTwoByte(BYTE b, BYTE b2[])//一个字符转换为两个字符
  264. {
  265. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  266. char szBuf[3];
  267. int nData = b;
  268. sprintf(szBuf, "%02X", nData);
  269. memcpy(b2, szBuf, 2);
  270. }
  271. int GetSysData()
  272. {
  273. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  274. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  275. int iPosFile = 0;
  276. int iLenghtFile = 0;
  277. ////////////////////////////////////////////////////////////////////////////
  278. //获取应用名
  279. //GetModuleFileName(AfxGetApp()->m_hInstance, g_strDirectory, MAX_PATH);
  280. GetModuleFileName(NULL, g_strDirectory, MAX_PATH);
  281. iLenghtFile = (int)strlen(g_strDirectory);
  282. for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--)
  283. {
  284. if (g_strDirectory[iPosFile] == '\\')
  285. {
  286. memset(g_strAppName, 0, sizeof(g_strAppName));
  287. memcpy(g_strAppName, g_strDirectory + iPosFile + 1, iLenghtFile - iPosFile - 1);
  288. break;
  289. }
  290. }
  291. iLenghtFile = strlen(g_strDirectory);
  292. for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--)
  293. {
  294. if (g_strDirectory[iPosFile] == '\\')
  295. {
  296. g_strDirectory[iPosFile] = '\0';
  297. break;
  298. }
  299. }
  300. wsprintf(strFile, "%s\\set.ini", g_strDirectory);
  301. //是否自动启动系统
  302. GetPrivateProfileString("Other", "AutoRunSystem", "", strValue, sizeof(strValue), strFile);
  303. if (strcmp(strValue, ""))
  304. g_iAutoRunSystem = atoi(strValue);
  305. else
  306. g_iAutoRunSystem = 0;
  307. if (g_iAutoRunSystem <= 0)
  308. g_iAutoRunSystem = 0;
  309. else
  310. g_iAutoRunSystem = 1;
  311. //语言
  312. GetPrivateProfileString("Other", "Language", "", g_strLanguage, sizeof(g_strLanguage), strFile);
  313. //Database
  314. GetPrivateProfileString("Database", "DBType", "", g_strDBType, sizeof(g_strDBType), strFile);
  315. GetPrivateProfileString("Database", "AccessFile", "", g_strAccessFile, sizeof(g_strAccessFile), strFile);
  316. GetPrivateProfileString("Database", "ServerName", "", g_strServerName, sizeof(g_strServerName), strFile);
  317. GetPrivateProfileString("Database", "DataBaseName", "", g_strDataBaseName, sizeof(g_strDataBaseName), strFile);
  318. GetPrivateProfileString("Database", "UserName", "", g_strUserName, sizeof(g_strUserName), strFile);
  319. GetPrivateProfileString("Database", "Password", "", g_strPassword, sizeof(g_strPassword), strFile);
  320. //数据库连接串
  321. if (!_stricmp(g_strDBType, "SQL SERVER"))
  322. sprintf(g_strConnectString, "Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
  323. g_strServerName, g_strDataBaseName, g_strUserName, g_strPassword);
  324. else if (!_stricmp(g_strDBType, "ACCESS97"))
  325. sprintf(g_strConnectString, "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=%s", g_strAccessFile);
  326. else if (!_stricmp(g_strDBType, "ACCESS2000"))
  327. sprintf(g_strConnectString, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s", g_strAccessFile);
  328. else if(!_stricmp(g_strDBType, "PGSQL"))
  329. sprintf(g_strConnectString, "DRIVER={PostgreSQL ODBC Driver(UNICODE)}; SERVER=%s; port=5432; DATABASE=%s; UID=%s; PWD=%s;",
  330. g_strServerName, g_strDataBaseName, g_strUserName, g_strPassword);
  331. else
  332. sprintf(g_strConnectString, "Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
  333. g_strServerName, g_strDataBaseName, g_strUserName, g_strPassword);
  334. //Common
  335. g_nLevelWav = GetPrivateProfileInt("Common", "LevelWav", 3, strFile);
  336. g_nLevelSms = GetPrivateProfileInt("Common", "LevelSms", 3, strFile);
  337. g_nLevelEmail = GetPrivateProfileInt("Common", "LevelEmail", 3, strFile);
  338. g_nLevelDevInterrupt = GetPrivateProfileInt("Common", "LevelDevInterrupt", 8, strFile);
  339. g_nLevelDlg = GetPrivateProfileInt("Common", "LevelDlg", 1, strFile);
  340. g_nLevelLocalWav = GetPrivateProfileInt("Common", "LevelLocalWav", 1, strFile);
  341. g_nDBRecordDate = GetPrivateProfileInt("Common", "DbRecordDate", 365, strFile);
  342. //Email
  343. g_nSendEmail = GetPrivateProfileInt("Email", "SendEmail", 0, strFile);
  344. GetPrivateProfileString("Email", "SMTPServer", "", g_strEmailSMTPSrv, sizeof(g_strEmailSMTPSrv), strFile);
  345. g_nEmailSMTPPort = GetPrivateProfileInt("Email", "SMTPPort", 25, strFile);
  346. g_nEmailIsNeed = GetPrivateProfileInt("Email", "IsNeed", 1, strFile);
  347. GetPrivateProfileString("Email", "UserAcc", "", g_strEmailUserAcc, sizeof(g_strEmailUserAcc), strFile);
  348. GetPrivateProfileString("Email", "UserPwd", "", g_strEmailUserPwd, sizeof(g_strEmailUserPwd), strFile);
  349. GetPrivateProfileString("Email", "MailFrom", "", g_strEmailFromAddr, sizeof(g_strEmailFromAddr), strFile);
  350. g_nEmailTimeOut = GetPrivateProfileInt("Email", "TimeOut", 60, strFile);
  351. GetPrivateProfileString("Email", "Subject", "", g_strEmailSubject, sizeof(g_strEmailSubject), strFile);
  352. g_nEmailCallTimes = GetPrivateProfileInt("Email", "ReCallTimes", 2, strFile);
  353. //Sms
  354. g_nSendSms = GetPrivateProfileInt("Sms", "SendSms", 0, strFile);
  355. GetPrivateProfileString("Sms", "Port", "", g_strSmsComPort, sizeof(g_strSmsComPort), strFile);
  356. g_nSmsRate = GetPrivateProfileInt("Sms", "Rate", 115200, strFile);
  357. g_nSmsDataBit = GetPrivateProfileInt("Sms", "DataBit", 8, strFile);
  358. g_nSmsStopBit = GetPrivateProfileInt("Sms", "StopBit", 1, strFile);
  359. g_nSmsParity = GetPrivateProfileInt("Sms", "Parity", 0, strFile);
  360. g_nSmsInterval = GetPrivateProfileInt("Sms", "Interval", 0, strFile);
  361. g_nSmsLanguageTrans = GetPrivateProfileInt("Sms", "LanguageTrans", 0, strFile);
  362. g_nSmsMaxChar = GetPrivateProfileInt("Sms", "MaxChar", 0, strFile);
  363. g_nSmsMakeCall = GetPrivateProfileInt("Sms", "MakeCall", 0, strFile);
  364. GetPrivateProfileString("Sms", "SMSC", "", g_strSmsSMSC, sizeof(g_strSmsSMSC), strFile);
  365. g_nSmsTimOut = GetPrivateProfileInt("Sms", "TimeOut", 60, strFile);
  366. g_nSmsCallTimes = GetPrivateProfileInt("Sms", "ReCallTimes", 2, strFile);
  367. //Voice
  368. g_nSendNotice = GetPrivateProfileInt("Voice", "SendNotice", 0, strFile);
  369. g_nNoticeCardType = GetPrivateProfileInt("Voice", "CardType", 0, strFile);
  370. GetPrivateProfileString("Voice", "PreTel", "", g_strNoticeTelPre, sizeof(g_strNoticeTelPre), strFile);
  371. g_nNoticeCallDelay = GetPrivateProfileInt("Voice", "CallRelay", 60, strFile);
  372. g_nNoticeCallTimes = GetPrivateProfileInt("Voice", "ReCallTimes", 2, strFile);
  373. //TTS
  374. //AlarmMode
  375. g_nAlarmModeIsDlg = GetPrivateProfileInt("AlarmMode", "Dlg", 0, strFile);
  376. g_nAlarmModeShowDlgTimes = GetPrivateProfileInt("AlarmMode", "ShowTime", 1, strFile);
  377. g_nAlarmModeIsSound = GetPrivateProfileInt("AlarmMode", "Sound", 0, strFile);
  378. g_nAlarmModeSoundNum = GetPrivateProfileInt("AlarmMode", "PlayNum", 1, strFile);
  379. GetPrivateProfileString("Other", "SummaryLog", "", g_strSummaryLogPath, sizeof(g_strSummaryLogPath), strFile);
  380. //定时发送短信和电话参数配置
  381. g_nTimingEnable = GetPrivateProfileInt("Timing", "Enable", 0, strFile);
  382. g_nTimingWeeks = GetPrivateProfileInt("Timing", "Weeks", 0, strFile);
  383. g_nTimingHours = GetPrivateProfileInt("Timing", "Hours", 0, strFile);
  384. g_nTimingMinutes = GetPrivateProfileInt("Timing", "Minutes", 0, strFile);
  385. g_nSnmpEnable = GetPrivateProfileInt("Snmp", "Enable", 0, strFile);
  386. GetPrivateProfileString("Snmp", "ObjOid", "", g_strSnmpObjOid, sizeof(g_strSnmpObjOid), strFile);
  387. GetPrivateProfileString("Snmp", "IP", "", g_strSnmpIP, sizeof(g_strSnmpIP), strFile);
  388. GetPrivateProfileString("Snmp", "Field", "", g_strSnmpField, sizeof(g_strSnmpField), strFile);
  389. ////////////////////////////////////////////////////////////////////////////
  390. ////////////////////////////////////////////////////////////////////////////
  391. //增加自动启动注册表项
  392. #if 0
  393. wsprintf(strFile, "%s\\%s", g_strDirectory, g_strAppName);
  394. AddAutoSysRun(strFile, g_iAutoRunSystem);
  395. #endif
  396. return 0;
  397. }
  398. //软件时间限制代码
  399. INT ProgramEncrypt(HWND hWnd)
  400. {
  401. SYSTEMTIME systemtime;
  402. HKEY hKey = NULL;
  403. CHAR strKeyName[50] = "";
  404. CHAR strFirstDate[20] = "";
  405. CHAR strMaxDate[20] = "";
  406. CHAR strMinDate[20] = "";
  407. CHAR strNowDate[20] = "";
  408. DWORD dwErrorCode = 0;
  409. INT iNumberOfDays = 0;
  410. DWORD dwType = 0;
  411. DWORD dwSize = 0;
  412. if (g_bEnableTimeLimited == FALSE)
  413. return 1;
  414. //创建注册表,有该键则读取,无则创建
  415. strcpy(strKeyName, "Software\\stoneu");
  416. dwErrorCode = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  417. strKeyName,
  418. 0,
  419. NULL,
  420. REG_OPTION_NON_VOLATILE,
  421. KEY_ALL_ACCESS,
  422. NULL, //Security
  423. &hKey,
  424. NULL);
  425. if (dwErrorCode == ERROR_SUCCESS)
  426. {
  427. dwType = REG_BINARY;
  428. dwSize = sizeof(strFirstDate);
  429. dwErrorCode = RegQueryValueEx(hKey,
  430. "Date",
  431. NULL,
  432. &dwType,
  433. (BYTE*)strFirstDate,
  434. &dwSize
  435. );
  436. if (dwErrorCode == ERROR_SUCCESS && strcmp(strFirstDate, ""))
  437. {
  438. ::GetLocalTime(&systemtime);
  439. CTime NowDate(systemtime.wYear, systemtime.wMonth, systemtime.wDay, systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
  440. CTime MaxDate(systemtime.wYear, systemtime.wMonth, systemtime.wDay, systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
  441. CTime MinDate(systemtime.wYear, systemtime.wMonth, systemtime.wDay, systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
  442. iNumberOfDays = 90;
  443. MaxDate = MaxDate + CTimeSpan(iNumberOfDays, 0, 0, 0);
  444. MinDate = MinDate - CTimeSpan(iNumberOfDays, 0, 0, 0);
  445. sprintf(strMaxDate, "%04d%02d%02d", MaxDate.GetYear(), MaxDate.GetMonth(), MaxDate.GetDay());
  446. sprintf(strMinDate, "%04d%02d%02d", MinDate.GetYear(), MinDate.GetMonth(), MinDate.GetDay());
  447. if (atoi(strMaxDate) >= atoi(strFirstDate) && atoi(strMinDate) <= atoi(strFirstDate))
  448. {
  449. if (NULL != hKey)
  450. RegCloseKey(hKey);
  451. hKey = NULL;
  452. return 1;
  453. }
  454. else
  455. {
  456. LOG4C((LOG_NOTICE, "软件试用天数已经到期!"));
  457. //MessageBox(hWnd, "软件试用天数已经到期!", "提示", MB_OK+MB_ICONINFORMATION);
  458. ////超过x天,则禁止运行
  459. //::InvalidateRect(hWnd, NULL, TRUE);
  460. //::UpdateWindow(hWnd);
  461. }
  462. }
  463. else
  464. {
  465. if (NULL == hWnd)
  466. {
  467. ::GetLocalTime(&systemtime);
  468. sprintf(strFirstDate, "%04d%02d%02d", systemtime.wYear, systemtime.wMonth, systemtime.wDay);
  469. dwErrorCode = RegSetValueEx(hKey,
  470. "Date",
  471. NULL,
  472. REG_BINARY,
  473. (BYTE*)strFirstDate,
  474. sizeof(strFirstDate)
  475. );
  476. if (dwErrorCode == ERROR_SUCCESS)
  477. {
  478. if (NULL != hKey)
  479. RegCloseKey(hKey);
  480. hKey = NULL;
  481. return 1;
  482. }
  483. }
  484. }
  485. }
  486. if (NULL != hKey)
  487. RegCloseKey(hKey);
  488. hKey = NULL;
  489. return 0;
  490. }
  491. VOID TerminateLastProcess()
  492. {
  493. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  494. if (hSnapShot != NULL)
  495. {
  496. PROCESSENTRY32 ProcessInfo = {0};//声明进程信息变量
  497. ProcessInfo.dwSize = sizeof(ProcessInfo);//设置ProcessInfo的大小
  498. //返回系统中第一个进程的信息
  499. BOOL bStatus = Process32First(hSnapShot, &ProcessInfo);
  500. while (bStatus)
  501. {
  502. if (!_stricmp(g_strAppName, ProcessInfo.szExeFile))//映像名称
  503. {
  504. HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessInfo.th32ProcessID);
  505. ::TerminateProcess(hProcess, 1);
  506. hProcess = NULL;
  507. Sleep(1000);
  508. break;
  509. }
  510. else
  511. //获取下一个进程的信息
  512. bStatus = Process32Next(hSnapShot, &ProcessInfo);
  513. }
  514. }
  515. }
  516. //增加自动启动注册表项
  517. INT AddAutoSysRun(CHAR *strExeFile, INT iAutoRunSystem)
  518. {
  519. HKEY hKey = NULL;
  520. DWORD dwErrorCode = 0;
  521. //创建注册表,有该键则读取,无则创建
  522. dwErrorCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  523. "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
  524. 0,
  525. NULL,
  526. REG_OPTION_NON_VOLATILE,
  527. KEY_ALL_ACCESS,
  528. NULL, //Security
  529. &hKey,
  530. NULL);
  531. if (dwErrorCode == ERROR_SUCCESS)
  532. {
  533. if (iAutoRunSystem)
  534. dwErrorCode = RegSetValueEx(hKey, "SToneU-isP-CMS", NULL, REG_SZ, (BYTE*)strExeFile, (DWORD)strlen(strExeFile));
  535. else
  536. dwErrorCode = RegDeleteKey(hKey, "SToneU-isP-CMS");
  537. }
  538. if (NULL != hKey)
  539. RegCloseKey(hKey);
  540. hKey = NULL;
  541. return 0;
  542. }