SysLib.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. #include "stdafx.h"
  2. #include <tlhelp32.h>
  3. #include "SysLib.h"
  4. #include <io.h>
  5. #include <odbcinst.h>
  6. #include <afxdb.h>
  7. #include "resource.h"
  8. #include "crc32.h"
  9. #include "Client2SrvType.h"
  10. //#include "..\ChineseRes\ChineseRes\resource.h"
  11. #include <psapi.h>
  12. #pragma comment ( lib, "psapi.lib" )
  13. //*********************************************************
  14. //Functiopn: LogEvent
  15. //Description: 记录服务事件
  16. //Calls:
  17. //Called By:
  18. //Table Accessed:
  19. //Table Updated:
  20. //Input:
  21. //Output:
  22. //Return:
  23. //Others:
  24. //History:
  25. // <author>niying <time>2006-8-10 <version> <desc>
  26. //*********************************************************
  27. void LogEvent(LPCTSTR pFormat, ...)
  28. {
  29. TCHAR chMsg[256];
  30. HANDLE hEventSource;
  31. LPTSTR lpszStrings[1];
  32. va_list pArg;
  33. va_start(pArg, pFormat);
  34. _vstprintf(chMsg, pFormat, pArg);
  35. va_end(pArg);
  36. lpszStrings[0] = chMsg;
  37. hEventSource = RegisterEventSource(NULL, g_szServiceName);
  38. if (hEventSource != NULL)
  39. {
  40. ReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (LPCTSTR*) &lpszStrings[0], NULL);
  41. DeregisterEventSource(hEventSource);
  42. }
  43. }
  44. //增加自动启动注册表项
  45. INT AddAutoSysRun(CHAR *strExeFile, INT iAutoRunSystem)
  46. {
  47. HKEY hKey = NULL;
  48. DWORD dwErrorCode = 0;
  49. //创建注册表,有该键则读取,无则创建
  50. dwErrorCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  51. "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
  52. 0,
  53. NULL,
  54. REG_OPTION_NON_VOLATILE,
  55. KEY_ALL_ACCESS,
  56. NULL, //Security
  57. &hKey,
  58. NULL);
  59. if (dwErrorCode == ERROR_SUCCESS)
  60. {
  61. if (iAutoRunSystem)
  62. dwErrorCode = RegSetValueEx(hKey, "eBus-CMS", NULL, REG_SZ, (BYTE*)strExeFile, (DWORD)strlen(strExeFile));
  63. else
  64. dwErrorCode = RegDeleteKey(hKey, "eBus-CMS");
  65. }
  66. if (NULL != hKey)
  67. RegCloseKey(hKey);
  68. hKey = NULL;
  69. return 0;
  70. }
  71. //软件时间限制代码
  72. INT ProgramEncrypt(HWND hWnd)
  73. {
  74. SYSTEMTIME systemtime;
  75. HKEY hKey = NULL;
  76. CHAR strKeyName[50] = "";
  77. CHAR strFirstDate[20] = "";
  78. CHAR strMaxDate[20] = "";
  79. CHAR strMinDate[20] = "";
  80. CHAR strNowDate[20] = "";
  81. DWORD dwErrorCode = 0;
  82. INT iNumberOfDays = 0;
  83. DWORD dwType = 0;
  84. DWORD dwSize = 0;
  85. if (g_bEnableTimeLimited == FALSE)
  86. return 1;
  87. //创建注册表,有该键则读取,无则创建
  88. strcpy(strKeyName, "Software\\stoneu");
  89. dwErrorCode = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  90. strKeyName,
  91. 0,
  92. NULL,
  93. REG_OPTION_NON_VOLATILE,
  94. KEY_ALL_ACCESS,
  95. NULL, //Security
  96. &hKey,
  97. NULL);
  98. if (dwErrorCode == ERROR_SUCCESS)
  99. {
  100. dwType = REG_BINARY;
  101. dwSize = sizeof(strFirstDate);
  102. dwErrorCode = RegQueryValueEx(hKey,
  103. "Date",
  104. NULL,
  105. &dwType,
  106. (BYTE*)strFirstDate,
  107. &dwSize
  108. );
  109. if (dwErrorCode == ERROR_SUCCESS && strcmp(strFirstDate, ""))
  110. {
  111. ::GetLocalTime(&systemtime);
  112. CTime NowDate(systemtime.wYear, systemtime.wMonth, systemtime.wDay, systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
  113. CTime MaxDate(systemtime.wYear, systemtime.wMonth, systemtime.wDay, systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
  114. CTime MinDate(systemtime.wYear, systemtime.wMonth, systemtime.wDay, systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
  115. iNumberOfDays = 90;
  116. MaxDate = MaxDate + CTimeSpan(iNumberOfDays, 0, 0, 0);
  117. MinDate = MinDate - CTimeSpan(iNumberOfDays, 0, 0, 0);
  118. sprintf(strMaxDate, "%04d%02d%02d", MaxDate.GetYear(), MaxDate.GetMonth(), MaxDate.GetDay());
  119. sprintf(strMinDate, "%04d%02d%02d", MinDate.GetYear(), MinDate.GetMonth(), MinDate.GetDay());
  120. if (atoi(strMaxDate) >= atoi(strFirstDate) && atoi(strMinDate) <= atoi(strFirstDate))
  121. {
  122. if (NULL != hKey)
  123. RegCloseKey(hKey);
  124. hKey = NULL;
  125. return 1;
  126. }
  127. else
  128. {
  129. LOG4C((LOG_NOTICE, "软件试用天数已经到期!"));
  130. //MessageBox(hWnd, "软件试用天数已经到期!", "提示", MB_OK+MB_ICONINFORMATION);
  131. ////超过x天,则禁止运行
  132. //::InvalidateRect(hWnd, NULL, TRUE);
  133. //::UpdateWindow(hWnd);
  134. }
  135. }
  136. else
  137. {
  138. if (NULL == hWnd)
  139. {
  140. ::GetLocalTime(&systemtime);
  141. sprintf(strFirstDate, "%04d%02d%02d", systemtime.wYear, systemtime.wMonth, systemtime.wDay);
  142. dwErrorCode = RegSetValueEx(hKey,
  143. "Date",
  144. NULL,
  145. REG_BINARY,
  146. (BYTE*)strFirstDate,
  147. sizeof(strFirstDate)
  148. );
  149. if (dwErrorCode == ERROR_SUCCESS)
  150. {
  151. if (NULL != hKey)
  152. RegCloseKey(hKey);
  153. hKey = NULL;
  154. return 1;
  155. }
  156. }
  157. }
  158. }
  159. if (NULL != hKey)
  160. RegCloseKey(hKey);
  161. hKey = NULL;
  162. return 0;
  163. }
  164. //
  165. // FindProcess
  166. // 这个函数唯一的参数是你指定的进程名,如:你的目标进程
  167. // 是 "Notepad.exe",返回值是该进程的ID,失败返回0
  168. //
  169. int FindProcessCount(char *strProcessName)
  170. {
  171. int nCount = 0;
  172. DWORD aProcesses[1024], cbNeeded, cbMNeeded;
  173. HMODULE hMods[1024];
  174. HANDLE hProcess;
  175. char szProcessName[MAX_PATH];
  176. if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return 0;
  177. for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)
  178. {
  179. //_tprintf(_T("%d\t"), aProcesses[i]);
  180. hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
  181. EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);
  182. GetModuleFileNameEx( hProcess, hMods[0], szProcessName,sizeof(szProcessName));
  183. if(strstr(szProcessName, strProcessName))
  184. {
  185. //_tprintf(_T("%s;"), szProcessName);
  186. nCount++;
  187. }
  188. //_tprintf(_T("\n"));
  189. }
  190. return nCount;
  191. }
  192. //
  193. // Function: ErrorForce
  194. // 此函数中用上面的 FindProcess 函数获得你的目标进程的ID
  195. // 用WIN API OpenPorcess 获得此进程的句柄,再以TerminateProcess
  196. // 强制结束这个进程
  197. //
  198. VOID KillProcess(DWORD dwProcess)
  199. {
  200. // When the all operation fail this function terminate the "winlogon" Process for force exit the system.
  201. HANDLE hTargetProcess = OpenProcess(PROCESS_QUERY_INFORMATION | // Required by Alpha
  202. PROCESS_CREATE_THREAD | // For CreateRemoteThread
  203. PROCESS_VM_OPERATION | // For VirtualAllocEx/VirtualFreeEx
  204. PROCESS_VM_WRITE, // For WriteProcessMemory
  205. FALSE, dwProcess);
  206. if(hTargetProcess == NULL)
  207. {
  208. return;
  209. }
  210. TerminateProcess(hTargetProcess, 0);
  211. return;
  212. }
  213. //
  214. // FindProcess
  215. // 这个函数唯一的参数是你指定的进程名,如:你的目标进程
  216. // 是 "Notepad.exe",返回值是该进程的ID,失败返回0
  217. //
  218. DWORD FindAndKillProcess(char *strProcessName)
  219. {
  220. DWORD aProcesses[1024], cbNeeded, cbMNeeded;
  221. HMODULE hMods[1024];
  222. HANDLE hProcess;
  223. char szProcessName[MAX_PATH];
  224. if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return 0;
  225. for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)
  226. {
  227. //_tprintf(_T("%d\t"), aProcesses[i]);
  228. hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
  229. EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);
  230. GetModuleFileNameEx( hProcess, hMods[0], szProcessName,sizeof(szProcessName));
  231. if(strstr(szProcessName, strProcessName))
  232. {
  233. //_tprintf(_T("%s;"), szProcessName);
  234. KillProcess(aProcesses[i]);
  235. //return(aProcesses[i]);
  236. }
  237. //_tprintf(_T("\n"));
  238. }
  239. return 0;
  240. }
  241. int Handle_DeleteTrayNullIcon( BOOL &bNullBeDel )
  242. {
  243. bNullBeDel = FALSE;
  244. HWND hStatus=::FindWindow("Shell_TrayWnd",NULL); //得到任务栏句柄
  245. if (hStatus==NULL)
  246. {
  247. //AfxMessageBox ( "Get Shell_TrayWnd error!" );
  248. return -1;
  249. }
  250. HWND hNotify=FindWindowEx(hStatus,NULL,"TrayNotifyWnd",NULL); //右下角区域
  251. if (hNotify==NULL)
  252. {
  253. //AfxMessageBox ( "Get TrayNotifyWnd error!" );
  254. return -1;
  255. }
  256. HWND hNotify1=FindWindowEx(hNotify,NULL,"SysPager",NULL);
  257. if (hNotify==NULL)
  258. {
  259. //AfxMessageBox ( "Get SysPager error!" );
  260. return -1;
  261. }
  262. HWND hNotify1_0=FindWindowEx(hNotify1,NULL,"ToolBarWindow32",NULL);//右下角区域(不包括时间)
  263. if (hNotify1_0==NULL)
  264. {
  265. //AfxMessageBox ( "Get ToolBarWindow32 error!" );
  266. return -1;
  267. }
  268. //-------------------以上是得到任务栏右下脚一块地方的句柄
  269. DWORD pid = 0;
  270. GetWindowThreadProcessId(hNotify1_0,&pid);
  271. if (pid==NULL)
  272. {
  273. //AfxMessageBox ( "Get pid error!" );
  274. return -1;
  275. }
  276. HANDLE hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_ALL_ACCESS,true,pid);
  277. if (hProcess==NULL)
  278. {
  279. //AfxMessageBox ( "Get hd error!" );
  280. return -1;
  281. }
  282. ::SendMessage(hNotify1_0,WM_PAINT ,NULL,NULL);
  283. CRect rect;
  284. ::GetWindowRect(hNotify1_0,&rect);
  285. ::InvalidateRect(hNotify1_0,&rect,false);
  286. int iNum = (int)::SendMessage(hNotify1_0,TB_BUTTONCOUNT ,NULL,NULL); //获取任务栏上图标个数
  287. unsigned long n = 0;
  288. TBBUTTON *pButton = new TBBUTTON;
  289. CString strInfo = _T("");
  290. wchar_t name[256] = {0};
  291. TBBUTTON BButton;
  292. unsigned long whd,proid;
  293. CString x;
  294. for(int i=0; i<iNum; i++)
  295. {
  296. ::SendMessage(hNotify1_0,TB_GETBUTTON,i,(LPARAM)(&BButton));
  297. ReadProcessMemory(hProcess,&BButton,pButton,sizeof(TBBUTTON),&n);
  298. if (pButton->iString != 0xffffffff)
  299. {
  300. try
  301. {
  302. ReadProcessMemory(hProcess,(void *)pButton->iString,name,255,&n);
  303. }
  304. catch(...)
  305. {
  306. }
  307. strInfo.Format("%d : %s\n",i+1,CString(name));
  308. //TRACE(strInfo);
  309. }
  310. try
  311. {
  312. whd=0;
  313. ReadProcessMemory(hProcess,(void *)pButton->dwData,&whd,4,&n);
  314. }
  315. catch(...)
  316. {
  317. }
  318. proid=NULL;
  319. GetWindowThreadProcessId((HWND)whd,&proid);
  320. if(proid==NULL)
  321. {
  322. bNullBeDel = TRUE;
  323. ::SendMessage(hNotify1_0,TB_DELETEBUTTON,i,0);
  324. }
  325. }
  326. delete pButton;
  327. return 0;
  328. }
  329. int GetSysData()
  330. {
  331. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  332. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  333. int iPosFile = 0;
  334. int iLenghtFile = 0;
  335. ////////////////////////////////////////////////////////////////////////////
  336. //获取应用名
  337. //GetModuleFileName(AfxGetApp()->m_hInstance, g_strDirectory, MAX_PATH);
  338. GetModuleFileName(NULL, g_strDirectory, MAX_PATH);
  339. iLenghtFile = (int)strlen(g_strDirectory);
  340. for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--)
  341. {
  342. if (g_strDirectory[iPosFile] == '\\')
  343. {
  344. memset(g_strAppName, 0, sizeof(g_strAppName));
  345. memcpy(g_strAppName, g_strDirectory + iPosFile + 1, iLenghtFile - iPosFile - 1);
  346. break;
  347. }
  348. }
  349. iLenghtFile = strlen(g_strDirectory);
  350. for (iPosFile = iLenghtFile - 1; iPosFile >= 0; iPosFile--)
  351. {
  352. if (g_strDirectory[iPosFile] == '\\')
  353. {
  354. g_strDirectory[iPosFile] = '\0';
  355. break;
  356. }
  357. }
  358. wsprintf(strFile, "%s\\serverset.ini", g_strDirectory);
  359. //是否自动启动系统
  360. GetPrivateProfileString("Other", "AutoRunSystem", "", strValue, sizeof(strValue), strFile);
  361. if (strcmp(strValue, ""))
  362. g_iAutoRunSystem = atoi(strValue);
  363. else
  364. g_iAutoRunSystem = 0;
  365. if (g_iAutoRunSystem <= 0)
  366. g_iAutoRunSystem = 0;
  367. else
  368. g_iAutoRunSystem = 1;
  369. //WatchService
  370. GetPrivateProfileString("WatchServer", "ServerIP", "", g_strWatchServerIP, sizeof(g_strWatchServerIP), strFile);
  371. GetPrivateProfileString("WatchServer", "Port", "", g_strWatchServerPort, sizeof(g_strWatchServerPort), strFile);
  372. //语言
  373. GetPrivateProfileString("Other", "Language", "", g_strLanguage, sizeof(g_strLanguage), strFile);
  374. //Database
  375. GetPrivateProfileString("Database", "DBType", "", g_strDBType, sizeof(g_strDBType), strFile);
  376. GetPrivateProfileString("Database", "AccessFile", "", g_strAccessFile, sizeof(g_strAccessFile), strFile);
  377. GetPrivateProfileString("Database", "ServerName", "", g_strServerName, sizeof(g_strServerName), strFile);
  378. GetPrivateProfileString("Database", "DataBaseName", "", g_strDataBaseName, sizeof(g_strDataBaseName), strFile);
  379. GetPrivateProfileString("Database", "UserName", "", g_strUserName, sizeof(g_strUserName), strFile);
  380. GetPrivateProfileString("Database", "Password", "", g_strPassword, sizeof(g_strPassword), strFile);
  381. //数据库连接串
  382. if (!_stricmp(g_strDBType, "SQL SERVER"))
  383. sprintf(g_strConnectString, "Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
  384. g_strServerName, g_strDataBaseName, g_strUserName, g_strPassword);
  385. else if (!_stricmp(g_strDBType, "ACCESS97"))
  386. sprintf(g_strConnectString, "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=%s", g_strAccessFile);
  387. else if (!_stricmp(g_strDBType, "ACCESS2000"))
  388. sprintf(g_strConnectString, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s", g_strAccessFile);
  389. else if(!_stricmp(g_strDBType, "PGSQL"))
  390. sprintf(g_strConnectString, "DRIVER={PostgreSQL ODBC Driver(UNICODE)}; SERVER=%s; port=5432; DATABASE=%s; UID=%s; PWD=%s;",
  391. g_strServerName, g_strDataBaseName, g_strUserName, g_strPassword);
  392. else
  393. sprintf(g_strConnectString, "Provider=sqloledb;Data Source=%s;Initial Catalog=%s;User Id=%s;Password=%s; ",
  394. g_strServerName, g_strDataBaseName, g_strUserName, g_strPassword);
  395. //Common
  396. GetPrivateProfileString("Common", "rgSNCODE", "", g_strSNCode, sizeof(g_strSNCode), strFile);
  397. g_nLevelWav = GetPrivateProfileInt("Common", "LevelWav", 3, strFile);
  398. g_nLevelSms = GetPrivateProfileInt("Common", "LevelSms", 3, strFile);
  399. g_nLevelEmail = GetPrivateProfileInt("Common", "LevelEmail", 3, strFile);
  400. g_nLevelDevInterrupt = GetPrivateProfileInt("Common", "LevelDevInterrupt", 8, strFile);
  401. g_nLevelDlg = GetPrivateProfileInt("Common", "LevelDlg", 1, strFile);
  402. g_nLevelLocalWav = GetPrivateProfileInt("Common", "LevelLocalWav", 1, strFile);
  403. g_nDBRecordDate = GetPrivateProfileInt("Common", "DbRecordDate", 365, strFile);
  404. //Email
  405. g_nSendEmail = GetPrivateProfileInt("Email", "SendEmail", 0, strFile);
  406. GetPrivateProfileString("Email", "SMTPServer", "", g_strEmailSMTPSrv, sizeof(g_strEmailSMTPSrv), strFile);
  407. g_nEmailSMTPPort = GetPrivateProfileInt("Email", "SMTPPort", 25, strFile);
  408. g_nEmailIsNeed = GetPrivateProfileInt("Email", "IsNeed", 1, strFile);
  409. GetPrivateProfileString("Email", "UserAcc", "", g_strEmailUserAcc, sizeof(g_strEmailUserAcc), strFile);
  410. GetPrivateProfileString("Email", "UserPwd", "", g_strEmailUserPwd, sizeof(g_strEmailUserPwd), strFile);
  411. GetPrivateProfileString("Email", "MailFrom", "", g_strEmailFromAddr, sizeof(g_strEmailFromAddr), strFile);
  412. g_nEmailTimeOut = GetPrivateProfileInt("Email", "TimeOut", 60, strFile);
  413. GetPrivateProfileString("Email", "Subject", "", g_strEmailSubject, sizeof(g_strEmailSubject), strFile);
  414. g_nEmailCallTimes = GetPrivateProfileInt("Email", "ReCallTimes", 2, strFile);
  415. //Sms
  416. g_nSendSms = GetPrivateProfileInt("Sms", "SendSms", 0, strFile);
  417. GetPrivateProfileString("Sms", "Port", "", g_strSmsComPort, sizeof(g_strSmsComPort), strFile);
  418. g_nSmsRate = GetPrivateProfileInt("Sms", "Rate", 115200, strFile);
  419. g_nSmsDataBit = GetPrivateProfileInt("Sms", "DataBit", 8, strFile);
  420. g_nSmsStopBit = GetPrivateProfileInt("Sms", "StopBit", 1, strFile);
  421. g_nSmsParity = GetPrivateProfileInt("Sms", "Parity", 0, strFile);
  422. g_nSmsInterval = GetPrivateProfileInt("Sms", "Interval", 0, strFile);
  423. g_nSmsLanguageTrans = GetPrivateProfileInt("Sms", "LanguageTrans", 0, strFile);
  424. g_nSmsMaxChar = GetPrivateProfileInt("Sms", "MaxChar", 0, strFile);
  425. g_nSmsMakeCall = GetPrivateProfileInt("Sms", "MakeCall", 0, strFile);
  426. GetPrivateProfileString("Sms", "SMSC", "", g_strSmsSMSC, sizeof(g_strSmsSMSC), strFile);
  427. g_nSmsTimOut = GetPrivateProfileInt("Sms", "TimeOut", 60, strFile);
  428. g_nSmsCallTimes = GetPrivateProfileInt("Sms", "ReCallTimes", 2, strFile);
  429. //Voice
  430. g_nSendNotice = GetPrivateProfileInt("Voice", "SendNotice", 0, strFile);
  431. g_nNoticeCardType = GetPrivateProfileInt("Voice", "CardType", 0, strFile);
  432. GetPrivateProfileString("Voice", "PreTel", "", g_strNoticeTelPre, sizeof(g_strNoticeTelPre), strFile);
  433. g_nNoticeCallDelay = GetPrivateProfileInt("Voice", "CallRelay", 60, strFile);
  434. g_nNoticeCallTimes = GetPrivateProfileInt("Voice", "ReCallTimes", 2, strFile);
  435. //TTS
  436. g_nTTSType = GetPrivateProfileInt("TTS", "VoiceType", 0, strFile);
  437. //AlarmMode
  438. g_nAlarmModeIsDlg = GetPrivateProfileInt("AlarmMode", "Dlg", 0, strFile);
  439. g_nAlarmModeShowDlgTimes = GetPrivateProfileInt("AlarmMode", "ShowTime", 1, strFile);
  440. g_nAlarmModeIsSound = GetPrivateProfileInt("AlarmMode", "Sound", 0, strFile);
  441. g_nAlarmModeSoundNum = GetPrivateProfileInt("AlarmMode", "PlayNum", 1, strFile);
  442. GetPrivateProfileString("Other", "SummaryLog", "", g_strSummaryLogPath, sizeof(g_strSummaryLogPath), strFile);
  443. //定时发送短信和电话参数配置
  444. g_nTimingEnable = GetPrivateProfileInt("Timing", "Enable", 0, strFile);
  445. g_nTimingWeeks = GetPrivateProfileInt("Timing", "Weeks", 0, strFile);
  446. g_nTimingHours = GetPrivateProfileInt("Timing", "Hours", 0, strFile);
  447. g_nTimingMinutes = GetPrivateProfileInt("Timing", "Minutes", 0, strFile);
  448. //变量输出,特别为上海烟草局做的
  449. g_nOutputEnable = GetPrivateProfileInt("output", "Enable", 0, strFile);
  450. GetPrivateProfileString("output", "path", "", g_strOutputPath, sizeof(g_strOutputPath), strFile);
  451. GetPrivateProfileString("output", "filename", "", g_strOutputFileName, sizeof(g_strOutputFileName), strFile);
  452. GetPrivateProfileString("output", "clock", "", g_strOutputClock, sizeof(g_strOutputClock), strFile);
  453. GetPrivateProfileString("output", "line1", "", g_strOutputLine1, sizeof(g_strOutputLine1), strFile);
  454. GetPrivateProfileString("output", "line2", "", g_strOutputLine2, sizeof(g_strOutputLine2), strFile);
  455. GetPrivateProfileString("output", "line3", "", g_strOutputLine3, sizeof(g_strOutputLine3), strFile);
  456. GetPrivateProfileString("output", "line4", "", g_strOutputLine4, sizeof(g_strOutputLine4), strFile);
  457. GetPrivateProfileString("output", "line5", "", g_strOutputLine5, sizeof(g_strOutputLine5), strFile);
  458. GetPrivateProfileString("output", "line6", "", g_strOutputLine6, sizeof(g_strOutputLine6), strFile);
  459. GetPrivateProfileString("output", "line7", "", g_strOutputLine7, sizeof(g_strOutputLine7), strFile);
  460. GetPrivateProfileString("output", "line8", "", g_strOutputLine8, sizeof(g_strOutputLine8), strFile);
  461. GetPrivateProfileString("output", "line9", "", g_strOutputLine9, sizeof(g_strOutputLine9), strFile);
  462. GetPrivateProfileString("output", "line10", "", g_strOutputLine10, sizeof(g_strOutputLine10), strFile);
  463. g_nOutputLineCount1 = GetPrivateProfileInt("output", "lineCount1", 0, strFile);
  464. g_nOutputLineCount2 = GetPrivateProfileInt("output", "lineCount2", 0, strFile);
  465. g_nOutputLineCount3 = GetPrivateProfileInt("output", "lineCount3", 0, strFile);
  466. g_nOutputLineCount4 = GetPrivateProfileInt("output", "lineCount4", 0, strFile);
  467. g_nOutputLineCount5 = GetPrivateProfileInt("output", "lineCount5", 0, strFile);
  468. g_nOutputLineCount6 = GetPrivateProfileInt("output", "lineCount6", 0, strFile);
  469. g_nOutputLineCount7 = GetPrivateProfileInt("output", "lineCount7", 0, strFile);
  470. g_nOutputLineCount8 = GetPrivateProfileInt("output", "lineCount8", 0, strFile);
  471. g_nOutputLineCount9 = GetPrivateProfileInt("output", "lineCount9", 0, strFile);
  472. g_nOutputLineCount10 = GetPrivateProfileInt("output", "lineCount10", 0, strFile);
  473. GetPrivateProfileString("output", "lineUid1", "", g_strOutputLineUid1, sizeof(g_strOutputLineUid1), strFile);
  474. GetPrivateProfileString("output", "lineUid2", "", g_strOutputLineUid2, sizeof(g_strOutputLineUid2), strFile);
  475. GetPrivateProfileString("output", "lineUid3", "", g_strOutputLineUid3, sizeof(g_strOutputLineUid3), strFile);
  476. GetPrivateProfileString("output", "lineUid4", "", g_strOutputLineUid4, sizeof(g_strOutputLineUid4), strFile);
  477. GetPrivateProfileString("output", "lineUid5", "", g_strOutputLineUid5, sizeof(g_strOutputLineUid5), strFile);
  478. GetPrivateProfileString("output", "lineUid6", "", g_strOutputLineUid6, sizeof(g_strOutputLineUid6), strFile);
  479. GetPrivateProfileString("output", "lineUid7", "", g_strOutputLineUid7, sizeof(g_strOutputLineUid7), strFile);
  480. GetPrivateProfileString("output", "lineUid8", "", g_strOutputLineUid8, sizeof(g_strOutputLineUid8), strFile);
  481. GetPrivateProfileString("output", "lineUid9", "", g_strOutputLineUid9, sizeof(g_strOutputLineUid9), strFile);
  482. GetPrivateProfileString("output", "lineUid10", "", g_strOutputLineUid10, sizeof(g_strOutputLineUid10), strFile);
  483. g_nSnmpEnable = GetPrivateProfileInt("Snmp", "Enable", 0, strFile);
  484. GetPrivateProfileString("Snmp", "ObjOid", "", g_strSnmpObjOid, sizeof(g_strSnmpObjOid), strFile);
  485. GetPrivateProfileString("Snmp", "IP", "", g_strSnmpIP, sizeof(g_strSnmpIP), strFile);
  486. GetPrivateProfileString("Snmp", "Field", "", g_strSnmpField, sizeof(g_strSnmpField), strFile);
  487. ////////////////////////////////////////////////////////////////////////////
  488. ////////////////////////////////////////////////////////////////////////////
  489. //增加自动启动注册表项
  490. #if 0
  491. wsprintf(strFile, "%s\\%s", g_strDirectory, g_strAppName);
  492. AddAutoSysRun(strFile, g_iAutoRunSystem);
  493. #endif
  494. return 0;
  495. }
  496. int GetResourceString()
  497. {
  498. g_strServiceName.LoadString(IDS_SERVICE_NAME);
  499. g_strHintSysRun.LoadString(IDS_HINT_SYS_RUN);
  500. g_strHintAlarmWelcome.LoadString(IDS_HINT_ALARM_WELCOME);
  501. g_strHintVoiceEnd.LoadString(IDS_HINT_VOICE_END);
  502. g_strHintReturnNormal.LoadString(IDS_HINT_RETURN_NORMAL);
  503. g_strHintUpperLimit.LoadString(IDS_HINT_UPPER_LIMIT);
  504. g_strHintLowerLimit.LoadString(IDS_HINT_LOWER_LIMIT);
  505. g_strHintCurrValue.LoadString(IDS_HINT_CURR_VALUE);
  506. return 0;
  507. }
  508. VOID TerminateLastProcess()
  509. {
  510. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  511. if (hSnapShot != NULL)
  512. {
  513. PROCESSENTRY32 ProcessInfo = {0};//声明进程信息变量
  514. ProcessInfo.dwSize = sizeof(ProcessInfo);//设置ProcessInfo的大小
  515. //返回系统中第一个进程的信息
  516. BOOL bStatus = Process32First(hSnapShot, &ProcessInfo);
  517. while (bStatus)
  518. {
  519. if (!_stricmp(g_strAppName, ProcessInfo.szExeFile))//映像名称
  520. {
  521. HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessInfo.th32ProcessID);
  522. ::TerminateProcess(hProcess, 1);
  523. hProcess = NULL;
  524. Sleep(1000);
  525. break;
  526. }
  527. else
  528. //获取下一个进程的信息
  529. bStatus = Process32Next(hSnapShot, &ProcessInfo);
  530. }
  531. }
  532. }
  533. //-------------------------------------------------------------------------------------
  534. //Description:
  535. // This function maps a character string to a wide-character (Unicode) string
  536. //
  537. //Parameters:
  538. // lpcszStr: [in] Pointer to the character string to be converted
  539. // lpwszStr: [out] Pointer to a buffer that receives the translated string.
  540. // dwSize: [in] Size of the buffer
  541. //
  542. //Return Values:
  543. // TRUE: Succeed
  544. // FALSE: Failed
  545. //
  546. //Example:
  547. // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
  548. //---------------------------------------------------------------------------------------
  549. BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
  550. {
  551. // Get the required size of the buffer that receives the Unicode
  552. // string.
  553. DWORD dwMinSize;
  554. dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
  555. if(dwSize < dwMinSize)
  556. {
  557. return FALSE;
  558. }
  559. // Convert headers from ASCII to Unicode.
  560. MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);
  561. return TRUE;
  562. }
  563. //-------------------------------------------------------------------------------------
  564. //Description:
  565. // This function maps a wide-character string to a new character string
  566. //
  567. //Parameters:
  568. // lpcwszStr: [in] Pointer to the character string to be converted
  569. // lpszStr: [out] Pointer to a buffer that receives the translated string.
  570. // dwSize: [in] Size of the buffer
  571. //
  572. //Return Values:
  573. // TRUE: Succeed
  574. // FALSE: Failed
  575. //
  576. //Example:
  577. // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
  578. //---------------------------------------------------------------------------------------
  579. BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
  580. {
  581. DWORD dwMinSize;
  582. dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
  583. if(dwSize < dwMinSize)
  584. {
  585. return FALSE;
  586. }
  587. WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
  588. return TRUE;
  589. }
  590. //////////////////////////////////////////////////////////////////////////////
  591. //名称:GetExcelDriver
  592. //功能:获取ODBC中Excel驱动
  593. //作者:徐景周(jingzhou_xu@163.net)
  594. //组织:未来工作室(Future Studio)
  595. //日期:2002.9.1
  596. /////////////////////////////////////////////////////////////////////////////
  597. CString GetExcelDriver()
  598. {
  599. char szBuf[2001];
  600. WORD cbBufMax = 2000;
  601. WORD cbBufOut;
  602. char *pszBuf = szBuf;
  603. CString sDriver;
  604. // 获取已安装驱动的名称(涵数在odbcinst.h里)
  605. if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
  606. return "";
  607. // 检索已安装的驱动是否有Excel...
  608. do
  609. {
  610. if (strstr(pszBuf, "Excel") != 0)
  611. {
  612. //发现 !
  613. sDriver = CString(pszBuf);
  614. break;
  615. }
  616. pszBuf = strchr(pszBuf, '\0') + 1;
  617. }
  618. while (pszBuf[1] != '\0');
  619. return sDriver;
  620. }
  621. ///////////////////////////////////////////////////////////////////////////////
  622. // BOOL MakeSurePathExists( CString &Path,bool FilenameIncluded)
  623. // 参数:
  624. // Path 路径
  625. // FilenameIncluded 路径是否包含文件名
  626. // 返回值:
  627. // 文件是否存在
  628. // 说明:
  629. // 判断Path文件(FilenameIncluded=true)是否存在,存在返回TURE,不存在返回FALSE
  630. // 自动创建目录
  631. //
  632. ///////////////////////////////////////////////////////////////////////////////
  633. BOOL MakeSurePathExists( CString &Path,
  634. bool FilenameIncluded)
  635. {
  636. int Pos=0;
  637. while((Pos=Path.Find('\\',Pos+1))!=-1)
  638. CreateDirectory(Path.Left(Pos),NULL);
  639. if(!FilenameIncluded)
  640. CreateDirectory(Path,NULL);
  641. // return ((!FilenameIncluded)?!_access(Path,0):
  642. // !_access(Path.Left(Path.ReverseFind('\\')),0));
  643. return !_access(Path,0);
  644. }
  645. int GetSensorType( CString strSensorType )
  646. {
  647. /*int nSensorType;
  648. if( !strSensorType.Compare((char*)(LPCTSTR)g_strTempTypeDesc) )
  649. {
  650. nSensorType = 1;
  651. }
  652. else if( !strSensorType.Compare((char*)(LPCTSTR)g_str4To20TypeDesc) )
  653. {
  654. nSensorType = 2;
  655. }
  656. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strHumidityTypeDesc) )
  657. {
  658. nSensorType = 3;
  659. }
  660. else if( !strSensorType.Compare("Water") )
  661. {
  662. nSensorType = 4;
  663. }
  664. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strDCTypeDesc) )
  665. {
  666. nSensorType = 5;
  667. }
  668. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strSecurityTypeDesc) )
  669. {
  670. nSensorType = 6;
  671. }
  672. else if( !strSensorType.Compare("Airflow") )
  673. {
  674. nSensorType = 8;
  675. }
  676. else if( !strSensorType.Compare("Siren") )
  677. {
  678. nSensorType = 9;
  679. }
  680. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strDryTypeDesc) )
  681. {
  682. nSensorType = 10;
  683. }
  684. else if( !strSensorType.Compare("AC Voltage") )
  685. {
  686. nSensorType = 12;
  687. }
  688. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strRelayTypeDesc) )
  689. {
  690. nSensorType = 13;
  691. }
  692. else if( !strSensorType.Compare(SENSOR_AI_DESC) )
  693. {
  694. nSensorType = 20;
  695. }
  696. else if( !strSensorType.Compare(SENSOR_DI_DESC) )
  697. {
  698. nSensorType = 21;
  699. }
  700. else if( !strSensorType.Compare(SENSOR_DO_DESC) )
  701. {
  702. nSensorType = 22;
  703. }
  704. else
  705. {
  706. nSensorType = 0;
  707. }
  708. return nSensorType;*/
  709. return true;
  710. }
  711. CString GetSensorType( int nSensorType )
  712. {
  713. /* CString strSensorType = "";
  714. switch( nSensorType )
  715. {
  716. case 1:
  717. strSensorType = CString(" ") + g_strTempTypeDesc;
  718. break;
  719. case 2:
  720. strSensorType = CString(" ") + g_str4To20TypeDesc;
  721. break;
  722. case 3:
  723. strSensorType = CString(" ") + g_strHumidityTypeDesc;
  724. break;
  725. case 4:
  726. strSensorType = " Water";
  727. break;
  728. case 5:
  729. strSensorType = CString(" ") + g_strDCTypeDesc;
  730. break;
  731. case 6:
  732. strSensorType = CString(" ") + g_strSecurityTypeDesc;
  733. break;
  734. case 8:
  735. strSensorType = " Airflow";
  736. break;
  737. case 9:
  738. strSensorType = " Siren";
  739. break;
  740. case 10:
  741. strSensorType = CString(" ") + g_strDryTypeDesc;
  742. break;
  743. case 12:
  744. strSensorType = " AC Voltage";
  745. break;
  746. case 13:
  747. strSensorType = CString(" ") + g_strRelayTypeDesc;
  748. break;
  749. case 14:
  750. strSensorType = " Motion";
  751. break;
  752. case 20:
  753. strSensorType = " AI";
  754. break;
  755. case 21:
  756. strSensorType = " DI";
  757. break;
  758. case 22:
  759. strSensorType = " DO";
  760. break;
  761. }
  762. return strSensorType;*/
  763. return "";
  764. }
  765. CString InterceptString(int qlen, CString strSource)
  766. {
  767. int len,i,y;
  768. CString sTemp,sreturn;
  769. strSource.TrimLeft();
  770. strSource.TrimRight();
  771. len=strSource.GetLength();
  772. y=0;
  773. sTemp=strSource.Right(len-qlen);
  774. for(i=0;i<len;i++)
  775. {
  776. //if(sTemp[y]<0 || sTemp[y]>255)
  777. //if(sTemp[i]<0 || sTemp[i]>=128)
  778. //if(sTemp.GetAt(y) & 0x80 )
  779. if(::IsDBCSLeadByte(sTemp.GetAt(y)))
  780. y=y+2;
  781. else
  782. y=y+1;
  783. if(y>=70)
  784. break;
  785. }
  786. sreturn=sTemp.Left(y);
  787. return sreturn;
  788. }
  789. //显示选择文件夹窗口
  790. //参数 sFolderPath:用于返回用户选择的文件夹的路径
  791. //参数 sTitle:用于指定选择文件夹窗口的标题
  792. //返回值 :操作结果,用户取消选择或操作失败返回FALSE,否则TRUE
  793. BOOL BrowseForFolder(CString & sFolderPath, CString sTitle)
  794. {
  795. BROWSEINFO bi;
  796. char Buffer[_MAX_PATH];
  797. bi.hwndOwner = NULL;
  798. bi.pidlRoot = NULL;
  799. bi.pszDisplayName = Buffer;
  800. bi.lpszTitle = sTitle;
  801. bi.ulFlags = 0;
  802. bi.lpfn = NULL;
  803. LPITEMIDLIST pIDList = SHBrowseForFolder(& bi);
  804. if (!pIDList)
  805. return FALSE;
  806. SHGetPathFromIDList(pIDList, Buffer);
  807. sFolderPath = Buffer;
  808. LPMALLOC lpMalloc;
  809. if (FAILED(SHGetMalloc(& lpMalloc)))
  810. return FALSE;
  811. //释放内存
  812. lpMalloc->Free(pIDList);
  813. lpMalloc->Release();
  814. return TRUE;
  815. }
  816. void TimeTicksToInt(CString strSrc, unsigned long &nDest)
  817. {
  818. CString strLeft;
  819. int nIndex = strSrc.Find(":");
  820. if( nIndex != -1 )
  821. {
  822. strLeft = strSrc.Left( nIndex - 1 );
  823. nDest = atoi(strLeft);
  824. }
  825. }
  826. unsigned int CalcCheckSum( void *pData, unsigned int nSize )
  827. {
  828. unsigned int checksum = 0;
  829. if ( nSize <= sizeof( ProtocolHeader ) )
  830. {
  831. return 0;
  832. }
  833. unsigned char *pBody = &( ( unsigned char* )pData )[ sizeof( ProtocolHeader ) ];
  834. nSize -= sizeof( ProtocolHeader );
  835. if( pBody )
  836. {
  837. checksum = crc32( 0, pBody, nSize );
  838. }
  839. return checksum;
  840. }
  841. int SplitStr(CHAR *strSource, CHAR cChar, int iItem, CHAR *strGet)
  842. {
  843. int iIndex = 0;
  844. int iIndexThis = -1;
  845. int iIndexNext = -1;
  846. int iLength = 0;
  847. int iItemThis = 0;
  848. CHAR strFileName[MAX_FILE_LENGTH + 1] = "";
  849. iLength = strlen(strSource);
  850. while (iIndex < iLength)
  851. {
  852. if (strSource[iIndex] == cChar)
  853. {
  854. iItemThis++;
  855. if (iItemThis == iItem)
  856. iIndexThis = iIndex;
  857. else if (iItemThis == iItem + 1)
  858. {
  859. iIndexNext = iIndex;
  860. break;
  861. }
  862. }
  863. iIndex++;
  864. }
  865. if (iIndexNext > 0 && iIndexNext > iIndexThis)
  866. {
  867. memcpy(strGet, strSource + iIndexThis + 1, iIndexNext - iIndexThis - 1);
  868. strGet[iIndexNext - iIndexThis - 1] = '\0';
  869. }
  870. else
  871. strcpy(strGet, "");
  872. return 0;
  873. }