Global.cpp 44 KB

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