SysLib.cpp 31 KB

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