Global.cpp 41 KB

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