Global.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. #include "StdAfx.h"
  2. #include "Global.h"
  3. #include "SoftKey.h"
  4. #include "CritSection.h"
  5. #include "./helper/ffsco.h"
  6. TCHAR g_ModulePath[MAX_PATH] = _T(""); // 软件目录;
  7. TCHAR g_ModuleFileName[MAX_PATH] = _T(""); // 软件名称;
  8. TCHAR g_szConnectString[MAX_PATH] = _T(""); // DB连接串;
  9. // 服务器信息;
  10. TCHAR g_szDBSource[MAX_PATH] = _T(""); // 数据库源(服务所在IP或计算机名);
  11. TCHAR g_szDBSourcePort[MAX_PATH] = _T(""); // 数据库源端口;
  12. DWORD g_dwDBServerPort = 0; // 数据库源端口;
  13. TCHAR g_szDBAccount[MAX_PATH] = _T(""); // 数据库登录用户;
  14. TCHAR g_szDBPassWord[MAX_PATH] = _T(""); // 数据库登录密码;
  15. TCHAR g_szDBName[MAX_PATH] = _T(""); // 数据库名称;
  16. // start+------------------------------------------------------------------
  17. // Jeff:以下为原本程序全局变量;
  18. #if THE_PRIMARY_GLOBAL_VARIABLE
  19. int g_dwTCPPort = 5678; // Jeff.通信端口;
  20. BOOL g_bReg = 0;
  21. CString g_strdbpsw; // 数据库账号、密码;
  22. CString g_strdbServrename; // 数据库服务名(地址)
  23. CString g_bakinfo;
  24. CString g_bakphoto;
  25. CString g_mainpath;
  26. SoftKey ytSoftKey;
  27. CString g_localname; // 本地计算机名;
  28. CDatabase g_db; // 名为"db"的数据库,本年度使用的数据库;
  29. CArray<int, int> g_nYearArray; // 年份(用于命名历史数据库)
  30. //CArray<CDatabase*,CDatabase*>g_dbArray; // 历史数据库;
  31. CArray<CDatabase*, CDatabase*>g_dbHistoryArray; // 历史数据库;
  32. CArray<CDatabase*, CDatabase*>g_branchdbarray;
  33. CArray<CDatabase*, CDatabase*>g_branchdb2array;//除历史年度的分店DB
  34. CStringArray g_branchnamearray;
  35. CStringArray g_branchname2array;
  36. CStringArray g_branchiparray;
  37. CStringArray g_branchdomainarray;
  38. CDatabase *g_curdb = NULL;
  39. int g_conncount = -1; // Jeff.加密狗中的座席版数量;
  40. CString g_sdomain;
  41. CStringArray g_conniparray;
  42. CStringArray g_connidarray;
  43. BOOL g_bCheckAll = 0;
  44. BYTE *g_pSkinData = NULL;
  45. DWORD g_SkinLeng;
  46. BOOL g_bAutoRun = 0;
  47. CStringArray g_filedatearray;
  48. CStringArray g_filedatearraynew;
  49. CString g_bakdate;
  50. CString g_localip;
  51. BOOL g_bSteal = 0;
  52. CString g_bakserver1;
  53. CString g_bakserver2;
  54. CString g_bakserver3;
  55. CString g_bakserver4;
  56. DWORD g_tisks = 0;
  57. #endif
  58. // endof+------------------------------------------------------------------
  59. //************************************//
  60. // 数据库地址;
  61. // 数据库端口;
  62. // 数据库管理员账号;
  63. // 数据库管理员密码;
  64. // 数据库名称;
  65. //
  66. // [函数]:GetIniInfo
  67. // [描述]:获取指定ini信息;
  68. // [参数]:
  69. // szPath:ini所在目录;
  70. // szIniName:ini名;
  71. // [返回]:void
  72. //
  73. //************************************//
  74. int GetIniInfo(const char *szPath, const char *szIniName)
  75. {
  76. TCHAR szDrive[_MAX_DRIVE];
  77. TCHAR szDir[_MAX_DIR];
  78. ::GetModuleFileName(NULL, g_ModulePath, sizeof(g_ModulePath) / sizeof(TCHAR));
  79. sprintf(g_ModuleFileName, "%s", g_ModulePath);
  80. _tsplitpath(g_ModulePath, szDrive, szDir, NULL, NULL);
  81. _tcscpy(g_ModulePath, szDrive);
  82. _tcscat(g_ModulePath, szDir);
  83. // -----------------------------------------------------//
  84. CHAR szFile[MAX_PATH + 1] = "";
  85. if (szPath != NULL && szIniName != NULL)
  86. wsprintf(szFile, "%s\\%s", szPath, szIniName);
  87. else
  88. wsprintf(szFile, "%s\\ServiceInfo.ini", g_ModulePath);
  89. TRACE("szFile =%s\n", szFile);
  90. OFSTRUCT ofStruct;
  91. OpenFile(szFile, &ofStruct, OF_EXIST);
  92. if (ERROR_FILE_NOT_FOUND == GetLastError())
  93. {
  94. return -1;
  95. }
  96. // 获取服务器端信息;
  97. GetPrivateProfileString("DatabaseInfo", "dbSource", "", g_szDBSource, MAX_PATH, szFile);
  98. g_dwDBServerPort = GetPrivateProfileInt("DatabaseInfo", "dbServerPort", 0, szFile);
  99. GetPrivateProfileString("DatabaseInfo", "dbAccount", "", g_szDBAccount, MAX_PATH, szFile);
  100. GetPrivateProfileString("DatabaseInfo", "dbPassWord", "", g_szDBPassWord, MAX_PATH, szFile);
  101. GetPrivateProfileString("DatabaseInfo", "dbName", "", g_szDBName, MAX_PATH, szFile);
  102. g_dwTCPPort = GetPrivateProfileInt("NetWorkInfo", "TCPChatPort", 5678, szFile);
  103. if (g_dwDBServerPort != 0)
  104. sprintf(g_szConnectString, "driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s",
  105. g_szDBSource, g_dwDBServerPort, g_szDBName, g_szDBAccount, g_szDBPassWord);
  106. else
  107. sprintf(g_szConnectString, "driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s",
  108. g_szDBSource, g_szDBName, g_szDBAccount, g_szDBPassWord);
  109. return 0;
  110. }
  111. //------------------------------------------------------------
  112. // 以下全局函数为原程序所有;
  113. #if THE_PRIMARY_GLOBAL_FUNCTION
  114. BOOL AddConn(CDatabase *m_conndb, CString dbname)
  115. {
  116. try {
  117. CString strCon;
  118. strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=%s;%s"), g_strdbServrename, dbname, g_strdbpsw);
  119. m_conndb->OpenEx(strCon, CDatabase::noOdbcDialog);
  120. }
  121. catch (CDBException * e)
  122. {
  123. WriteLogin("新建连接时出错:" + e->m_strError);
  124. e->Delete();
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. void WriteError(CString str)
  130. {
  131. MyLock lock("DBServerWriteError");
  132. CStdioFile fp;
  133. CString path = g_mainpath + "\\MsgError.txt";
  134. if (::PathFileExists(path))
  135. {
  136. fp.Open(path, CFile::modeWrite);
  137. int length = fp.GetLength();
  138. if (length > 1024 * 1024)
  139. {
  140. fp.Close();
  141. ::DeleteFile(path); // 删除MsgError.txt
  142. return;
  143. }
  144. fp.SeekToEnd();
  145. }
  146. else
  147. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  148. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
  149. fp.Close();
  150. }
  151. void WritePswWrong(CString str)
  152. {
  153. MyLock lock("DBServerWritePswWrong");
  154. CStdioFile fp;
  155. CString path = g_mainpath + "\\PswWrong.txt";
  156. if (::PathFileExists(path))
  157. {
  158. fp.Open(path, CFile::modeWrite);
  159. int length = fp.GetLength();
  160. if (length > 1024 * 1024)
  161. {
  162. fp.Close();
  163. ::DeleteFile(path); // 删除PswWrong.txt
  164. return;
  165. }
  166. fp.SeekToEnd();
  167. }
  168. else
  169. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  170. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
  171. fp.Close();
  172. }
  173. void WriteNullBalance(CString str)
  174. {
  175. MyLock lock("DBServerWriteNullBalance");
  176. CStdioFile fp;
  177. CString path = g_mainpath + "\\NullBalance.txt";
  178. if (::PathFileExists(path))
  179. {
  180. fp.Open(path, CFile::modeWrite);
  181. int length = fp.GetLength();
  182. if (length > 1024 * 1024)
  183. {
  184. fp.Close();
  185. ::DeleteFile(path); // 删除NullBalance.txt
  186. return;
  187. }
  188. fp.SeekToEnd();
  189. }
  190. else
  191. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  192. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
  193. fp.Close();
  194. }
  195. void WriteUnallowIP(CString str)
  196. {
  197. CStdioFile fp;
  198. CString path = g_mainpath + "\\UnallowIP.txt";
  199. if (::PathFileExists(path))
  200. {
  201. fp.Open(path, CFile::modeWrite);
  202. int length = fp.GetLength();
  203. if (length > 1024 * 1024)
  204. {
  205. fp.Close();
  206. ::DeleteFile(path); // 删除UnallowIP.txt
  207. return;
  208. }
  209. fp.SeekToEnd();
  210. }
  211. else
  212. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  213. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
  214. fp.Close();
  215. }
  216. int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
  217. {
  218. UINT num = 0; // number of image encoders
  219. UINT size = 0; // size of the image encoder array in bytes CheckFolderFileExist
  220. ImageCodecInfo* pImageCodecInfo = NULL;
  221. GetImageEncodersSize(&num, &size);
  222. if (size == 0)
  223. return -1; // Failure
  224. pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  225. if (pImageCodecInfo == NULL)
  226. return -1; // Failure
  227. GetImageEncoders(num, size, pImageCodecInfo);
  228. for (UINT j = 0; j < num; ++j)
  229. {
  230. if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
  231. {
  232. *pClsid = pImageCodecInfo[j].Clsid;
  233. free(pImageCodecInfo);
  234. return j; // Success
  235. }
  236. }
  237. free(pImageCodecInfo);
  238. return -1; // Failure
  239. }
  240. void SaveImageToFile(Image *img, CString path)
  241. {
  242. try
  243. {
  244. if (img == NULL)return;
  245. CLSID encoderClsid;
  246. BSTR bstr = path.AllocSysString();
  247. path.MakeLower();
  248. if (path.Right(3) == "bmp")
  249. {
  250. GetEncoderClsid(L"image/bmp", &encoderClsid);
  251. img->Save(bstr, &encoderClsid, NULL);
  252. }
  253. else if (path.Right(3) == "png")
  254. {
  255. GetEncoderClsid(L"image/png", &encoderClsid);
  256. img->Save(bstr, &encoderClsid, NULL);
  257. }
  258. else// if(path.Right (3)=="jpg")
  259. {
  260. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  261. EncoderParameters encoderParameters;
  262. ULONG quality;
  263. encoderParameters.Count = 1;
  264. encoderParameters.Parameter[0].Guid = EncoderQuality;
  265. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  266. encoderParameters.Parameter[0].NumberOfValues = 1;
  267. // Save the image as a JPEG with quality level 100.
  268. quality = 100;
  269. encoderParameters.Parameter[0].Value = &quality;
  270. img->Save(bstr, &encoderClsid, &encoderParameters);
  271. }
  272. SysFreeString(bstr);
  273. }
  274. catch (...)
  275. {
  276. }
  277. }
  278. void SaveImageToFile2(Image *img, CString path, ULONG quality)
  279. {
  280. try
  281. {
  282. if (img == NULL)return;
  283. CLSID encoderClsid;
  284. BSTR bstr = path.AllocSysString();
  285. path.MakeLower();
  286. if (path.Right(3) == "bmp")
  287. {
  288. GetEncoderClsid(L"image/bmp", &encoderClsid);
  289. img->Save(bstr, &encoderClsid, NULL);
  290. }
  291. else if (path.Right(3) == "png")
  292. {
  293. GetEncoderClsid(L"image/png", &encoderClsid);
  294. img->Save(bstr, &encoderClsid, NULL);
  295. }
  296. else// if(path.Right (3)=="jpg")
  297. {
  298. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  299. EncoderParameters encoderParameters;
  300. encoderParameters.Count = 1;
  301. encoderParameters.Parameter[0].Guid = EncoderQuality;
  302. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  303. encoderParameters.Parameter[0].NumberOfValues = 1;
  304. // Save the image as a JPEG with quality level 100.
  305. encoderParameters.Parameter[0].Value = &quality;
  306. img->Save(bstr, &encoderClsid, &encoderParameters);
  307. }
  308. SysFreeString(bstr);
  309. }
  310. catch (...)
  311. {
  312. }
  313. }
  314. void RectFitDes(int width, int height, CRect &rc)
  315. {
  316. float fscale = (float)width / (float)height;
  317. float rcscale = ((float)rc.Width()) / ((float)rc.Height());
  318. int rcwid = rc.Width();
  319. int rchei = rc.Height();
  320. int dt = 0;
  321. if (rcscale < fscale)
  322. {
  323. dt = (rchei - rcwid / fscale) / 2;
  324. rc.top += dt;
  325. rc.bottom -= dt;
  326. }
  327. else
  328. {
  329. dt = (rcwid - rchei*fscale) / 2;
  330. rc.left += dt;
  331. rc.right -= dt;
  332. }
  333. }
  334. ///目录是否存在的检查: 支持网络文件,判断文件时有漏洞
  335. bool CheckFolderFileExist(CString &strPath)
  336. {
  337. strPath.MakeLower();
  338. if (strPath.Find("c:\\") != -1 || strPath.Find("d:\\") != -1 || strPath.Find("e:\\") != -1 || strPath.Find("f:\\") != -1 || strPath.Find("g:\\") != -1 || strPath.Find("h:\\") != -1 || strPath.Find("i:\\") != -1 || strPath.Find("j:\\") != -1 || strPath.Find("k:\\") != -1 || strPath.Find("l:\\") != -1 || strPath.Find("m:\\") != -1)
  339. {
  340. return ::PathFileExists(strPath);
  341. }
  342. CString path = strPath;
  343. CFile fp;
  344. if (fp.Open(path, CFile::modeRead))
  345. {
  346. fp.Close();
  347. return 1;
  348. }
  349. if (strPath.Right(1) != "\\")
  350. path += "\\";
  351. path += "zaqw1234.dat";
  352. ::DeleteFile(path);// 删除zaqw1234.dat文件;
  353. if (fp.Open(path, CFile::modeCreate) == 0)
  354. return 0;
  355. fp.Close();
  356. ::DeleteFile(path); // 删除zaqw1234.dat文件;
  357. return 1;
  358. }
  359. bool CheckFileExist(CString &strPath)
  360. {
  361. strPath.MakeLower();
  362. if (strPath.Find("c:\\") != -1 || strPath.Find("d:\\") != -1 || strPath.Find("e:\\") != -1 || strPath.Find("f:\\") != -1 || strPath.Find("g:\\") != -1 || strPath.Find("h:\\") != -1 || strPath.Find("i:\\") != -1 || strPath.Find("j:\\") != -1 || strPath.Find("k:\\") != -1 || strPath.Find("l:\\") != -1 || strPath.Find("m:\\") != -1)
  363. {
  364. return ::PathFileExists(strPath);
  365. }
  366. CString path = strPath;
  367. CFile fp;
  368. if (fp.Open(path, CFile::modeRead))
  369. {
  370. fp.Close();
  371. return 1;
  372. }
  373. return 0;
  374. }
  375. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  376. {
  377. try
  378. {
  379. HCRYPTPROV hCryptProv;
  380. HCRYPTKEY hKey;
  381. HCRYPTHASH hHash;
  382. PBYTE pbBuffer;
  383. DWORD dwBlockLen;
  384. DWORD dwBufferLen;
  385. DWORD dwCount;
  386. //以下获得一个CSP句柄
  387. if (CryptAcquireContext(
  388. &hCryptProv,
  389. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  390. NULL,
  391. PROV_RSA_FULL,
  392. 0))
  393. {
  394. printf("A cryptographic provider has been acquired. \n");
  395. }
  396. else//密钥容器不存在
  397. {
  398. if (CryptAcquireContext(
  399. &hCryptProv,
  400. NULL,
  401. NULL,
  402. PROV_RSA_FULL,
  403. CRYPT_NEWKEYSET))//创建密钥容器
  404. {
  405. //创建密钥容器成功,并得到CSP句柄
  406. printf("A new key container has been created.\n");
  407. }
  408. else
  409. {
  410. return 0;
  411. }
  412. }
  413. //--------------------------------------------------------------------
  414. // 创建一个会话密钥(session key)
  415. // 会话密钥也叫对称密钥,用于对称加密算法。
  416. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  417. // CryptReleaseContext 期间的阶段。)
  418. //--------------------------------------------------------------------
  419. // Create a hash object.
  420. if (CryptCreateHash(
  421. hCryptProv,
  422. CALG_MD5,
  423. 0,
  424. 0,
  425. &hHash))
  426. {
  427. printf("A hash object has been created. \n");
  428. }
  429. else
  430. {
  431. return 0;
  432. }
  433. //--------------------------------------------------------------------
  434. // 用输入的密码产生一个散列
  435. if (CryptHashData(
  436. hHash,
  437. (BYTE *)szPassword,
  438. strlen(szPassword),
  439. 0))
  440. {
  441. printf("The password has been added to the hash. \n");
  442. }
  443. else
  444. {
  445. return 0;
  446. }
  447. //--------------------------------------------------------------------
  448. // 通过散列生成会话密钥(session key)
  449. if (CryptDeriveKey(
  450. hCryptProv,
  451. ENCRYPT_ALGORITHM,
  452. hHash,
  453. KEYLENGTH,
  454. &hKey))
  455. {
  456. printf("An encryption key is derived from the password hash. \n");
  457. }
  458. else
  459. {
  460. return 0;
  461. }
  462. //--------------------------------------------------------------------
  463. // Destroy the hash object.
  464. CryptDestroyHash(hHash);
  465. hHash = NULL;
  466. //--------------------------------------------------------------------
  467. // The session key is now ready.
  468. //--------------------------------------------------------------------
  469. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  470. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  471. // 数据长度。
  472. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  473. //--------------------------------------------------------------------
  474. // Determine the block size. If a block cipher is used,
  475. // it must have room for an extra block.
  476. if (ENCRYPT_BLOCK_SIZE > 1)
  477. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  478. else
  479. dwBufferLen = dwBlockLen;
  480. dwCount = dwBufferLen;
  481. //--------------------------------------------------------------------
  482. // In a do loop, encrypt the source file and write to the source file.
  483. int count;
  484. // if(leng%dwBlockLen==0)
  485. count = leng / dwBufferLen;
  486. // else
  487. // count=leng/dwBlockLen+1;
  488. int i = 0;
  489. for ( i = 0; i < count; i++)
  490. {
  491. pbBuffer = buffer + i*dwBufferLen;
  492. // 加密数据
  493. if (!CryptEncrypt(
  494. hKey, //密钥
  495. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  496. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  497. //这里通过判断是否到文件尾来决定是否为最后一块。
  498. 0, //保留
  499. pbBuffer, //输入被加密数据,输出加密后的数据
  500. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  501. dwBufferLen)) //pbBuffer的大小。
  502. {
  503. return 0;
  504. }
  505. }
  506. if (leng%dwBlockLen)
  507. {
  508. pbBuffer = buffer + i*dwBufferLen;
  509. dwCount = leng - i*dwBufferLen;
  510. if (!CryptEncrypt(
  511. hKey, //密钥
  512. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  513. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  514. //这里通过判断是否到文件尾来决定是否为最后一块。
  515. 0, //保留
  516. pbBuffer, //输入被加密数据,输出加密后的数据
  517. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  518. dwBufferLen)) //pbBuffer的大小。
  519. {
  520. return 0;
  521. }
  522. }
  523. //--------------------------------------------------------------------
  524. // Destroy session key.
  525. if (hKey)
  526. CryptDestroyKey(hKey);
  527. //--------------------------------------------------------------------
  528. // Destroy hash object.
  529. if (hHash)
  530. CryptDestroyHash(hHash);
  531. //--------------------------------------------------------------------
  532. // Release provider handle.
  533. if (hCryptProv)
  534. CryptReleaseContext(hCryptProv, 0);
  535. return(TRUE);
  536. }
  537. catch (...)
  538. {
  539. }
  540. }
  541. //-------------------------------------------------------------------------------
  542. // Remark by Jeff
  543. // 函数:BakData2
  544. // 描述:
  545. // 参数:
  546. // savedir : 存储备份数据库文件的目录;
  547. // bToday : 是否备份今天的数据库;
  548. // bBakHis : 是否备份历史数据库;
  549. //
  550. // 返回:无;
  551. //-------------------------------------------------------------------------------
  552. void BakData2(CString savedir, BOOL bToday, BOOL bBakHis)
  553. {
  554. // 1.使用master系统数据库来备份;
  555. CDatabase g_masterdb;
  556. try
  557. {
  558. #if 0
  559. CString strCon;
  560. strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=master;%s"), g_strdbServrename, g_strdbpsw);
  561. g_masterdb.OpenEx(strCon, CDatabase::noOdbcDialog);
  562. #else
  563. TCHAR szConnectString[MAX_PATH] = _T("");
  564. if (g_dwDBServerPort != 0)
  565. sprintf(szConnectString, "driver={SQL Server};Server=%s,%d;database=master;uid=%s;pwd=%s",
  566. g_szDBSource, g_dwDBServerPort, g_szDBAccount, g_szDBPassWord);
  567. else
  568. sprintf(szConnectString, "driver={SQL Server};Server=%s;database=master;uid=%s;pwd=%s",
  569. g_szDBSource, g_szDBAccount, g_szDBPassWord);
  570. g_masterdb.OpenEx(szConnectString, CDatabase::noOdbcDialog);
  571. #endif
  572. }
  573. catch (CDBException *e)
  574. {
  575. e->Delete();
  576. }
  577. try
  578. {
  579. // 2.获取昨天的日期tm2;
  580. CTime tm = CTime::GetCurrentTime();
  581. CTimeSpan dt(1, 0, 0, 0);
  582. CTime tm2;
  583. if (bToday)
  584. tm2 = tm;
  585. else
  586. tm2 = tm - dt;
  587. CString savepath;
  588. savepath.Format("%s\\%s(sql).bak", savedir, tm2.Format("%Y%m%d"));
  589. BOOL bBak = 0; // 是否备份成功;
  590. BOOL bBak2 = 0; // 是否备份历史年限数据库成功;
  591. // 3.如果没找到<昨天>备份数据库,则进行备份;
  592. if (::PathFileExists(savepath) == 0)
  593. {
  594. g_masterdb.SetQueryTimeout(600);
  595. {
  596. CString sql;
  597. #if 0 // Jeff.deal;
  598. sql.Format("BACKUP DATABASE db TO DISK='%s' WITH FORMAT", savepath);
  599. #else
  600. sql.Format("BACKUP DATABASE %s TO DISK='%s' WITH FORMAT", g_szDBName, savepath);
  601. #endif
  602. try
  603. {
  604. g_masterdb.ExecuteSQL(sql);
  605. bBak = 1;
  606. g_bakinfo = "资料备份成功";
  607. }
  608. catch (CDBException * e)
  609. {
  610. WriteLogin(e->m_strError);
  611. WriteLogin("备份失败!");
  612. e->Delete();
  613. }
  614. }
  615. g_masterdb.SetQueryTimeout(100);
  616. }
  617. // 4.Jeff.是否备份历史数据库;
  618. if (bBakHis)
  619. {
  620. for (int i = CTime::GetCurrentTime().GetYear() - 1; i >= 2009; i--)
  621. {
  622. g_masterdb.SetQueryTimeout(600);
  623. {
  624. CString savepath2;
  625. savepath2.Format("%s\\%s-%d(sql).bak", savedir, tm2.Format("%Y%m%d"), i);
  626. CString sql;
  627. sql.Format("BACKUP DATABASE [%d] TO DISK='%s' WITH FORMAT", i, savepath2);
  628. try
  629. {
  630. g_masterdb.ExecuteSQL(sql);
  631. bBak2 = 1;
  632. }
  633. catch (CDBException * e)
  634. {
  635. e->Delete();
  636. break;
  637. }
  638. }
  639. g_masterdb.SetQueryTimeout(100);
  640. }
  641. }
  642. // 5.昨天的数据库备份是否备份成功;
  643. if (bBak)
  644. {
  645. CTimeSpan dt2(31, 0, 0, 0);
  646. CTimeSpan dt3(365, 0, 0, 0);
  647. // tm:今天, tm3:1个月前, tm4:1年前;
  648. CTime tm3 = tm - dt2;
  649. CTime tm4 = tm - dt3;
  650. // 6.循环删除数据库备份;
  651. while (tm3 > tm4)
  652. {
  653. savepath.Format("%s\\%s.bak", savedir, tm3.Format("%Y%m%d"));
  654. #ifdef SQLSERVER_VERSION
  655. savepath.Format("%s\\%s(sql).bak", savedir, tm3.Format("%Y%m%d"));
  656. #endif
  657. // Jeff.删除一个月前的数据库备份;
  658. if (!CheckFileExist(savepath))
  659. break;
  660. ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
  661. tm3 -= dt; // dt 1天的间隔;
  662. }
  663. }
  664. if (bBak2)
  665. {
  666. CTimeSpan dt2(15, 0, 0, 0);
  667. CTimeSpan dt3(365, 0, 0, 0);
  668. for (int i = tm.GetYear() - 1; i >= 2009; i--)
  669. {
  670. CTime tm3 = tm - dt2;
  671. CTime tm4 = tm - dt3;
  672. while (tm3 > tm4)
  673. {
  674. savepath.Format("%s\\%s-%d.bak", savedir, tm3.Format("%Y%m%d"), i);
  675. #ifdef SQLSERVER_VERSION
  676. savepath.Format("%s\\%s-%d(sql).bak", savedir, tm3.Format("%Y%m%d"), i);
  677. #endif
  678. if (!CheckFileExist(savepath))
  679. break;
  680. ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
  681. tm3 -= dt;
  682. }
  683. }
  684. }
  685. }
  686. catch (...)
  687. {
  688. WriteLogin("出错在函数BakData2中");
  689. }
  690. g_masterdb.Close();
  691. }
  692. void BakData(CString savedir, BOOL bToday, BOOL bBakHis)// back data
  693. {
  694. #ifdef SQLSERVER_VERSION
  695. BakData2(savedir, bToday, bBakHis);
  696. return;
  697. #endif
  698. try
  699. {
  700. CTime tm = CTime::GetCurrentTime();
  701. CTimeSpan dt(1, 0, 0, 0);
  702. CTime tm2;
  703. if (bToday)
  704. tm2 = tm;
  705. else
  706. tm2 = tm - dt;
  707. CString savepath;
  708. savepath.Format("%s\\%s.bak", savedir, tm2.Format("%Y%m%d"));
  709. #ifdef SQLSERVER_VERSION
  710. savepath.Format("%s\\%s(sql).bak", savedir, tm2.Format("%Y%m%d"));
  711. #endif
  712. BOOL bBak = 0;
  713. BOOL bBak2 = 0;
  714. {
  715. TCHAR szDBFile[MAX_PATH] = _T(""); // Jeff.add
  716. #ifdef SQLSERVER_VERSION
  717. sprintf(szDBFile, "%s\\数据\\%s.mdf", g_ModulePath, g_szDBName);
  718. //if(::CopyFile (g_mainpath+"\\数据\\db.mdf", savepath, 0))
  719. if (::CopyFile(szDBFile, savepath, 0))
  720. #else
  721. sprintf(szDBFile, "%s\\数据\\%s.mdb", g_ModulePath, g_szDBName);
  722. //if(::CopyFile (g_mainpath+"\\数据\\db.mdb", savepath, 0))
  723. if (::CopyFile(szDBFile, savepath, 0))
  724. #endif
  725. {
  726. bBak = 1;
  727. g_bakinfo = "资料备份成功";
  728. }
  729. }
  730. for (int i = tm.GetYear() - 1; i >= 1900; i--)
  731. {
  732. #ifdef SQLSERVER_VERSION
  733. savepath.Format("%s\\数据\\%d.mdf", g_mainpath, i);
  734. #else
  735. savepath.Format("%s\\数据\\%d.mdb", g_mainpath, i);
  736. #endif
  737. if (CheckFileExist(savepath) == 0)break;
  738. CString savepath2;
  739. savepath2.Format("%s\\%s-%d.bak", savedir, tm2.Format("%Y%m%d"), i);
  740. #ifdef SQLSERVER_VERSION
  741. savepath2.Format("%s\\%s-%d(sql).bak", savedir, tm2.Format("%Y%m%d"), i);
  742. #endif
  743. if (::CopyFile(savepath, savepath2, 0))
  744. bBak2 = 1;
  745. }
  746. if (bBak)
  747. {
  748. CTimeSpan dt2(15, 0, 0, 0);
  749. CTimeSpan dt3(365, 0, 0, 0);
  750. CTime tm3 = tm - dt2;
  751. CTime tm4 = tm - dt3;
  752. while (tm3 > tm4)
  753. {
  754. savepath.Format("%s\\%s.bak", savedir, tm3.Format("%Y%m%d"));
  755. #ifdef SQLSERVER_VERSION
  756. savepath.Format("%s\\%s(sql).bak", savedir, tm3.Format("%Y%m%d"));
  757. #endif
  758. if (!CheckFileExist(savepath))
  759. break;
  760. ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
  761. tm3 -= dt;
  762. }
  763. }
  764. if (bBak2)
  765. {
  766. CTimeSpan dt2(7, 0, 0, 0);
  767. CTimeSpan dt3(365, 0, 0, 0);
  768. for (int i = tm.GetYear() - 1; i >= 1900; i--)
  769. {
  770. #ifdef SQLSERVER_VERSION
  771. savepath.Format("%s\\数据\\%d.mdf", g_mainpath, i);
  772. #else
  773. savepath.Format("%s\\数据\\%d.mdb", g_mainpath, i);
  774. #endif
  775. if (CheckFileExist(savepath) == 0)break;
  776. CTime tm3 = tm - dt2;
  777. CTime tm4 = tm - dt3;
  778. while (tm3 > tm4)
  779. {
  780. savepath.Format("%s\\%s-%d.bak", savedir, tm3.Format("%Y%m%d"), i);
  781. #ifdef SQLSERVER_VERSION
  782. savepath.Format("%s\\%s-%d(sql).bak", savedir, tm3.Format("%Y%m%d"), i);
  783. #endif
  784. if (!CheckFileExist(savepath))
  785. break;
  786. ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
  787. tm3 -= dt;
  788. }
  789. }
  790. }
  791. }
  792. catch (...)
  793. {
  794. WriteLogin("出错在函数BakData中");
  795. }
  796. }
  797. DWORD FindAppProcessID(CString path)
  798. {
  799. try
  800. {
  801. HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  802. PROCESSENTRY32 Info;
  803. Info.dwSize = sizeof(PROCESSENTRY32);
  804. path.MakeLower();
  805. if (::Process32First(handle, &Info))
  806. {
  807. do
  808. {
  809. CString ss = Info.szExeFile;
  810. ss.MakeLower();
  811. if (ss == path)
  812. {
  813. ::CloseHandle(handle);
  814. return Info.th32ProcessID;
  815. }
  816. } while (::Process32Next(handle, &Info));
  817. ::CloseHandle(handle);
  818. }
  819. return -1;
  820. }
  821. catch (...)
  822. {
  823. }
  824. }
  825. void WriteLogin(CString str)
  826. {
  827. MyLock lock("CDBServerDlgWriteLogin");
  828. try
  829. {
  830. CStdioFile fp;
  831. CString path = g_mainpath + "\\日志.txt";
  832. if (CheckFileExist(path))
  833. {
  834. fp.Open(path, CFile::modeWrite);
  835. int length = fp.GetLength();
  836. if (length > 5 * 1024 * 1024)
  837. {
  838. fp.Close();
  839. ::DeleteFile(path); // 删除备份的 日志.txt;
  840. return;
  841. }
  842. fp.SeekToEnd();
  843. }
  844. else
  845. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  846. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S") + " " + str + "\n");
  847. fp.Close();
  848. }
  849. catch (...)
  850. {
  851. }
  852. }
  853. void GetPathFromNetShareName(CString name, CString &sRet)
  854. {
  855. if (sRet.Find(":") != -1)return;
  856. NET_API_STATUS res;
  857. SHARE_INFO_502 * psi = NULL;
  858. WCHAR wszShareName[255];
  859. TCHAR szPathName[255];
  860. TCHAR path[255];
  861. memset(path, 0, 255);
  862. //转换成宽字符
  863. MultiByteToWideChar(CP_ACP, 0, name, -1, wszShareName, 255);
  864. #if 0
  865. res = NetShareGetInfo(NULL, (char *)wszShareName, 502, (LPBYTE *)&psi);
  866. #else
  867. res = NetShareGetInfo(NULL, wszShareName, 502, (LPBYTE *)&psi);
  868. #endif
  869. if (res == NERR_Success)
  870. {
  871. wsprintf((char*)szPathName, "%S ", psi->shi502_path);
  872. sRet = szPathName;
  873. sRet.TrimRight();
  874. }
  875. else
  876. {
  877. CString str;
  878. #if 0 // Jeff delete;
  879. str.Format("转换路径失败-%d", res);
  880. ::WriteTextLog(str);//Jeff delete,此处使用了系统域符号,其实完全没作用.
  881. #else
  882. str.Format("转换路径失败(\"%s\"),", sRet);
  883. ErrorExit(str, res);
  884. #endif
  885. }
  886. //释放缓冲
  887. NetApiBufferFree(psi);
  888. }
  889. CString GetModifyTime2(CString path)
  890. {
  891. try
  892. {
  893. CFileFind finder;
  894. BOOL bWorking = finder.FindFile(path);
  895. if (bWorking)
  896. {
  897. finder.FindNextFile();
  898. CTime tm;
  899. finder.GetLastWriteTime(tm);
  900. CString stime;
  901. stime.Format("%d%02d%02d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
  902. return stime;
  903. }
  904. return "";
  905. }
  906. #if JEFF_TEST_ON
  907. catch (CFileException *e)
  908. {
  909. CString strError;
  910. strError.Format("2.读文件[%s]修改时间出错:%d", e->m_strFileName, e->m_lOsError);
  911. WriteLogin(strError);
  912. e->Delete();
  913. }
  914. #else
  915. catch (...)
  916. {
  917. WriteLogin("出错");
  918. }
  919. #endif
  920. }
  921. CString GetModifyTime(CString path)
  922. {
  923. try
  924. {
  925. HANDLE hFile;
  926. WIN32_FIND_DATA wfd;
  927. // SYSTEMTIME systime;
  928. FILETIME localtime;
  929. CString stime; //输出时间
  930. memset(&wfd, 0, sizeof(wfd));
  931. if ((hFile = FindFirstFile(path, &wfd)) == INVALID_HANDLE_VALUE)
  932. return "";
  933. //ok,转换时间
  934. FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localtime);
  935. stime.Format("%16d%16d", localtime.dwHighDateTime, localtime.dwLowDateTime);
  936. return stime;
  937. }
  938. #if JEFF_TEST_ON
  939. catch (CFileException *e)
  940. {
  941. CString strError;
  942. strError.Format("读文件[%s]修改时间出错:%d", e->m_strFileName, e->m_lOsError);
  943. WriteLogin(strError);
  944. e->Delete();
  945. }
  946. #else catch (...)
  947. {
  948. WriteLogin("出错");
  949. }
  950. #endif
  951. }
  952. //--------------------------------------------------------------------------------
  953. // Jeff add 2014.06.23;
  954. // 函数:ErrorExit
  955. // 描述:
  956. // 参数:
  957. // lpszFunction:函数名;
  958. // dwError:错误码;
  959. //
  960. //--------------------------------------------------------------------------------
  961. void ErrorExit(CString strDescription, const DWORD &dwError)
  962. {
  963. #if 1
  964. LPVOID lpMsgBuf;
  965. BOOL fOk = FormatMessage(
  966. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  967. FORMAT_MESSAGE_FROM_SYSTEM |
  968. FORMAT_MESSAGE_IGNORE_INSERTS,
  969. NULL,
  970. dwError,
  971. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  972. (LPTSTR)&lpMsgBuf,
  973. 0, NULL);
  974. if (!fOk)
  975. {
  976. // Is it a network-related error?
  977. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  978. if (hDll != NULL)
  979. {
  980. FormatMessage(
  981. FORMAT_MESSAGE_FROM_HMODULE |
  982. FORMAT_MESSAGE_FROM_SYSTEM |
  983. FORMAT_MESSAGE_IGNORE_INSERTS,
  984. hDll,
  985. dwError,
  986. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  987. (LPTSTR)&lpMsgBuf,
  988. 0,
  989. NULL);
  990. FreeLibrary(hDll);
  991. }
  992. }
  993. if (lpMsgBuf != NULL)
  994. {
  995. CString strDisplay;
  996. strDisplay.Format("%s.错误码=%d,Windows描述:%s", strDescription, dwError, (PCTSTR)LocalLock(lpMsgBuf));
  997. WriteLogin(strDisplay);
  998. LocalFree(lpMsgBuf);
  999. }
  1000. else
  1001. {
  1002. WriteLogin(strDescription);
  1003. }
  1004. #else
  1005. HLOCAL hlocal = NULL; // Buffer that gets the error message string
  1006. // Get the error code's textual description
  1007. BOOL fOk = FormatMessage(
  1008. FORMAT_MESSAGE_FROM_SYSTEM |
  1009. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1010. FORMAT_MESSAGE_IGNORE_INSERTS,
  1011. NULL,
  1012. dwError,
  1013. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1014. (PTSTR)&hlocal,
  1015. 0,
  1016. NULL);
  1017. if (!fOk)
  1018. {
  1019. // Is it a network-related error?
  1020. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1021. if (hDll != NULL)
  1022. {
  1023. FormatMessage(
  1024. FORMAT_MESSAGE_FROM_HMODULE |
  1025. FORMAT_MESSAGE_FROM_SYSTEM |
  1026. FORMAT_MESSAGE_IGNORE_INSERTS,
  1027. hDll,
  1028. dwError,
  1029. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1030. (PTSTR)&hlocal,
  1031. 0,
  1032. NULL);
  1033. FreeLibrary(hDll);
  1034. }
  1035. }
  1036. if (hlocal != NULL)
  1037. {
  1038. CString strDisplay;
  1039. strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
  1040. WriteLogin(strDisplay);
  1041. LocalFree(hlocal);
  1042. }
  1043. else
  1044. {
  1045. WriteLogin("Error number not found.");
  1046. }
  1047. #endif
  1048. }
  1049. void MyGetIPByName(CString &name)
  1050. {
  1051. CString strIP = name;
  1052. name.Empty();
  1053. DWORD dwServerIP = 0;
  1054. HOSTENT *host = gethostbyname(strIP);
  1055. struct in_addr addr;
  1056. if (host != NULL)
  1057. {
  1058. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1059. {
  1060. memset(&addr, 0, sizeof(addr));
  1061. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1062. dwServerIP = ntohl(addr.S_un.S_addr);
  1063. }
  1064. BYTE* pIP = (BYTE*)&dwServerIP;
  1065. name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1066. }
  1067. }
  1068. void EncryptFile3(CString path)
  1069. {
  1070. CFile fp;
  1071. if (fp.Open(path, CFile::modeRead) == 0)return;
  1072. DWORD leng = fp.GetLength();
  1073. BYTE *pData = new BYTE[leng];
  1074. fp.Read(pData, leng);
  1075. fp.Close();
  1076. EncryptFile2(pData, leng, "z9871234abcd");
  1077. fp.Open(path, CFile::modeWrite);
  1078. fp.Write(pData, leng);
  1079. fp.Close();
  1080. delete[]pData;
  1081. }
  1082. int FindArray(CStringArray *pArray, CString Str)
  1083. {
  1084. for (int i = 0; i < pArray->GetSize(); i++)
  1085. {
  1086. if (pArray->ElementAt(i) == Str)
  1087. return i;
  1088. }
  1089. return -1;
  1090. }
  1091. BOOL isInnerIP(DWORD a_ip)
  1092. {
  1093. BOOL bValid = 0;
  1094. if ((a_ip >> 24 == 0xa) || (a_ip >> 16 == 0xc0a8) || (a_ip >> 22 == 0x2b0))
  1095. {
  1096. bValid = 1;
  1097. }
  1098. return bValid;
  1099. }
  1100. BOOL CompareIP(CString name1, CString name2)
  1101. {
  1102. CStringArray g_serverarray1;
  1103. CStringArray g_serverarray2;
  1104. CString strIP = name1;
  1105. DWORD dwServerIP = 0;
  1106. HOSTENT *host = gethostbyname(strIP);
  1107. struct in_addr addr;
  1108. if (host != NULL)
  1109. {
  1110. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1111. {
  1112. memset(&addr, 0, sizeof(addr));
  1113. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1114. dwServerIP = ntohl(addr.S_un.S_addr);
  1115. BYTE* pIP = (BYTE*)&dwServerIP;
  1116. strIP.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1117. g_serverarray1.Add(strIP);
  1118. }
  1119. }
  1120. while (g_serverarray1.GetSize() > 1)
  1121. {
  1122. CString name;
  1123. BOOL bFind = 0;
  1124. for (int i = 0; i < g_serverarray1.GetSize(); i++)
  1125. {
  1126. DWORD dwServerIP = inet_addr(g_serverarray1.ElementAt(i));
  1127. dwServerIP = htonl(dwServerIP);//加了才正确
  1128. if (isInnerIP(dwServerIP) == 0)
  1129. {
  1130. g_serverarray1.RemoveAt(i);
  1131. bFind = 1;
  1132. break;
  1133. }
  1134. }
  1135. if (bFind == 0)
  1136. {
  1137. break;
  1138. }
  1139. }
  1140. //////////
  1141. {
  1142. CString strIP = name2;
  1143. DWORD dwServerIP = 0;
  1144. HOSTENT *host = gethostbyname(strIP);
  1145. struct in_addr addr;
  1146. if (host != NULL)
  1147. {
  1148. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1149. {
  1150. memset(&addr, 0, sizeof(addr));
  1151. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1152. dwServerIP = ntohl(addr.S_un.S_addr);
  1153. BYTE* pIP = (BYTE*)&dwServerIP;
  1154. strIP.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1155. g_serverarray2.Add(strIP);
  1156. }
  1157. }
  1158. while (g_serverarray2.GetSize() > 1)
  1159. {
  1160. CString name;
  1161. BOOL bFind = 0;
  1162. for (int i = 0; i < g_serverarray2.GetSize(); i++)
  1163. {
  1164. DWORD dwServerIP = inet_addr(g_serverarray2.ElementAt(i));
  1165. dwServerIP = htonl(dwServerIP);//加了才正确
  1166. if (isInnerIP(dwServerIP) == 0)
  1167. {
  1168. g_serverarray2.RemoveAt(i);
  1169. bFind = 1;
  1170. break;
  1171. }
  1172. }
  1173. if (bFind == 0)
  1174. {
  1175. break;
  1176. }
  1177. }
  1178. }
  1179. if (g_serverarray1.GetSize() == 0 || g_serverarray2.GetSize() == 0)return 1;
  1180. for (int i = 0; i < g_serverarray1.GetSize(); i++)
  1181. {
  1182. for (int j = 0; j < g_serverarray2.GetSize(); j++)
  1183. {
  1184. if (g_serverarray1.ElementAt(i) == g_serverarray2.ElementAt(j))return 0;
  1185. }
  1186. }
  1187. return 1;
  1188. }
  1189. void DeleteDirectory(CString strDir)
  1190. {
  1191. try
  1192. {
  1193. // 首先删除文件及子文件夹
  1194. CFileFind ff;
  1195. BOOL bFound = ff.FindFile(strDir + "\\*", 0);
  1196. while (bFound)
  1197. {
  1198. bFound = ff.FindNextFile();
  1199. if (ff.GetFileName() == "." || ff.GetFileName() == "..")
  1200. continue;
  1201. // 去掉文件(夹)只读等属性
  1202. SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
  1203. if (ff.IsDirectory())
  1204. { // 递归删除子文件夹
  1205. DeleteDirectory(ff.GetFilePath());
  1206. RemoveDirectory(ff.GetFilePath());
  1207. }
  1208. else
  1209. {
  1210. DeleteFile(ff.GetFilePath()); // Jeff:删除某文件夹下的所有子文件;
  1211. }
  1212. }
  1213. ff.Close();
  1214. // 然后删除该文件夹
  1215. RemoveDirectory(strDir);
  1216. }
  1217. catch (...)
  1218. {
  1219. WriteLogin("出错在函数deldir中");
  1220. }
  1221. // int SHFileOperation( LPSHFILEOPSTRUCT lpFileOp );
  1222. }
  1223. int GetLengthEx(CString str)
  1224. {
  1225. wchar_t wstr[500];
  1226. int k = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, strlen(str), wstr, 500);
  1227. return k;
  1228. }
  1229. CString GetFileSize(CString path)
  1230. {
  1231. CString str = "0";
  1232. try
  1233. {
  1234. CFile fp;
  1235. fp.Open(path, CFile::modeRead);
  1236. str.Format("%d", fp.GetLength());
  1237. fp.Close();
  1238. }
  1239. catch (...)
  1240. {
  1241. }
  1242. return str;
  1243. }
  1244. void SortArray(CStringArray &datearray)
  1245. {
  1246. try
  1247. {
  1248. CString strtemp;
  1249. float ftemp;
  1250. int last = datearray.GetSize() - 1;
  1251. bool sorted = true;
  1252. do {
  1253. sorted = true;
  1254. for (int i = 0; i < last; i++)
  1255. {
  1256. if (datearray[i] < datearray[i + 1])
  1257. {
  1258. strtemp = datearray[i];
  1259. datearray[i] = datearray[i + 1];
  1260. datearray[i + 1] = strtemp;
  1261. sorted = false;
  1262. }
  1263. }
  1264. last--;
  1265. } while (!sorted);
  1266. }
  1267. catch (...)
  1268. {
  1269. WriteLogin("SortArray出错");
  1270. }
  1271. }
  1272. int CheckPhoneType(CString phoneno)//移动:0 联通:1 电信:2 小灵通:3 未知:-1
  1273. {
  1274. int i = 0;
  1275. if (phoneno.IsEmpty())return -1;
  1276. for ( i = 0; i < phoneno.GetLength(); i++)
  1277. {
  1278. if (phoneno.GetAt(i) < '0' || phoneno.GetAt(i) > '9')return -1;
  1279. }
  1280. if (phoneno.GetAt(0) == '1')
  1281. {
  1282. if (phoneno.GetLength() != 11)
  1283. return -1;
  1284. int mobile[] = { 139, 138, 137, 136, 135, 134, 159, 158, 152, 151, 150, 157, 188, 187, 144, 182, 147, 183, 184 };
  1285. int unicom[] = { 130, 131, 132, 155, 156, 186, 185 };
  1286. int telecom[] = { 133, 153, 189, 180, 181 };
  1287. for (i = 0; i < sizeof(mobile) / sizeof(int); i++)
  1288. {
  1289. if (mobile[i] == atoi(phoneno.Left(3)))
  1290. {
  1291. return 0;
  1292. }
  1293. }
  1294. for (i = 0; i < sizeof(unicom) / sizeof(int); i++)
  1295. {
  1296. if (unicom[i] == atoi(phoneno.Left(3)))
  1297. {
  1298. return 1;
  1299. }
  1300. }
  1301. for (i = 0; i < sizeof(telecom) / sizeof(int); i++)
  1302. {
  1303. if (telecom[i] == atoi(phoneno.Left(3)))
  1304. {
  1305. return 2;
  1306. }
  1307. }
  1308. return -1;
  1309. }
  1310. /* else if(phoneno.GetAt (0)=='0')
  1311. {
  1312. if(phoneno.GetLength ()>=10 && phoneno.GetLength ()<=12)
  1313. {
  1314. return 3;
  1315. }
  1316. }*/
  1317. return -1;
  1318. }
  1319. CTime GetTmFromStr(CString date)
  1320. {
  1321. try
  1322. {
  1323. CTime tm(atoi(date.Mid(0, 4)), atoi(date.Mid(5, 2)), atoi(date.Mid(8, 2)), 0, 0, 0);
  1324. CString ss;
  1325. ss.Format("%d-%d-%d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
  1326. return tm;
  1327. }
  1328. catch (...)
  1329. {
  1330. date = "1980-01-01";
  1331. CTime tm(atoi(date.Mid(0, 4)), atoi(date.Mid(5, 2)), atoi(date.Mid(8, 2)), 0, 0, 0);
  1332. return tm;
  1333. }
  1334. }
  1335. void ConvertToPrice(CString &str)
  1336. {
  1337. if (str.Find('.') == -1)return;
  1338. str.TrimRight('0');
  1339. str.TrimRight('.');
  1340. }
  1341. void SortArray(CArray<CStringArray, CStringArray>*List1array, int pos)
  1342. {
  1343. CString stemp;
  1344. int last = List1array->GetSize() - 1;
  1345. int ret = last;
  1346. bool sorted = true;
  1347. do
  1348. {
  1349. sorted = true;
  1350. for (int i = 0; i < last; i++)
  1351. {
  1352. if (List1array->ElementAt(i).ElementAt(pos) < List1array->ElementAt(i + 1).ElementAt(pos))
  1353. {
  1354. for (int a = 0; a < List1array->ElementAt(i).GetSize(); a++)
  1355. {
  1356. stemp = List1array->ElementAt(i).ElementAt(a);
  1357. List1array->ElementAt(i).SetAt(a, List1array->ElementAt(i + 1).ElementAt(a));
  1358. List1array->ElementAt(i + 1).SetAt(a, stemp);
  1359. }
  1360. sorted = false;
  1361. }
  1362. }
  1363. last--;
  1364. } while (!sorted);
  1365. }
  1366. void DeCodePicCount(int &value1, int &value2)
  1367. {
  1368. // SoftKey ytSoftKey;
  1369. int ret = 0;//ytSoftKey.Ini();
  1370. if (ret != 0)
  1371. {
  1372. return;
  1373. }
  1374. int d[8]; double f[8];
  1375. TCHAR s0[50] = _T(""), s1[50] = _T(""), s2[50] = _T(""), s3[50] = _T(""), s4[50] = _T(""), s5[50] = _T(""), s6[50] = _T(""), s7[50] = _T("");
  1376. //运行自定义函数
  1377. ret = ytSoftKey.decryptpiccount(&value1, &value2, &d[2], &d[3], &d[4], &d[5], &d[6], &d[7],
  1378. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1379. s0, s1, s2, s3, s4, s5, s6, s7);
  1380. if (ret != 0 && ret != -43)
  1381. {
  1382. exit(0);
  1383. }
  1384. }
  1385. void DeCodelong1(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1386. {
  1387. // SoftKey ytSoftKey;
  1388. int ret = 0;//ytSoftKey.Ini();
  1389. if (ret != 0)
  1390. {
  1391. return;
  1392. }
  1393. double f[8];
  1394. TCHAR s0[50] = _T(""), s1[50] = _T(""), s2[50] = _T(""), s3[50] = _T(""), s4[50] = _T(""), s5[50] = _T(""), s6[50] = _T(""), s7[50] = _T("");
  1395. //运行自定义函数
  1396. ret = ytSoftKey.decryptlong1(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1397. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1398. s0, s1, s2, s3, s4, s5, s6, s7);
  1399. if (ret != 0 && ret != -43)
  1400. {
  1401. exit(0);
  1402. }
  1403. }
  1404. void DeCodelong2(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1405. {
  1406. // SoftKey ytSoftKey;
  1407. int ret = 0;//ytSoftKey.Ini();
  1408. if (ret != 0)
  1409. {
  1410. return;
  1411. }
  1412. double f[8];
  1413. TCHAR s0[50] = _T(""), s1[50] = _T(""), s2[50] = _T(""), s3[50] = _T(""), s4[50] = _T(""), s5[50] = _T(""), s6[50] = _T(""), s7[50] = _T("");
  1414. //运行自定义函数
  1415. ret = ytSoftKey.decryptlong2(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1416. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1417. s0, s1, s2, s3, s4, s5, s6, s7);
  1418. if (ret != 0 && ret != -43)
  1419. {
  1420. exit(0);
  1421. }
  1422. }
  1423. void DeCodelong3(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1424. {
  1425. // SoftKey ytSoftKey;
  1426. int ret = 0;//ytSoftKey.Ini();
  1427. if (ret != 0)
  1428. {
  1429. return;
  1430. }
  1431. double f[8];
  1432. TCHAR s0[50] = _T(""), s1[50] = _T(""), s2[50] = _T(""), s3[50] = _T(""), s4[50] = _T(""), s5[50] = _T(""), s6[50] = _T(""), s7[50] = _T("");
  1433. //运行自定义函数
  1434. ret = ytSoftKey.decryptlong3(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1435. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1436. s0, s1, s2, s3, s4, s5, s6, s7);
  1437. if (ret != 0 && ret != -43)
  1438. {
  1439. exit(0);
  1440. }
  1441. }
  1442. void DeCodelong4(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1443. {
  1444. // SoftKey ytSoftKey;
  1445. int ret = 0;//ytSoftKey.Ini();
  1446. if (ret != 0)
  1447. {
  1448. exit(0);
  1449. }
  1450. double f[8];
  1451. TCHAR s0[50] = _T(""), s1[50] = _T(""), s2[50] = _T(""), s3[50] = _T(""), s4[50] = _T(""), s5[50] = _T(""), s6[50] = _T(""), s7[50] = _T("");
  1452. //运行自定义函数
  1453. ret = ytSoftKey.decryptlong4(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1454. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1455. s0, s1, s2, s3, s4, s5, s6, s7);
  1456. if (ret != 0 && ret != -43)
  1457. {
  1458. exit(0);
  1459. }
  1460. }
  1461. #endif
  1462. /************************************************************************/
  1463. /* 函数:WriteTextLog[7/28/2016 IT];
  1464. /* 描述:写文本日志;
  1465. /* 参数:;
  1466. /* [IN] :;
  1467. /* 返回:void;
  1468. /* 注意:;
  1469. /* 示例:;
  1470. /*
  1471. /* 修改:;
  1472. /* 日期:;
  1473. /* 内容:;
  1474. /************************************************************************/
  1475. void WriteTextLog(const TCHAR *format, ...)
  1476. {
  1477. try
  1478. {
  1479. static ThreadSection _critSection;
  1480. AutoThreadSection aSection(&_critSection);
  1481. // 解析出日志路径;
  1482. TCHAR szlogpath[MAX_PATH] = {0};
  1483. static TCHAR szModulePath[MAX_PATH] = {0};
  1484. static TCHAR szFna[_MAX_DIR] = { 0 };
  1485. if ( szModulePath[0] == _T('\0') )
  1486. {
  1487. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  1488. TCHAR szDir[_MAX_DIR] = { 0 };
  1489. TCHAR szExt[_MAX_DIR] = { 0 };
  1490. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  1491. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  1492. _tcscpy_s(szModulePath, szDrive);
  1493. _tcscat_s(szModulePath, szDir);
  1494. }
  1495. _stprintf_s(szlogpath, _T("%s日志\\%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]").GetString());
  1496. // 打开或创建文件;
  1497. CStdioFile fp;
  1498. if (PathFileExists(szlogpath))
  1499. {
  1500. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  1501. {
  1502. return;
  1503. }
  1504. fp.SeekToEnd();
  1505. }
  1506. else
  1507. {
  1508. if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
  1509. return;
  1510. }
  1511. // 格式化前设置语言区域;
  1512. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  1513. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  1514. // 格式化日志内容;
  1515. va_list args = NULL;
  1516. int len = 0;
  1517. TCHAR *buffer = NULL;
  1518. va_start( args, format );
  1519. // _vscprintf doesn't count. terminating '\0'
  1520. len = _vsctprintf_p( format, args );
  1521. if ( len == -1 )
  1522. {
  1523. goto clear;
  1524. }
  1525. len++;
  1526. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  1527. _vstprintf_s( buffer, len, format, args ); // C4996
  1528. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  1529. // 将日志内容输入到文件中;
  1530. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  1531. fp.WriteString(buffer);
  1532. fp.WriteString(_T("\n"));
  1533. // 关闭文件,释放资源并设置回原语言区域;
  1534. free( buffer );
  1535. clear:
  1536. _tsetlocale(LC_CTYPE, old_locale);
  1537. free(old_locale);//还原区域设定;
  1538. fp.Close();
  1539. }
  1540. catch (CException *e)
  1541. {
  1542. e->ReportError();
  1543. e->Delete();
  1544. }
  1545. }