Global.cpp 36 KB

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