SysLib.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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, "SToneU-isP-CMS", NULL, REG_SZ, (BYTE*)strExeFile, (DWORD)strlen(strExeFile));
  63. else
  64. dwErrorCode = RegDeleteKey(hKey, "SToneU-isP-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\\set.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. g_nLevelWav = GetPrivateProfileInt("Common", "LevelWav", 3, strFile);
  397. g_nLevelSms = GetPrivateProfileInt("Common", "LevelSms", 3, strFile);
  398. g_nLevelEmail = GetPrivateProfileInt("Common", "LevelEmail", 3, strFile);
  399. g_nLevelDevInterrupt = GetPrivateProfileInt("Common", "LevelDevInterrupt", 8, strFile);
  400. g_nLevelDlg = GetPrivateProfileInt("Common", "LevelDlg", 1, strFile);
  401. g_nLevelLocalWav = GetPrivateProfileInt("Common", "LevelLocalWav", 1, strFile);
  402. g_nDBRecordDate = GetPrivateProfileInt("Common", "DbRecordDate", 365, strFile);
  403. //Email
  404. g_nSendEmail = GetPrivateProfileInt("Email", "SendEmail", 0, strFile);
  405. GetPrivateProfileString("Email", "SMTPServer", "", g_strEmailSMTPSrv, sizeof(g_strEmailSMTPSrv), strFile);
  406. g_nEmailSMTPPort = GetPrivateProfileInt("Email", "SMTPPort", 25, strFile);
  407. g_nEmailIsNeed = GetPrivateProfileInt("Email", "IsNeed", 1, strFile);
  408. GetPrivateProfileString("Email", "UserAcc", "", g_strEmailUserAcc, sizeof(g_strEmailUserAcc), strFile);
  409. GetPrivateProfileString("Email", "UserPwd", "", g_strEmailUserPwd, sizeof(g_strEmailUserPwd), strFile);
  410. GetPrivateProfileString("Email", "MailFrom", "", g_strEmailFromAddr, sizeof(g_strEmailFromAddr), strFile);
  411. g_nEmailTimeOut = GetPrivateProfileInt("Email", "TimeOut", 60, strFile);
  412. GetPrivateProfileString("Email", "Subject", "", g_strEmailSubject, sizeof(g_strEmailSubject), strFile);
  413. g_nEmailCallTimes = GetPrivateProfileInt("Email", "ReCallTimes", 2, strFile);
  414. //Sms
  415. g_nSendSms = GetPrivateProfileInt("Sms", "SendSms", 0, strFile);
  416. GetPrivateProfileString("Sms", "Port", "", g_strSmsComPort, sizeof(g_strSmsComPort), strFile);
  417. g_nSmsRate = GetPrivateProfileInt("Sms", "Rate", 115200, strFile);
  418. g_nSmsDataBit = GetPrivateProfileInt("Sms", "DataBit", 8, strFile);
  419. g_nSmsStopBit = GetPrivateProfileInt("Sms", "StopBit", 1, strFile);
  420. g_nSmsParity = GetPrivateProfileInt("Sms", "Parity", 0, strFile);
  421. g_nSmsInterval = GetPrivateProfileInt("Sms", "Interval", 0, strFile);
  422. g_nSmsLanguageTrans = GetPrivateProfileInt("Sms", "LanguageTrans", 0, strFile);
  423. g_nSmsMaxChar = GetPrivateProfileInt("Sms", "MaxChar", 0, strFile);
  424. g_nSmsMakeCall = GetPrivateProfileInt("Sms", "MakeCall", 0, strFile);
  425. GetPrivateProfileString("Sms", "SMSC", "", g_strSmsSMSC, sizeof(g_strSmsSMSC), strFile);
  426. g_nSmsTimOut = GetPrivateProfileInt("Sms", "TimeOut", 60, strFile);
  427. g_nSmsCallTimes = GetPrivateProfileInt("Sms", "ReCallTimes", 2, strFile);
  428. //Voice
  429. g_nSendNotice = GetPrivateProfileInt("Voice", "SendNotice", 0, strFile);
  430. g_nNoticeCardType = GetPrivateProfileInt("Voice", "CardType", 0, strFile);
  431. GetPrivateProfileString("Voice", "PreTel", "", g_strNoticeTelPre, sizeof(g_strNoticeTelPre), strFile);
  432. g_nNoticeCallDelay = GetPrivateProfileInt("Voice", "CallRelay", 60, strFile);
  433. g_nNoticeCallTimes = GetPrivateProfileInt("Voice", "ReCallTimes", 2, strFile);
  434. //TTS
  435. g_nTTSType = GetPrivateProfileInt("TTS", "VoiceType", 0, strFile);
  436. //AlarmMode
  437. g_nAlarmModeIsDlg = GetPrivateProfileInt("AlarmMode", "Dlg", 0, strFile);
  438. g_nAlarmModeShowDlgTimes = GetPrivateProfileInt("AlarmMode", "ShowTime", 1, strFile);
  439. g_nAlarmModeIsSound = GetPrivateProfileInt("AlarmMode", "Sound", 0, strFile);
  440. g_nAlarmModeSoundNum = GetPrivateProfileInt("AlarmMode", "PlayNum", 1, strFile);
  441. GetPrivateProfileString("Other", "SummaryLog", "", g_strSummaryLogPath, sizeof(g_strSummaryLogPath), strFile);
  442. //定时发送短信和电话参数配置
  443. g_nTimingEnable = GetPrivateProfileInt("Timing", "Enable", 0, strFile);
  444. g_nTimingWeeks = GetPrivateProfileInt("Timing", "Weeks", 0, strFile);
  445. g_nTimingHours = GetPrivateProfileInt("Timing", "Hours", 0, strFile);
  446. g_nTimingMinutes = GetPrivateProfileInt("Timing", "Minutes", 0, strFile);
  447. //变量输出,特别为上海烟草局做的
  448. g_nOutputEnable = GetPrivateProfileInt("output", "Enable", 0, strFile);
  449. GetPrivateProfileString("output", "path", "", g_strOutputPath, sizeof(g_strOutputPath), strFile);
  450. GetPrivateProfileString("output", "filename", "", g_strOutputFileName, sizeof(g_strOutputFileName), strFile);
  451. GetPrivateProfileString("output", "clock", "", g_strOutputClock, sizeof(g_strOutputClock), strFile);
  452. GetPrivateProfileString("output", "line1", "", g_strOutputLine1, sizeof(g_strOutputLine1), strFile);
  453. GetPrivateProfileString("output", "line2", "", g_strOutputLine2, sizeof(g_strOutputLine2), strFile);
  454. GetPrivateProfileString("output", "line3", "", g_strOutputLine3, sizeof(g_strOutputLine3), strFile);
  455. GetPrivateProfileString("output", "line4", "", g_strOutputLine4, sizeof(g_strOutputLine4), strFile);
  456. GetPrivateProfileString("output", "line5", "", g_strOutputLine5, sizeof(g_strOutputLine5), strFile);
  457. GetPrivateProfileString("output", "line6", "", g_strOutputLine6, sizeof(g_strOutputLine6), strFile);
  458. GetPrivateProfileString("output", "line7", "", g_strOutputLine7, sizeof(g_strOutputLine7), strFile);
  459. GetPrivateProfileString("output", "line8", "", g_strOutputLine8, sizeof(g_strOutputLine8), strFile);
  460. GetPrivateProfileString("output", "line9", "", g_strOutputLine9, sizeof(g_strOutputLine9), strFile);
  461. GetPrivateProfileString("output", "line10", "", g_strOutputLine10, sizeof(g_strOutputLine10), strFile);
  462. g_nOutputLineCount1 = GetPrivateProfileInt("output", "lineCount1", 0, strFile);
  463. g_nOutputLineCount2 = GetPrivateProfileInt("output", "lineCount2", 0, strFile);
  464. g_nOutputLineCount3 = GetPrivateProfileInt("output", "lineCount3", 0, strFile);
  465. g_nOutputLineCount4 = GetPrivateProfileInt("output", "lineCount4", 0, strFile);
  466. g_nOutputLineCount5 = GetPrivateProfileInt("output", "lineCount5", 0, strFile);
  467. g_nOutputLineCount6 = GetPrivateProfileInt("output", "lineCount6", 0, strFile);
  468. g_nOutputLineCount7 = GetPrivateProfileInt("output", "lineCount7", 0, strFile);
  469. g_nOutputLineCount8 = GetPrivateProfileInt("output", "lineCount8", 0, strFile);
  470. g_nOutputLineCount9 = GetPrivateProfileInt("output", "lineCount9", 0, strFile);
  471. g_nOutputLineCount10 = GetPrivateProfileInt("output", "lineCount10", 0, strFile);
  472. GetPrivateProfileString("output", "lineUid1", "", g_strOutputLineUid1, sizeof(g_strOutputLineUid1), strFile);
  473. GetPrivateProfileString("output", "lineUid2", "", g_strOutputLineUid2, sizeof(g_strOutputLineUid2), strFile);
  474. GetPrivateProfileString("output", "lineUid3", "", g_strOutputLineUid3, sizeof(g_strOutputLineUid3), strFile);
  475. GetPrivateProfileString("output", "lineUid4", "", g_strOutputLineUid4, sizeof(g_strOutputLineUid4), strFile);
  476. GetPrivateProfileString("output", "lineUid5", "", g_strOutputLineUid5, sizeof(g_strOutputLineUid5), strFile);
  477. GetPrivateProfileString("output", "lineUid6", "", g_strOutputLineUid6, sizeof(g_strOutputLineUid6), strFile);
  478. GetPrivateProfileString("output", "lineUid7", "", g_strOutputLineUid7, sizeof(g_strOutputLineUid7), strFile);
  479. GetPrivateProfileString("output", "lineUid8", "", g_strOutputLineUid8, sizeof(g_strOutputLineUid8), strFile);
  480. GetPrivateProfileString("output", "lineUid9", "", g_strOutputLineUid9, sizeof(g_strOutputLineUid9), strFile);
  481. GetPrivateProfileString("output", "lineUid10", "", g_strOutputLineUid10, sizeof(g_strOutputLineUid10), strFile);
  482. g_nSnmpEnable = GetPrivateProfileInt("Snmp", "Enable", 0, strFile);
  483. GetPrivateProfileString("Snmp", "ObjOid", "", g_strSnmpObjOid, sizeof(g_strSnmpObjOid), strFile);
  484. GetPrivateProfileString("Snmp", "IP", "", g_strSnmpIP, sizeof(g_strSnmpIP), strFile);
  485. GetPrivateProfileString("Snmp", "Field", "", g_strSnmpField, sizeof(g_strSnmpField), strFile);
  486. ////////////////////////////////////////////////////////////////////////////
  487. ////////////////////////////////////////////////////////////////////////////
  488. //增加自动启动注册表项
  489. #if 0
  490. wsprintf(strFile, "%s\\%s", g_strDirectory, g_strAppName);
  491. AddAutoSysRun(strFile, g_iAutoRunSystem);
  492. #endif
  493. return 0;
  494. }
  495. int GetResourceString()
  496. {
  497. g_strServiceName.LoadString(IDS_SERVICE_NAME);
  498. g_strHintSysRun.LoadString(IDS_HINT_SYS_RUN);
  499. g_strHintAlarmWelcome.LoadString(IDS_HINT_ALARM_WELCOME);
  500. g_strHintVoiceEnd.LoadString(IDS_HINT_VOICE_END);
  501. g_strHintReturnNormal.LoadString(IDS_HINT_RETURN_NORMAL);
  502. g_strHintUpperLimit.LoadString(IDS_HINT_UPPER_LIMIT);
  503. g_strHintLowerLimit.LoadString(IDS_HINT_LOWER_LIMIT);
  504. g_strHintCurrValue.LoadString(IDS_HINT_CURR_VALUE);
  505. return 0;
  506. }
  507. VOID TerminateLastProcess()
  508. {
  509. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  510. if (hSnapShot != NULL)
  511. {
  512. PROCESSENTRY32 ProcessInfo = {0};//声明进程信息变量
  513. ProcessInfo.dwSize = sizeof(ProcessInfo);//设置ProcessInfo的大小
  514. //返回系统中第一个进程的信息
  515. BOOL bStatus = Process32First(hSnapShot, &ProcessInfo);
  516. while (bStatus)
  517. {
  518. if (!_stricmp(g_strAppName, ProcessInfo.szExeFile))//映像名称
  519. {
  520. HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessInfo.th32ProcessID);
  521. ::TerminateProcess(hProcess, 1);
  522. hProcess = NULL;
  523. Sleep(1000);
  524. break;
  525. }
  526. else
  527. //获取下一个进程的信息
  528. bStatus = Process32Next(hSnapShot, &ProcessInfo);
  529. }
  530. }
  531. }
  532. //-------------------------------------------------------------------------------------
  533. //Description:
  534. // This function maps a character string to a wide-character (Unicode) string
  535. //
  536. //Parameters:
  537. // lpcszStr: [in] Pointer to the character string to be converted
  538. // lpwszStr: [out] Pointer to a buffer that receives the translated string.
  539. // dwSize: [in] Size of the buffer
  540. //
  541. //Return Values:
  542. // TRUE: Succeed
  543. // FALSE: Failed
  544. //
  545. //Example:
  546. // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
  547. //---------------------------------------------------------------------------------------
  548. BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
  549. {
  550. // Get the required size of the buffer that receives the Unicode
  551. // string.
  552. DWORD dwMinSize;
  553. dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
  554. if(dwSize < dwMinSize)
  555. {
  556. return FALSE;
  557. }
  558. // Convert headers from ASCII to Unicode.
  559. MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);
  560. return TRUE;
  561. }
  562. //-------------------------------------------------------------------------------------
  563. //Description:
  564. // This function maps a wide-character string to a new character string
  565. //
  566. //Parameters:
  567. // lpcwszStr: [in] Pointer to the character string to be converted
  568. // lpszStr: [out] Pointer to a buffer that receives the translated string.
  569. // dwSize: [in] Size of the buffer
  570. //
  571. //Return Values:
  572. // TRUE: Succeed
  573. // FALSE: Failed
  574. //
  575. //Example:
  576. // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
  577. //---------------------------------------------------------------------------------------
  578. BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
  579. {
  580. DWORD dwMinSize;
  581. dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
  582. if(dwSize < dwMinSize)
  583. {
  584. return FALSE;
  585. }
  586. WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
  587. return TRUE;
  588. }
  589. //////////////////////////////////////////////////////////////////////////////
  590. //名称:GetExcelDriver
  591. //功能:获取ODBC中Excel驱动
  592. //作者:徐景周(jingzhou_xu@163.net)
  593. //组织:未来工作室(Future Studio)
  594. //日期:2002.9.1
  595. /////////////////////////////////////////////////////////////////////////////
  596. CString GetExcelDriver()
  597. {
  598. char szBuf[2001];
  599. WORD cbBufMax = 2000;
  600. WORD cbBufOut;
  601. char *pszBuf = szBuf;
  602. CString sDriver;
  603. // 获取已安装驱动的名称(涵数在odbcinst.h里)
  604. if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
  605. return "";
  606. // 检索已安装的驱动是否有Excel...
  607. do
  608. {
  609. if (strstr(pszBuf, "Excel") != 0)
  610. {
  611. //发现 !
  612. sDriver = CString(pszBuf);
  613. break;
  614. }
  615. pszBuf = strchr(pszBuf, '\0') + 1;
  616. }
  617. while (pszBuf[1] != '\0');
  618. return sDriver;
  619. }
  620. ///////////////////////////////////////////////////////////////////////////////
  621. // BOOL MakeSurePathExists( CString &Path,bool FilenameIncluded)
  622. // 参数:
  623. // Path 路径
  624. // FilenameIncluded 路径是否包含文件名
  625. // 返回值:
  626. // 文件是否存在
  627. // 说明:
  628. // 判断Path文件(FilenameIncluded=true)是否存在,存在返回TURE,不存在返回FALSE
  629. // 自动创建目录
  630. //
  631. ///////////////////////////////////////////////////////////////////////////////
  632. BOOL MakeSurePathExists( CString &Path,
  633. bool FilenameIncluded)
  634. {
  635. int Pos=0;
  636. while((Pos=Path.Find('\\',Pos+1))!=-1)
  637. CreateDirectory(Path.Left(Pos),NULL);
  638. if(!FilenameIncluded)
  639. CreateDirectory(Path,NULL);
  640. // return ((!FilenameIncluded)?!_access(Path,0):
  641. // !_access(Path.Left(Path.ReverseFind('\\')),0));
  642. return !_access(Path,0);
  643. }
  644. int GetSensorType( CString strSensorType )
  645. {
  646. /*int nSensorType;
  647. if( !strSensorType.Compare((char*)(LPCTSTR)g_strTempTypeDesc) )
  648. {
  649. nSensorType = 1;
  650. }
  651. else if( !strSensorType.Compare((char*)(LPCTSTR)g_str4To20TypeDesc) )
  652. {
  653. nSensorType = 2;
  654. }
  655. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strHumidityTypeDesc) )
  656. {
  657. nSensorType = 3;
  658. }
  659. else if( !strSensorType.Compare("Water") )
  660. {
  661. nSensorType = 4;
  662. }
  663. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strDCTypeDesc) )
  664. {
  665. nSensorType = 5;
  666. }
  667. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strSecurityTypeDesc) )
  668. {
  669. nSensorType = 6;
  670. }
  671. else if( !strSensorType.Compare("Airflow") )
  672. {
  673. nSensorType = 8;
  674. }
  675. else if( !strSensorType.Compare("Siren") )
  676. {
  677. nSensorType = 9;
  678. }
  679. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strDryTypeDesc) )
  680. {
  681. nSensorType = 10;
  682. }
  683. else if( !strSensorType.Compare("AC Voltage") )
  684. {
  685. nSensorType = 12;
  686. }
  687. else if( !strSensorType.Compare((char*)(LPCTSTR)g_strRelayTypeDesc) )
  688. {
  689. nSensorType = 13;
  690. }
  691. else if( !strSensorType.Compare(SENSOR_AI_DESC) )
  692. {
  693. nSensorType = 20;
  694. }
  695. else if( !strSensorType.Compare(SENSOR_DI_DESC) )
  696. {
  697. nSensorType = 21;
  698. }
  699. else if( !strSensorType.Compare(SENSOR_DO_DESC) )
  700. {
  701. nSensorType = 22;
  702. }
  703. else
  704. {
  705. nSensorType = 0;
  706. }
  707. return nSensorType;*/
  708. return true;
  709. }
  710. CString GetSensorType( int nSensorType )
  711. {
  712. /* CString strSensorType = "";
  713. switch( nSensorType )
  714. {
  715. case 1:
  716. strSensorType = CString(" ") + g_strTempTypeDesc;
  717. break;
  718. case 2:
  719. strSensorType = CString(" ") + g_str4To20TypeDesc;
  720. break;
  721. case 3:
  722. strSensorType = CString(" ") + g_strHumidityTypeDesc;
  723. break;
  724. case 4:
  725. strSensorType = " Water";
  726. break;
  727. case 5:
  728. strSensorType = CString(" ") + g_strDCTypeDesc;
  729. break;
  730. case 6:
  731. strSensorType = CString(" ") + g_strSecurityTypeDesc;
  732. break;
  733. case 8:
  734. strSensorType = " Airflow";
  735. break;
  736. case 9:
  737. strSensorType = " Siren";
  738. break;
  739. case 10:
  740. strSensorType = CString(" ") + g_strDryTypeDesc;
  741. break;
  742. case 12:
  743. strSensorType = " AC Voltage";
  744. break;
  745. case 13:
  746. strSensorType = CString(" ") + g_strRelayTypeDesc;
  747. break;
  748. case 14:
  749. strSensorType = " Motion";
  750. break;
  751. case 20:
  752. strSensorType = " AI";
  753. break;
  754. case 21:
  755. strSensorType = " DI";
  756. break;
  757. case 22:
  758. strSensorType = " DO";
  759. break;
  760. }
  761. return strSensorType;*/
  762. return "";
  763. }
  764. CString InterceptString(int qlen, CString strSource)
  765. {
  766. int len,i,y;
  767. CString sTemp,sreturn;
  768. strSource.TrimLeft();
  769. strSource.TrimRight();
  770. len=strSource.GetLength();
  771. y=0;
  772. sTemp=strSource.Right(len-qlen);
  773. for(i=0;i<len;i++)
  774. {
  775. //if(sTemp[y]<0 || sTemp[y]>255)
  776. //if(sTemp[i]<0 || sTemp[i]>=128)
  777. //if(sTemp.GetAt(y) & 0x80 )
  778. if(::IsDBCSLeadByte(sTemp.GetAt(y)))
  779. y=y+2;
  780. else
  781. y=y+1;
  782. if(y>=70)
  783. break;
  784. }
  785. sreturn=sTemp.Left(y);
  786. return sreturn;
  787. }
  788. //显示选择文件夹窗口
  789. //参数 sFolderPath:用于返回用户选择的文件夹的路径
  790. //参数 sTitle:用于指定选择文件夹窗口的标题
  791. //返回值 :操作结果,用户取消选择或操作失败返回FALSE,否则TRUE
  792. BOOL BrowseForFolder(CString & sFolderPath, CString sTitle)
  793. {
  794. BROWSEINFO bi;
  795. char Buffer[_MAX_PATH];
  796. bi.hwndOwner = NULL;
  797. bi.pidlRoot = NULL;
  798. bi.pszDisplayName = Buffer;
  799. bi.lpszTitle = sTitle;
  800. bi.ulFlags = 0;
  801. bi.lpfn = NULL;
  802. LPITEMIDLIST pIDList = SHBrowseForFolder(& bi);
  803. if (!pIDList)
  804. return FALSE;
  805. SHGetPathFromIDList(pIDList, Buffer);
  806. sFolderPath = Buffer;
  807. LPMALLOC lpMalloc;
  808. if (FAILED(SHGetMalloc(& lpMalloc)))
  809. return FALSE;
  810. //释放内存
  811. lpMalloc->Free(pIDList);
  812. lpMalloc->Release();
  813. return TRUE;
  814. }
  815. void TimeTicksToInt(CString strSrc, unsigned long &nDest)
  816. {
  817. CString strLeft;
  818. int nIndex = strSrc.Find(":");
  819. if( nIndex != -1 )
  820. {
  821. strLeft = strSrc.Left( nIndex - 1 );
  822. nDest = atoi(strLeft);
  823. }
  824. }
  825. unsigned int CalcCheckSum( void *pData, unsigned int nSize )
  826. {
  827. unsigned int checksum = 0;
  828. if ( nSize <= sizeof( ProtocolHeader ) )
  829. {
  830. return 0;
  831. }
  832. unsigned char *pBody = &( ( unsigned char* )pData )[ sizeof( ProtocolHeader ) ];
  833. nSize -= sizeof( ProtocolHeader );
  834. if( pBody )
  835. {
  836. checksum = crc32( 0, pBody, nSize );
  837. }
  838. return checksum;
  839. }
  840. int SplitStr(CHAR *strSource, CHAR cChar, int iItem, CHAR *strGet)
  841. {
  842. int iIndex = 0;
  843. int iIndexThis = -1;
  844. int iIndexNext = -1;
  845. int iLength = 0;
  846. int iItemThis = 0;
  847. CHAR strFileName[MAX_FILE_LENGTH + 1] = "";
  848. iLength = strlen(strSource);
  849. while (iIndex < iLength)
  850. {
  851. if (strSource[iIndex] == cChar)
  852. {
  853. iItemThis++;
  854. if (iItemThis == iItem)
  855. iIndexThis = iIndex;
  856. else if (iItemThis == iItem + 1)
  857. {
  858. iIndexNext = iIndex;
  859. break;
  860. }
  861. }
  862. iIndex++;
  863. }
  864. if (iIndexNext > 0 && iIndexNext > iIndexThis)
  865. {
  866. memcpy(strGet, strSource + iIndexThis + 1, iIndexNext - iIndexThis - 1);
  867. strGet[iIndexNext - iIndexThis - 1] = '\0';
  868. }
  869. else
  870. strcpy(strGet, "");
  871. return 0;
  872. }