Global.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  1. #include "StdAfx.h"
  2. #include "Global.h"
  3. #include "./helper/ffsco.h"
  4. // ----stat头文件;
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. // ----stat头文件;
  8. #define BAKDB_FORMAT _T("BACKUP DATABASE [%s] TO DISK='%s' WITH FORMAT")
  9. TCHAR g_szHostName[MAX_PATH] = {0}; // 本机名称;
  10. TCHAR g_ModulePath[MAX_PATH] = _T(""); // 软件目录;
  11. TCHAR g_ModuleFileName[MAX_PATH] = _T(""); // 软件名称;
  12. TCHAR g_szConnectString[MAX_PATH] = _T(""); // DB连接串;
  13. // 服务器信息;
  14. TCHAR g_szDBSource[MAX_PATH] = _T(""); // 数据库源(服务所在IP或计算机名);
  15. TCHAR g_szDBSourcePort[MAX_PATH] = _T(""); // 数据库源端口;
  16. DWORD g_dwDBServerPort = 0; // 数据库源端口;
  17. TCHAR g_szDBAccount[MAX_PATH] = _T(""); // 数据库登录用户;
  18. TCHAR g_szDBPassWord[MAX_PATH] = _T(""); // 数据库登录密码;
  19. TCHAR g_szDBName[MAX_PATH] = _T(""); // 数据库名称;
  20. TCHAR g_szLogDBName[MAX_PATH] = _T(""); // Log数据库名称;
  21. DWORD g_dwCSPort = 0;
  22. TCHAR g_szShareAccount[MAX_PATH] = {0};
  23. TCHAR g_szSharePasswd[MAX_PATH] = {0};
  24. TCHAR g_szN8ViewSvrIP[MAX_PATH] = {0}; // N8选版服务器IP;
  25. TCHAR g_szN8ViewUserDir[MAX_PATH] = {0}; // N8选版服务器用户目录(共享目录);
  26. TCHAR g_szN8ViewStsDir[MAX_PATH] = {0}; // N8选版服务器统计数据目录(共享目录);
  27. TCHAR g_szN8ViewProductDir[MAX_PATH] = {0}; // N8选版产品配置目录(共享目录);
  28. TCHAR g_szN8ViewShareDir[MAX_PATH] = {0}; // N8选版共享目录;
  29. TCHAR g_szN8ViewShareName[MAX_PATH] = {0};
  30. BOOL g_bEnableClound = FALSE; // 是否启用云选功能;
  31. BOOL g_bExportBarCode = FALSE; // 是否导出条形码位图;
  32. DWORD g_dwSoftRunTime = 0; // 软件剩余可运行时间;
  33. TCHAR g_szCloundDiskAccount[MAX_PATH] ={0}; // 云盘账号;
  34. TCHAR g_szCloundDiskPsw[MAX_PATH] = {0}; // 云盘密码(保存时需要加密);
  35. DWORD g_dwDBPoolMaxCount = 1;
  36. DWORD g_dwDBPoolDef = 1;
  37. BOOL g_bIsCatalogDamage = FALSE;
  38. BOOL g_bCheckdb = FALSE;
  39. DWORD g_dwCheckdbTimeOut = 30;
  40. DWORD g_dwStartDelayTime = 30; // 服务程序延时启动(单位:秒);
  41. CStringArray g_AryAllCatalogName;
  42. TCHAR g_szMacAddress[MAX_PATH] = {0};
  43. BOOL g_bExistSpecialUsb = FALSE;
  44. CString g_strStudioGuid = _T(""); // 数据库全局Guid;
  45. CString g_strGuardCmd = _T("");
  46. CString g_strExecResult = _T("");
  47. CGuardCommandEx g_GuardCommand;
  48. TCHAR g_szGuardcmdMd5[33] = {0};
  49. // start+------------------------------------------------------------------
  50. // Jeff:以下为原本程序全局变量;
  51. #if THE_PRIMARY_GLOBAL_VARIABLE
  52. int g_TCPport = 5678; // Jeff.通信端口;
  53. BOOL g_bReg = 0;
  54. CString g_strdbpsw; // 数据库账号、密码;
  55. CString g_strdbServrename; // 数据库服务名(地址)8
  56. CString g_bakinfo;
  57. CString g_bakphoto;
  58. CString g_mainpath;
  59. SoftKey ytSoftKey;
  60. CString g_localname; // 本地计算机名;
  61. //CDatabase g_db; // 名为"db"的数据库,本年度使用的数据库;
  62. CArray<int, int> g_nYearArray; // 年份(用于命名历史数据库)
  63. //CArray<CDatabase*,CDatabase*>g_dbArray; // 历史数据库;
  64. CArray<CDatabase*, CDatabase*>g_dbHistoryArray; // 历史数据库;
  65. CArray<CDatabase*, CDatabase*>g_branchdbarray;
  66. CArray<CDatabase*, CDatabase*>g_branchdb2array;//除历史年度的分店DB
  67. CStringArray g_branchnamearray;
  68. CStringArray g_branchname2array;
  69. CStringArray g_branchiparray;
  70. CStringArray g_branchdomainarray;
  71. CDatabase *g_pMaindb = NULL;
  72. int g_conncount = -1; // Jeff.加密狗中的座席版数量;
  73. CString g_sdomain;
  74. CStringArray g_conniparray;
  75. CStringArray g_connidarray;
  76. BOOL g_bCheckAll = 0;
  77. BYTE *g_pSkinData = NULL;
  78. DWORD g_SkinLeng;
  79. BOOL g_bAutoRun = 0;
  80. CStringArray g_filedatearray;
  81. CStringArray g_filedatearraynew;
  82. CString g_bakdate;
  83. CString g_localip;
  84. BOOL g_bSteal = 0;
  85. CString g_bakserver1;
  86. CString g_bakserver2;
  87. CString g_bakserver3;
  88. CString g_bakserver4;
  89. DWORD g_tisks = 0;
  90. #if 0
  91. int g_nBakDays = 10; // 备份几天前的时间设置(单位:天数)
  92. int g_nBackupModel = 1;
  93. #endif //#if 0
  94. int g_bAutoCheckSvc = 1; // (1是/0否)自动检查服务(次/小时)
  95. HINSTANCE g_hDllInst = NULL;
  96. HINSTANCE g_Instance = NULL;
  97. #endif
  98. // endof+------------------------------------------------------------------
  99. /************************************************************************/
  100. /* 函数:InitSocketLibrary[2/29/2016 IT];
  101. /* 描述:Initialize Winsock library. This function calls WSAStartup;
  102. /* 参数:;
  103. /* [IN] wVersion:Winsock version use MAKEWORD macro if possible (e.g.: MAKEWORD(2,2));
  104. /* 返回:return true if successful;
  105. /* 注意:关联ReleaseSocketLibrary;
  106. /* 示例:;
  107. /*
  108. /* 修改:;
  109. /* 日期:;
  110. /* 内容:;
  111. /************************************************************************/
  112. bool InitSocketLibrary(IN WORD wVersion)
  113. {
  114. WSADATA WSAData = { 0 };
  115. return ( 0 == WSAStartup( wVersion, &WSAData ) );
  116. }
  117. /************************************************************************/
  118. /* 函数:ReleaseSocketLibrary[2/29/2016 IT];
  119. /* 描述:Release Winsock library;
  120. /* 参数:;
  121. /* 返回:return true if successful;
  122. /* 注意:关联InitSocketLibrary;
  123. /* 示例:;
  124. /*
  125. /* 修改:;
  126. /* 日期:;
  127. /* 内容:;
  128. /************************************************************************/
  129. bool ReleaseSocketLibrary()
  130. {
  131. return ( 0 == WSACleanup() );
  132. }
  133. /************************************************************************/
  134. /* 函数:[5/20/2016 IT];
  135. /* 描述:;
  136. /* 参数:;
  137. /* [IN] :;
  138. /* [OUT] :;
  139. /* [IN/OUT] :;
  140. /* 返回:void;
  141. /* 注意:;
  142. /* 示例:;
  143. /*
  144. /* 修改:;
  145. /* 日期:;
  146. /* 内容:;
  147. /************************************************************************/
  148. int GetIniInfo(const TCHAR *szPath, const TCHAR *szIniName)
  149. {
  150. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  151. TCHAR szDir[_MAX_DIR] = { 0 };
  152. TCHAR szFName[_MAX_FNAME] = { 0 };
  153. TCHAR szExt[_MAX_EXT] = { 0 };
  154. ::GetModuleFileName(NULL, g_ModulePath, sizeof(g_ModulePath) / sizeof(TCHAR));
  155. _stprintf_s(g_ModuleFileName, _T("%s"), g_ModulePath);
  156. _tsplitpath_s(g_ModulePath, szDrive, szDir, szFName, szExt);
  157. _tcscpy_s(g_ModulePath, szDrive);
  158. _tcscat_s(g_ModulePath, szDir);
  159. // -----------------------------------------------------//
  160. TCHAR szFile[MAX_PATH + 1] = _T("");
  161. if (szPath != NULL && szIniName != NULL)
  162. _stprintf_s(szFile, _T("%s\\%s"), szPath, szIniName);
  163. else
  164. _stprintf_s(szFile, _T("%s\\ServiceInfo.ini"), g_ModulePath);
  165. TRACE("szFile =%s\n", szFile);
  166. HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  167. if (ERROR_FILE_NOT_FOUND == GetLastError())
  168. {
  169. return -1;
  170. }
  171. CloseHandle(hFile);
  172. hFile = NULL;
  173. // 获取服务器端信息;
  174. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbSource"), _T(""), g_szDBSource, MAX_PATH, szFile);
  175. g_dwDBServerPort = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbServerPort"), 0, szFile);
  176. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbAccount"), _T(""), g_szDBAccount, MAX_PATH, szFile);
  177. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbPassWord"), _T(""), g_szDBPassWord, MAX_PATH, szFile);
  178. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbName"), _T(""), g_szDBName, MAX_PATH, szFile);
  179. g_dwDBPoolMaxCount = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolMaxObj"), 50, szFile);
  180. g_dwDBPoolDef = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolDef"), 3, szFile);
  181. #if USE_ADO_OPT_DB
  182. GetPrivateProfileString("DatabaseInfo", "LogdbName", "", g_szLogDBName, MAX_PATH, szFile);
  183. #endif//#if USE_ADO_OPT_DB
  184. g_dwCSPort = GetPrivateProfileInt(_T("NetWorkInfo"), _T("TCPChatPort"), 5678, szFile);
  185. if (g_dwDBServerPort != 0)
  186. {
  187. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  188. _stprintf_s(g_szConnectString, DB_SW_CONN_WITH_PORT, g_szDBSource, g_dwDBServerPort,g_szDBName);
  189. else
  190. _stprintf_s(g_szConnectString, DB_SS_CONN_WITH_PORT,g_szDBSource, g_dwDBServerPort, g_szDBName, g_szDBAccount, g_szDBPassWord);
  191. }
  192. else
  193. {
  194. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  195. _stprintf_s(g_szConnectString, DB_SW_CONN_WITHOUT_PORT, g_szDBSource, g_szDBName);
  196. else
  197. _stprintf_s(g_szConnectString, DB_SS_CONN_WITHOUT_PORT,g_szDBSource, g_szDBName, g_szDBAccount, g_szDBPassWord);
  198. }
  199. #if 0
  200. g_nBakDays = GetPrivateProfileInt("Back", "uBakDays", 10, szFile);
  201. g_nBackupModel = GetPrivateProfileInt("Back", "uBackupModel", 1, szFile);
  202. #endif //#if 0
  203. g_bAutoCheckSvc = GetPrivateProfileInt("Back", "AutoCheckSvc", 1, szFile);
  204. // 局域网共享账号和密码;
  205. GetPrivateProfileString(_T("NetWorkInfo"), _T("ShareAccount"), _T(""), g_szShareAccount, MAX_PATH, szFile);
  206. GetPrivateProfileString(_T("NetWorkInfo"), _T("SharePasswd"), _T(""), g_szSharePasswd, MAX_PATH, szFile);
  207. // NemoInfo;
  208. GetPrivateProfileString(_T("NemoInfo"), _T("N8ViewSvr"), _T(""), g_szN8ViewSvrIP, MAX_PATH, szFile);
  209. GetPrivateProfileString(_T("NemoInfo"), _T("N8ViewUserDir"), _T(""), g_szN8ViewUserDir, MAX_PATH, szFile);
  210. GetPrivateProfileString(_T("NemoInfo"), _T("N8ViewStsDir"), _T(""), g_szN8ViewStsDir, MAX_PATH, szFile);
  211. GetPrivateProfileString(_T("NemoInfo"), _T("N8ViewProductDir"), _T(""), g_szN8ViewProductDir, MAX_PATH, szFile);
  212. GetPrivateProfileString(_T("NemoInfo"), _T("N8ViewShareDir"), _T(""), g_szN8ViewShareDir, MAX_PATH, szFile);
  213. GetPrivateProfileString(_T("NemoInfo"), _T("N8ViewShareName"), _T(""), g_szN8ViewShareName, MAX_PATH, szFile);
  214. //////////////////////////////////////////////////////////////////////////
  215. // SystemInfo;
  216. g_bExportBarCode = GetPrivateProfileInt(_T("SystemInfo"), _T("ExportBarCode"), 0, szFile);
  217. g_bEnableClound = GetPrivateProfileInt(_T("SystemInfo"), _T("EnableClound"), 0, szFile);
  218. // 云盘账号;
  219. GetPrivateProfileString(_T("SystemInfo"), _T("CloundDiskAccount"), _T(""), g_szCloundDiskAccount, MAX_PATH, szFile);
  220. // 云盘密码;
  221. GetPrivateProfileString(_T("SystemInfo"), _T("CloundDiskPsw"), _T(""), g_szCloundDiskPsw, MAX_PATH, szFile);
  222. // 解密云盘密码;
  223. CString strResult;
  224. des_crypt((CString)g_szCloundDiskPsw, strResult, DES_DECRYPT);
  225. _stprintf_s(g_szCloundDiskPsw, MAX_PATH, _T("%s"), strResult.GetString());
  226. // 是否检测数据库;
  227. g_bCheckdb = GetPrivateProfileInt(_T("SystemInfo"), _T("Checkdb"), 1, szFile);
  228. // 检测数据库的超时值;
  229. g_dwCheckdbTimeOut = GetPrivateProfileInt(_T("SystemInfo"), _T("CheckdbTimeOut"), 30, szFile);
  230. if ( !g_bCheckdb )
  231. {
  232. g_bIsCatalogDamage = FALSE;
  233. }
  234. g_dwStartDelayTime = GetPrivateProfileInt(_T("SystemInfo"), _T("StartDelayTime"), 0, szFile );
  235. return 0;
  236. }
  237. //------------------------------------------------------------
  238. // 以下全局函数为原程序所有;
  239. #if THE_PRIMARY_GLOBAL_FUNCTION
  240. BOOL AddConn(CDatabase *m_conndb, CString dbname)
  241. {
  242. try {
  243. CString strCon;
  244. strCon.Format(DB_SS_CONN_WITHOUT_PORT, g_strdbServrename, dbname, g_szDBAccount, g_szDBPassWord);
  245. m_conndb->OpenEx(strCon, CDatabase::noOdbcDialog);
  246. }
  247. catch (CDBException * e)
  248. {
  249. WriteTextLog(_T("新建连接时出错:%s"),e->m_strError);
  250. #ifdef _DEBUG
  251. e->ReportError();
  252. #endif
  253. e->Delete();
  254. return 0;
  255. }
  256. return 1;
  257. }
  258. void WriteError(CString str)
  259. {
  260. MyLock lock("DBServerWriteError");
  261. CStdioFile fp;
  262. CString path = g_mainpath + "\\MsgError.txt";
  263. if (::PathFileExists(path))
  264. {
  265. fp.Open(path, CFile::modeWrite);
  266. int length = fp.GetLength();
  267. if (length > 1024 * 1024)
  268. {
  269. fp.Close();
  270. ::DeleteFile(path); // 删除MsgError.txt
  271. return;
  272. }
  273. fp.SeekToEnd();
  274. }
  275. else
  276. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  277. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
  278. fp.Close();
  279. }
  280. void WritePswWrong(CString str)
  281. {
  282. MyLock lock("DBServerWritePswWrong");
  283. CStdioFile fp;
  284. CString path = g_mainpath + "\\PswWrong.txt";
  285. if (::PathFileExists(path))
  286. {
  287. fp.Open(path, CFile::modeWrite);
  288. int length = fp.GetLength();
  289. if (length > 1024 * 1024)
  290. {
  291. fp.Close();
  292. ::DeleteFile(path); // 删除PswWrong.txt
  293. return;
  294. }
  295. fp.SeekToEnd();
  296. }
  297. else
  298. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  299. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
  300. fp.Close();
  301. }
  302. void WriteNullBalance(CString str)
  303. {
  304. MyLock lock("DBServerWriteNullBalance");
  305. CStdioFile fp;
  306. CString path = g_mainpath + "\\NullBalance.txt";
  307. if (::PathFileExists(path))
  308. {
  309. fp.Open(path, CFile::modeWrite);
  310. int length = fp.GetLength();
  311. if (length > 1024 * 1024)
  312. {
  313. fp.Close();
  314. ::DeleteFile(path); // 删除NullBalance.txt
  315. return;
  316. }
  317. fp.SeekToEnd();
  318. }
  319. else
  320. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  321. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
  322. fp.Close();
  323. }
  324. void WriteUnallowIP(CString str)
  325. {
  326. CStdioFile fp;
  327. CString path = g_mainpath + "\\UnallowIP.txt";
  328. if (::PathFileExists(path))
  329. {
  330. fp.Open(path, CFile::modeWrite);
  331. int length = fp.GetLength();
  332. if (length > 1024 * 1024)
  333. {
  334. fp.Close();
  335. ::DeleteFile(path); // 删除UnallowIP.txt
  336. return;
  337. }
  338. fp.SeekToEnd();
  339. }
  340. else
  341. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  342. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
  343. fp.Close();
  344. }
  345. int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
  346. {
  347. UINT num = 0; // number of image encoders
  348. UINT size = 0; // size of the image encoder array in bytes CheckFolderFileExist
  349. ImageCodecInfo* pImageCodecInfo = NULL;
  350. GetImageEncodersSize(&num, &size);
  351. if (size == 0)
  352. return -1; // Failure
  353. pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  354. if (pImageCodecInfo == NULL)
  355. return -1; // Failure
  356. GetImageEncoders(num, size, pImageCodecInfo);
  357. for (UINT j = 0; j < num; ++j)
  358. {
  359. if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
  360. {
  361. *pClsid = pImageCodecInfo[j].Clsid;
  362. free(pImageCodecInfo);
  363. return j; // Success
  364. }
  365. }
  366. free(pImageCodecInfo);
  367. return -1; // Failure
  368. }
  369. void SaveImageToFile(Image *img, CString path)
  370. {
  371. try
  372. {
  373. if (img == NULL)return;
  374. CLSID encoderClsid;
  375. BSTR bstr = path.AllocSysString();
  376. path.MakeLower();
  377. if (path.Right(3) == "bmp")
  378. {
  379. GetEncoderClsid(L"image/bmp", &encoderClsid);
  380. img->Save(bstr, &encoderClsid, NULL);
  381. }
  382. else if (path.Right(3) == "png")
  383. {
  384. GetEncoderClsid(L"image/png", &encoderClsid);
  385. img->Save(bstr, &encoderClsid, NULL);
  386. }
  387. else// if(path.Right (3)=="jpg")
  388. {
  389. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  390. EncoderParameters encoderParameters;
  391. ULONG quality;
  392. encoderParameters.Count = 1;
  393. encoderParameters.Parameter[0].Guid = EncoderQuality;
  394. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  395. encoderParameters.Parameter[0].NumberOfValues = 1;
  396. // Save the image as a JPEG with quality level 100.
  397. quality = 100;
  398. encoderParameters.Parameter[0].Value = &quality;
  399. img->Save(bstr, &encoderClsid, &encoderParameters);
  400. }
  401. SysFreeString(bstr);
  402. }
  403. catch (...)
  404. {
  405. }
  406. }
  407. void SaveImageToFile2(Image *img, CString path, ULONG quality)
  408. {
  409. try
  410. {
  411. if (img == NULL)return;
  412. CLSID encoderClsid;
  413. BSTR bstr = path.AllocSysString();
  414. path.MakeLower();
  415. if (path.Right(3) == "bmp")
  416. {
  417. GetEncoderClsid(L"image/bmp", &encoderClsid);
  418. img->Save(bstr, &encoderClsid, NULL);
  419. }
  420. else if (path.Right(3) == "png")
  421. {
  422. GetEncoderClsid(L"image/png", &encoderClsid);
  423. img->Save(bstr, &encoderClsid, NULL);
  424. }
  425. else// if(path.Right (3)=="jpg")
  426. {
  427. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  428. EncoderParameters encoderParameters;
  429. encoderParameters.Count = 1;
  430. encoderParameters.Parameter[0].Guid = EncoderQuality;
  431. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  432. encoderParameters.Parameter[0].NumberOfValues = 1;
  433. // Save the image as a JPEG with quality level 100.
  434. encoderParameters.Parameter[0].Value = &quality;
  435. img->Save(bstr, &encoderClsid, &encoderParameters);
  436. }
  437. SysFreeString(bstr);
  438. }
  439. catch (...)
  440. {
  441. }
  442. }
  443. void RectFitDes(int width, int height, CRect &rc)
  444. {
  445. float fscale = (float)width / (float)height;
  446. float rcscale = ((float)rc.Width()) / ((float)rc.Height());
  447. int rcwid = rc.Width();
  448. int rchei = rc.Height();
  449. int dt = 0;
  450. if (rcscale < fscale)
  451. {
  452. dt = (rchei - rcwid / fscale) / 2;
  453. rc.top += dt;
  454. rc.bottom -= dt;
  455. }
  456. else
  457. {
  458. dt = (rcwid - rchei*fscale) / 2;
  459. rc.left += dt;
  460. rc.right -= dt;
  461. }
  462. }
  463. ///目录是否存在的检查: 支持网络文件,判断文件时有漏洞
  464. bool CheckFolderFileExist(CString &strPath)
  465. {
  466. strPath.MakeLower();
  467. /*
  468. 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)
  469. {
  470. return ::PathFileExists(strPath);
  471. }
  472. */
  473. if(!PathFileExists(strPath))
  474. return 0;
  475. CString path = strPath;
  476. CFile fp;
  477. if (fp.Open(path, CFile::modeRead))
  478. {
  479. fp.Close();
  480. return 1;
  481. }
  482. if (strPath.Right(1) != "\\")
  483. path += "\\";
  484. path += "zaqw1234.dat";
  485. ::DeleteFile(path);// 删除zaqw1234.dat文件;
  486. if (fp.Open(path, CFile::modeCreate) == 0)
  487. return 0;
  488. fp.Close();
  489. ::DeleteFile(path); // 删除zaqw1234.dat文件;
  490. return 1;
  491. }
  492. bool CheckFileExist(CString &strPath)
  493. {
  494. strPath.MakeLower();
  495. 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)
  496. {
  497. return ::PathFileExists(strPath);
  498. }
  499. CString path = strPath;
  500. CFile fp;
  501. if (fp.Open(path, CFile::modeRead))
  502. {
  503. fp.Close();
  504. return 1;
  505. }
  506. return 0;
  507. }
  508. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  509. {
  510. try
  511. {
  512. HCRYPTPROV hCryptProv;
  513. HCRYPTKEY hKey;
  514. HCRYPTHASH hHash;
  515. PBYTE pbBuffer;
  516. DWORD dwBlockLen;
  517. DWORD dwBufferLen;
  518. DWORD dwCount;
  519. //以下获得一个CSP句柄
  520. if (CryptAcquireContext(
  521. &hCryptProv,
  522. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  523. NULL,
  524. PROV_RSA_FULL,
  525. 0))
  526. {
  527. printf("A cryptographic provider has been acquired. \n");
  528. }
  529. else//密钥容器不存在
  530. {
  531. if (CryptAcquireContext(
  532. &hCryptProv,
  533. NULL,
  534. NULL,
  535. PROV_RSA_FULL,
  536. CRYPT_NEWKEYSET))//创建密钥容器
  537. {
  538. //创建密钥容器成功,并得到CSP句柄
  539. printf("A new key container has been created.\n");
  540. }
  541. else
  542. {
  543. return 0;
  544. }
  545. }
  546. //--------------------------------------------------------------------
  547. // 创建一个会话密钥(session key)
  548. // 会话密钥也叫对称密钥,用于对称加密算法。
  549. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  550. // CryptReleaseContext 期间的阶段。)
  551. //--------------------------------------------------------------------
  552. // Create a hash object.
  553. if (CryptCreateHash(
  554. hCryptProv,
  555. CALG_MD5,
  556. 0,
  557. 0,
  558. &hHash))
  559. {
  560. printf("A hash object has been created. \n");
  561. }
  562. else
  563. {
  564. return 0;
  565. }
  566. //--------------------------------------------------------------------
  567. // 用输入的密码产生一个散列
  568. if (CryptHashData(
  569. hHash,
  570. (BYTE *)szPassword,
  571. strlen(szPassword),
  572. 0))
  573. {
  574. printf("The password has been added to the hash. \n");
  575. }
  576. else
  577. {
  578. return 0;
  579. }
  580. //--------------------------------------------------------------------
  581. // 通过散列生成会话密钥(session key)
  582. if (CryptDeriveKey(
  583. hCryptProv,
  584. ENCRYPT_ALGORITHM,
  585. hHash,
  586. KEYLENGTH,
  587. &hKey))
  588. {
  589. printf("An encryption key is derived from the password hash. \n");
  590. }
  591. else
  592. {
  593. return 0;
  594. }
  595. //--------------------------------------------------------------------
  596. // Destroy the hash object.
  597. CryptDestroyHash(hHash);
  598. hHash = NULL;
  599. //--------------------------------------------------------------------
  600. // The session key is now ready.
  601. //--------------------------------------------------------------------
  602. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  603. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  604. // 数据长度。
  605. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  606. //--------------------------------------------------------------------
  607. // Determine the block size. If a block cipher is used,
  608. // it must have room for an extra block.
  609. if (ENCRYPT_BLOCK_SIZE > 1)
  610. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  611. else
  612. dwBufferLen = dwBlockLen;
  613. dwCount = dwBufferLen;
  614. //--------------------------------------------------------------------
  615. // In a do loop, encrypt the source file and write to the source file.
  616. int count;
  617. // if(leng%dwBlockLen==0)
  618. count = leng / dwBufferLen;
  619. // else
  620. // count=leng/dwBlockLen+1;
  621. int i = 0;
  622. for (i = 0; i < count; i++)
  623. {
  624. pbBuffer = buffer + i*dwBufferLen;
  625. // 加密数据
  626. if (!CryptEncrypt(
  627. hKey, //密钥
  628. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  629. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  630. //这里通过判断是否到文件尾来决定是否为最后一块。
  631. 0, //保留
  632. pbBuffer, //输入被加密数据,输出加密后的数据
  633. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  634. dwBufferLen)) //pbBuffer的大小。
  635. {
  636. return 0;
  637. }
  638. }
  639. if (leng%dwBlockLen)
  640. {
  641. pbBuffer = buffer + i*dwBufferLen;
  642. dwCount = leng - i*dwBufferLen;
  643. if (!CryptEncrypt(
  644. hKey, //密钥
  645. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  646. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  647. //这里通过判断是否到文件尾来决定是否为最后一块。
  648. 0, //保留
  649. pbBuffer, //输入被加密数据,输出加密后的数据
  650. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  651. dwBufferLen)) //pbBuffer的大小。
  652. {
  653. return 0;
  654. }
  655. }
  656. //--------------------------------------------------------------------
  657. // Destroy session key.
  658. if (hKey)
  659. CryptDestroyKey(hKey);
  660. //--------------------------------------------------------------------
  661. // Destroy hash object.
  662. if (hHash)
  663. CryptDestroyHash(hHash);
  664. //--------------------------------------------------------------------
  665. // Release provider handle.
  666. if (hCryptProv)
  667. CryptReleaseContext(hCryptProv, 0);
  668. return(TRUE);
  669. }
  670. catch (...)
  671. {
  672. }
  673. }
  674. /************************************************************************/
  675. /* 函数:[3/15/2016 IT];
  676. /* 描述:;
  677. /* 参数:;
  678. /* [IN] strBackupDir:数据库的备份路径;
  679. /* [IN] bBackupToday: 是否备份今天的数据库;
  680. /* [IN] bBackupHistory:是否备份历史数据库;
  681. /* 返回:void;
  682. /* 注意:;
  683. /* 示例:;
  684. /*
  685. /* 修改:;
  686. /* 日期:;
  687. /* 内容:;
  688. /************************************************************************/
  689. void BackupDatabase( IN CString strBackupDir, IN BOOL bBackupToday, IN BOOL bBackupHistory )
  690. {
  691. // 1.使用master系统数据库来备份;
  692. CDatabase tagMaster;
  693. try
  694. {
  695. TCHAR szConnectString[MAX_PATH] = _T("");
  696. if( g_dwDBServerPort != 0)
  697. {
  698. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  699. _stprintf_s(szConnectString,DB_SW_CONN_WITH_PORT, g_szDBSource, g_dwDBServerPort, _T("master"));
  700. else
  701. _stprintf_s(szConnectString,DB_SS_CONN_WITH_PORT, g_szDBSource, g_dwDBServerPort, _T("master"), g_szDBAccount, g_szDBPassWord);
  702. }
  703. else
  704. {
  705. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  706. _stprintf_s(szConnectString,DB_SW_CONN_WITHOUT_PORT, g_szDBSource, _T("master"));
  707. else
  708. _stprintf_s(szConnectString,DB_SS_CONN_WITHOUT_PORT, g_szDBSource, _T("master"), g_szDBAccount, g_szDBPassWord);
  709. }
  710. tagMaster.OpenEx(szConnectString, CDatabase::noOdbcDialog);
  711. }
  712. catch (CDBException *e)
  713. {
  714. #ifdef _DEBUG
  715. e->ReportError();
  716. #endif
  717. e->Delete();
  718. }
  719. try
  720. {
  721. // 2.获取昨天的日期tm2;
  722. CTime tToday = CTime::GetCurrentTime();
  723. CTimeSpan tSpan(1, 0, 0, 0);
  724. CTime tYesterday;
  725. if (bBackupToday)
  726. tYesterday = tToday;
  727. else
  728. tYesterday = tToday - tSpan;
  729. CString savepath;
  730. savepath.Format(_T("%s\\%s(sql).bak"), strBackupDir, tYesterday.Format(_T("%Y%m%d")));
  731. BOOL bBak = 0; // 是否备份成功;
  732. BOOL bBak2 = 0; // 是否备份历史年限数据库成功;
  733. // 3.如果没找到<昨天>备份数据库,则进行备份;
  734. if (!PathFileExists(savepath))
  735. {
  736. tagMaster.SetQueryTimeout(600);
  737. {
  738. CString sql;
  739. sql.Format(_T("BACKUP DATABASE [%s] TO DISK='%s' WITH FORMAT"), g_szDBName, savepath);
  740. try
  741. {
  742. tagMaster.ExecuteSQL(sql);
  743. bBak = 1;
  744. g_bakinfo = _T("资料备份成功");
  745. }
  746. catch (CDBException * e)
  747. {
  748. #ifdef _DEBUG
  749. e->ReportError();
  750. #endif
  751. WriteTextLog(_T("备份失败:%s"),e->m_strError);
  752. e->Delete();
  753. }
  754. }
  755. tagMaster.SetQueryTimeout(100);
  756. }
  757. // 4.Jeff.是否备份历史数据库;
  758. if (bBackupHistory)
  759. {
  760. for (int i = CTime::GetCurrentTime().GetYear() - 1; i >= 2009; i--)
  761. {
  762. tagMaster.SetQueryTimeout(600);
  763. {
  764. CString savepath2;
  765. savepath2.Format(_T("%s\\%s-%d(sql).bak"), strBackupDir, tYesterday.Format(_T("%Y%m%d")), i);
  766. CString sql;
  767. sql.Format(_T("BACKUP DATABASE [%d] TO DISK='%s' WITH FORMAT"), i, savepath2);
  768. try
  769. {
  770. tagMaster.ExecuteSQL(sql);
  771. bBak2 = 1;
  772. }
  773. catch (CDBException * e)
  774. {
  775. #ifdef _DEBUG
  776. e->ReportError();
  777. #endif
  778. e->Delete();
  779. break;
  780. }
  781. }
  782. tagMaster.SetQueryTimeout(100);
  783. }
  784. }
  785. // 5.昨天的数据库备份是否备份成功;
  786. if (bBak)
  787. {
  788. CTimeSpan dt2(31, 0, 0, 0);
  789. CTimeSpan dt3(365, 0, 0, 0);
  790. // tm:今天, tm3:1个月前, tm4:1年前;
  791. CTime tm3 = tToday - dt2;
  792. CTime tm4 = tToday - dt3;
  793. // 6.循环删除数据库备份;
  794. while (tm3 > tm4)
  795. {
  796. savepath.Format(_T("%s\\%s.bak"), strBackupDir, tm3.Format(_T("%Y%m%d")));
  797. #ifdef SQLSERVER_VERSION
  798. savepath.Format(_T("%s\\%s(sql).bak"), strBackupDir, tm3.Format(_T("%Y%m%d")));
  799. #endif
  800. // Jeff.删除一个月前的数据库备份;
  801. if (!CheckFileExist(savepath))
  802. break;
  803. ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
  804. tm3 -= tSpan; // dt 1天的间隔;
  805. }
  806. }
  807. if (bBak2)
  808. {
  809. CTimeSpan dt2(15, 0, 0, 0);
  810. CTimeSpan dt3(365, 0, 0, 0);
  811. for (int i = tToday.GetYear() - 1; i >= 2009; i--)
  812. {
  813. CTime tm3 = tToday - dt2;
  814. CTime tm4 = tToday - dt3;
  815. while (tm3 > tm4)
  816. {
  817. savepath.Format(_T("%s\\%s-%d.bak"), strBackupDir, tm3.Format(_T("%Y%m%d")), i);
  818. #ifdef SQLSERVER_VERSION
  819. savepath.Format(_T("%s\\%s-%d(sql).bak"), strBackupDir, tm3.Format(_T("%Y%m%d")), i);
  820. #endif
  821. if (!CheckFileExist(savepath))
  822. break;
  823. ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
  824. tm3 -= tSpan;
  825. }
  826. }
  827. }
  828. }
  829. catch (...)
  830. {
  831. WriteTextLog("出错在函数BackupDatabase中");
  832. }
  833. tagMaster.Close();
  834. }
  835. void BakData(CString savedir, BOOL bToday, BOOL bBakHis)// back data
  836. {
  837. #ifdef SQLSERVER_VERSION
  838. BackupDatabase(savedir, bToday, bBakHis);
  839. return;
  840. #endif
  841. try
  842. {
  843. CTime tm = CTime::GetCurrentTime();
  844. CTimeSpan dt(1, 0, 0, 0);
  845. CTime tm2;
  846. if (bToday)
  847. tm2 = tm;
  848. else
  849. tm2 = tm - dt;
  850. CString savepath;
  851. savepath.Format("%s\\%s.bak", savedir, tm2.Format("%Y%m%d"));
  852. #ifdef SQLSERVER_VERSION
  853. savepath.Format("%s\\%s(sql).bak", savedir, tm2.Format("%Y%m%d"));
  854. #endif
  855. BOOL bBak = 0;
  856. BOOL bBak2 = 0;
  857. {
  858. TCHAR szDBFile[MAX_PATH] = _T(""); // Jeff.add
  859. #ifdef SQLSERVER_VERSION
  860. _stprintf_s(szDBFile, _T("%s\\数据\\%s.mdf"), g_ModulePath, g_szDBName);
  861. //if(::CopyFile (g_mainpath+"\\数据\\db.mdf", savepath, 0))
  862. #else
  863. sprintf(szDBFile, "%s\\数据\\%s.mdb", g_ModulePath, g_szDBName);
  864. //if(::CopyFile (g_mainpath+"\\数据\\db.mdb", savepath, 0))
  865. #endif
  866. if (::CopyFile(szDBFile, savepath, 0))
  867. {
  868. bBak = 1;
  869. g_bakinfo = "资料备份成功";
  870. }
  871. }
  872. if (bBak)
  873. {
  874. CTimeSpan dt2(15, 0, 0, 0);
  875. CTimeSpan dt3(365, 0, 0, 0);
  876. CTime tm3 = tm - dt2;
  877. CTime tm4 = tm - dt3;
  878. while (tm3 > tm4)
  879. {
  880. savepath.Format("%s\\%s.bak", savedir, tm3.Format("%Y%m%d"));
  881. #ifdef SQLSERVER_VERSION
  882. savepath.Format("%s\\%s(sql).bak", savedir, tm3.Format("%Y%m%d"));
  883. #endif
  884. if (!CheckFileExist(savepath))
  885. break;
  886. ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
  887. tm3 -= dt;
  888. }
  889. }
  890. //备份历史数据
  891. for (int i = tm.GetYear() - 1; i >= 1900; i--)
  892. {
  893. #ifdef SQLSERVER_VERSION
  894. savepath.Format("%s\\数据\\%d.mdf", g_mainpath, i);
  895. #else
  896. savepath.Format("%s\\数据\\%d.mdb", g_mainpath, i);
  897. #endif
  898. if (CheckFileExist(savepath) == 0)
  899. break;
  900. CString savepath2 = _T("");
  901. savepath2.Format("%s\\%s-%d.bak", savedir, tm2.Format("%Y%m%d"), i);
  902. #ifdef SQLSERVER_VERSION
  903. savepath2.Format("%s\\%s-%d(sql).bak", savedir, tm2.Format("%Y%m%d"), i);
  904. #endif
  905. if (::CopyFile(savepath, savepath2, 0))
  906. bBak2 = 1;
  907. }
  908. if (bBak2)
  909. {
  910. CTimeSpan dt2(7, 0, 0, 0);
  911. CTimeSpan dt3(365, 0, 0, 0);
  912. for (int i = tm.GetYear() - 1; i >= 1900; i--)
  913. {
  914. #ifdef SQLSERVER_VERSION
  915. savepath.Format("%s\\数据\\%d.mdf", g_mainpath, i);
  916. #else
  917. savepath.Format("%s\\数据\\%d.mdb", g_mainpath, i);
  918. #endif
  919. if (CheckFileExist(savepath) == 0)break;
  920. CTime tm3 = tm - dt2;
  921. CTime tm4 = tm - dt3;
  922. while (tm3 > tm4)
  923. {
  924. savepath.Format("%s\\%s-%d.bak", savedir, tm3.Format("%Y%m%d"), i);
  925. #ifdef SQLSERVER_VERSION
  926. savepath.Format("%s\\%s-%d(sql).bak", savedir, tm3.Format("%Y%m%d"), i);
  927. #endif
  928. if (!CheckFileExist(savepath))
  929. break;
  930. ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
  931. tm3 -= dt;
  932. }
  933. }
  934. }
  935. }
  936. catch (...)
  937. {
  938. WriteTextLog("出错在函数BakData中");
  939. }
  940. }
  941. DWORD FindAppProcessID(CString path)
  942. {
  943. try
  944. {
  945. HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  946. PROCESSENTRY32 Info;
  947. Info.dwSize = sizeof(PROCESSENTRY32);
  948. path.MakeLower();
  949. if (::Process32First(handle, &Info))
  950. {
  951. do
  952. {
  953. CString ss = Info.szExeFile;
  954. ss.MakeLower();
  955. if (ss == path)
  956. {
  957. ::CloseHandle(handle);
  958. return Info.th32ProcessID;
  959. }
  960. } while (::Process32Next(handle, &Info));
  961. ::CloseHandle(handle);
  962. }
  963. return 0;
  964. }
  965. catch (...)
  966. {
  967. }
  968. return 0;
  969. }
  970. BOOL lyfzCopyFileEx(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, const BOOL &bFailIfExists)
  971. {
  972. // 1.如果是覆盖选项;
  973. if (!bFailIfExists)
  974. return CopyFile(lpExistingFileName, lpNewFileName, bFailIfExists);
  975. // 2.如果不是覆盖选项;
  976. TCHAR szNewFileName[1024] = { 0 };
  977. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  978. TCHAR szDir[_MAX_DIR] = { 0 };
  979. TCHAR szFna[_MAX_FNAME] = { 0 };
  980. TCHAR szExt[_MAX_EXT] = { 0 };
  981. _stprintf_s(szNewFileName, _T("%s"), lpNewFileName);
  982. _tsplitpath_s(szNewFileName, szDrive, szDir, szFna, szExt);
  983. int nIndex = 1;
  984. while (PathFileExists(szNewFileName))
  985. {
  986. _stprintf_s(szNewFileName, _T("%s%s%s (%d)%s"), szDrive, szDir, szFna, nIndex++, szExt);
  987. }
  988. return CopyFile(lpExistingFileName, szNewFileName, bFailIfExists);
  989. }
  990. void GetPathFromNetShareName(CString name, CString &sRet)
  991. {
  992. if (sRet.Find(":") != -1)return;
  993. NET_API_STATUS res;
  994. SHARE_INFO_502 * psi = NULL;
  995. WCHAR wszShareName[255];
  996. TCHAR szPathName[255];
  997. TCHAR path[255];
  998. memset(path, 0, 255);
  999. //转换成宽字符
  1000. MultiByteToWideChar(CP_ACP, 0, name, -1, wszShareName, 255);
  1001. #if 0
  1002. res = NetShareGetInfo(NULL, (char *)wszShareName, 502, (LPBYTE *)&psi);
  1003. #else
  1004. res = NetShareGetInfo(NULL, wszShareName, 502, (LPBYTE *)&psi);
  1005. #endif
  1006. if (res == NERR_Success)
  1007. {
  1008. wsprintf((char*)szPathName, "%S ", psi->shi502_path);
  1009. sRet = szPathName;
  1010. sRet.TrimRight();
  1011. }
  1012. else
  1013. {
  1014. CString str;
  1015. #if 0 // Jeff delete;
  1016. str.Format("转换路径失败-%d", res);
  1017. ::WriteTextLog(str);//Jeff delete,此处使用了系统域符号,其实完全没作用.
  1018. #else
  1019. str.Format("转换路径失败(\"%s\"),", sRet);
  1020. ErrorExit(str, res);
  1021. #endif
  1022. }
  1023. //释放缓冲
  1024. NetApiBufferFree(psi);
  1025. }
  1026. CString GetModifyTime2(CString path)
  1027. {
  1028. try
  1029. {
  1030. CFileFind finder;
  1031. BOOL bWorking = finder.FindFile(path);
  1032. if (bWorking)
  1033. {
  1034. finder.FindNextFile();
  1035. CTime tm;
  1036. finder.GetLastWriteTime(tm);
  1037. CString stime;
  1038. stime.Format("%d%02d%02d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
  1039. return stime;
  1040. }
  1041. return "";
  1042. }
  1043. catch (CFileException *e)
  1044. {
  1045. WriteTextLog(_T("2.读文件[%s]修改时间出错:%d"), e->m_strFileName, e->m_lOsError);
  1046. e->Delete();
  1047. }
  1048. return _T("");
  1049. }
  1050. CString GetModifyTime(CString path)
  1051. {
  1052. try
  1053. {
  1054. HANDLE hFile;
  1055. WIN32_FIND_DATA wfd;
  1056. // SYSTEMTIME systime;
  1057. FILETIME localtime;
  1058. CString stime; //输出时间
  1059. memset(&wfd, 0, sizeof(wfd));
  1060. if ((hFile = FindFirstFile(path, &wfd)) == INVALID_HANDLE_VALUE)
  1061. return "";
  1062. //ok,转换时间
  1063. FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localtime);
  1064. stime.Format("%16d%16d", localtime.dwHighDateTime, localtime.dwLowDateTime);
  1065. return stime;
  1066. }
  1067. catch (CFileException *e)
  1068. {
  1069. CString strError;
  1070. WriteTextLog(_T("读文件[%s]修改时间出错:%d"), e->m_strFileName, e->m_lOsError);
  1071. e->Delete();
  1072. }
  1073. return _T("");
  1074. }
  1075. CString GetErrorInfo(IN const DWORD &dwError)
  1076. {
  1077. LPVOID lpMsgBuf;
  1078. CString strErrorInfo;
  1079. BOOL fOk = FormatMessage(
  1080. FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,
  1081. NULL,
  1082. dwError,
  1083. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  1084. (LPTSTR)&lpMsgBuf,
  1085. 0, NULL
  1086. );
  1087. if (!fOk)
  1088. {
  1089. // Is it a network-related error?
  1090. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1091. if (hDll != NULL)
  1092. {
  1093. FormatMessage(
  1094. FORMAT_MESSAGE_FROM_HMODULE |FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,
  1095. hDll, dwError,
  1096. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1097. (LPTSTR)&lpMsgBuf,
  1098. 0, NULL
  1099. );
  1100. FreeLibrary(hDll);
  1101. }
  1102. }
  1103. if (lpMsgBuf != NULL)
  1104. {
  1105. strErrorInfo.Format(_T("错误码=%d,系统描述:%s"), dwError, (PCTSTR)LocalLock(lpMsgBuf));
  1106. LocalFree(lpMsgBuf);
  1107. }
  1108. else
  1109. {
  1110. strErrorInfo.Format(_T("错误码=%d,系统描述:未知错误内容!"), dwError);
  1111. }
  1112. return strErrorInfo;
  1113. }
  1114. //--------------------------------------------------------------------------------
  1115. // Jeff add 2014.06.23;
  1116. // 函数:ErrorExit
  1117. // 描述:
  1118. // 参数:
  1119. // lpszFunction:函数名;
  1120. // dwError:错误码;
  1121. //
  1122. //--------------------------------------------------------------------------------
  1123. void ErrorExit(CString strDescription, const DWORD &dwError)
  1124. {
  1125. #if 1
  1126. LPVOID lpMsgBuf;
  1127. BOOL fOk = FormatMessage(
  1128. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1129. FORMAT_MESSAGE_FROM_SYSTEM |
  1130. FORMAT_MESSAGE_IGNORE_INSERTS,
  1131. NULL,
  1132. dwError,
  1133. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  1134. (LPTSTR)&lpMsgBuf,
  1135. 0, NULL);
  1136. if (!fOk)
  1137. {
  1138. // Is it a network-related error?
  1139. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1140. if (hDll != NULL)
  1141. {
  1142. FormatMessage(
  1143. FORMAT_MESSAGE_FROM_HMODULE |
  1144. FORMAT_MESSAGE_FROM_SYSTEM |
  1145. FORMAT_MESSAGE_IGNORE_INSERTS,
  1146. hDll,
  1147. dwError,
  1148. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1149. (LPTSTR)&lpMsgBuf,
  1150. 0,
  1151. NULL);
  1152. FreeLibrary(hDll);
  1153. }
  1154. }
  1155. if (lpMsgBuf != NULL)
  1156. {
  1157. WriteTextLog("%s.错误码=%d,Windows描述:%s", strDescription, dwError, (LPCTSTR)LocalLock(lpMsgBuf));
  1158. LocalFree(lpMsgBuf);
  1159. }
  1160. else
  1161. {
  1162. WriteTextLog(strDescription);
  1163. }
  1164. #else
  1165. HLOCAL hlocal = NULL; // Buffer that gets the error message string
  1166. // Get the error code's textual description
  1167. BOOL fOk = FormatMessage(
  1168. FORMAT_MESSAGE_FROM_SYSTEM |
  1169. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1170. FORMAT_MESSAGE_IGNORE_INSERTS,
  1171. NULL,
  1172. dwError,
  1173. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1174. (PTSTR)&hlocal,
  1175. 0,
  1176. NULL);
  1177. if (!fOk)
  1178. {
  1179. // Is it a network-related error?
  1180. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1181. if (hDll != NULL)
  1182. {
  1183. FormatMessage(
  1184. FORMAT_MESSAGE_FROM_HMODULE |
  1185. FORMAT_MESSAGE_FROM_SYSTEM |
  1186. FORMAT_MESSAGE_IGNORE_INSERTS,
  1187. hDll,
  1188. dwError,
  1189. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1190. (PTSTR)&hlocal,
  1191. 0,
  1192. NULL);
  1193. FreeLibrary(hDll);
  1194. }
  1195. }
  1196. if (hlocal != NULL)
  1197. {
  1198. CString strDisplay;
  1199. strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
  1200. WriteTextLog(strDisplay);
  1201. LocalFree(hlocal);
  1202. }
  1203. else
  1204. {
  1205. WriteTextLog("Error number not found.");
  1206. }
  1207. #endif
  1208. }
  1209. void MyGetIPByName(CString &name)
  1210. {
  1211. CString strIP = name;
  1212. name.Empty();
  1213. DWORD dwServerIP = 0;
  1214. HOSTENT *host = gethostbyname(strIP);
  1215. struct in_addr addr;
  1216. if (host != NULL)
  1217. {
  1218. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1219. {
  1220. memset(&addr, 0, sizeof(addr));
  1221. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1222. dwServerIP = ntohl(addr.S_un.S_addr);
  1223. }
  1224. BYTE* pIP = (BYTE*)&dwServerIP;
  1225. name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1226. }
  1227. }
  1228. void EncryptFile3(CString path)
  1229. {
  1230. CFile fp;
  1231. if (fp.Open(path, CFile::modeRead) == 0)return;
  1232. DWORD leng = fp.GetLength();
  1233. BYTE *pData = new BYTE[leng];
  1234. fp.Read(pData, leng);
  1235. fp.Close();
  1236. EncryptFile2(pData, leng, "z9871234abcd");
  1237. fp.Open(path, CFile::modeWrite);
  1238. fp.Write(pData, leng);
  1239. fp.Close();
  1240. delete[]pData;
  1241. }
  1242. int FindArray(CStringArray *pArray, CString Str)
  1243. {
  1244. for (int i = 0; i < pArray->GetSize(); i++)
  1245. {
  1246. if (pArray->ElementAt(i) == Str)
  1247. return i;
  1248. }
  1249. return -1;
  1250. }
  1251. BOOL isInnerIP(DWORD a_ip)
  1252. {
  1253. BOOL bValid = 0;
  1254. if ((a_ip >> 24 == 0xa) || (a_ip >> 16 == 0xc0a8) || (a_ip >> 22 == 0x2b0))
  1255. {
  1256. bValid = 1;
  1257. }
  1258. return bValid;
  1259. }
  1260. BOOL CompareIP(CString name1, CString name2)
  1261. {
  1262. CStringArray g_serverarray1;
  1263. CStringArray g_serverarray2;
  1264. CString strIP = name1;
  1265. DWORD dwServerIP = 0;
  1266. HOSTENT *host = gethostbyname(strIP);
  1267. struct in_addr addr;
  1268. if (host != NULL)
  1269. {
  1270. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1271. {
  1272. memset(&addr, 0, sizeof(addr));
  1273. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1274. dwServerIP = ntohl(addr.S_un.S_addr);
  1275. BYTE* pIP = (BYTE*)&dwServerIP;
  1276. strIP.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1277. g_serverarray1.Add(strIP);
  1278. }
  1279. }
  1280. while (g_serverarray1.GetSize() > 1)
  1281. {
  1282. CString name;
  1283. BOOL bFind = 0;
  1284. for (int i = 0; i < g_serverarray1.GetSize(); i++)
  1285. {
  1286. DWORD dwServerIP = inet_addr(g_serverarray1.ElementAt(i));
  1287. dwServerIP = htonl(dwServerIP);//加了才正确
  1288. if (isInnerIP(dwServerIP) == 0)
  1289. {
  1290. g_serverarray1.RemoveAt(i);
  1291. bFind = 1;
  1292. break;
  1293. }
  1294. }
  1295. if (bFind == 0)
  1296. {
  1297. break;
  1298. }
  1299. }
  1300. //////////
  1301. {
  1302. CString strIP = name2;
  1303. DWORD dwServerIP = 0;
  1304. HOSTENT *host = gethostbyname(strIP);
  1305. struct in_addr addr;
  1306. if (host != NULL)
  1307. {
  1308. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1309. {
  1310. memset(&addr, 0, sizeof(addr));
  1311. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1312. dwServerIP = ntohl(addr.S_un.S_addr);
  1313. BYTE* pIP = (BYTE*)&dwServerIP;
  1314. strIP.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1315. g_serverarray2.Add(strIP);
  1316. }
  1317. }
  1318. while (g_serverarray2.GetSize() > 1)
  1319. {
  1320. CString name;
  1321. BOOL bFind = 0;
  1322. for (int i = 0; i < g_serverarray2.GetSize(); i++)
  1323. {
  1324. DWORD dwServerIP = inet_addr(g_serverarray2.ElementAt(i));
  1325. dwServerIP = htonl(dwServerIP);//加了才正确
  1326. if (isInnerIP(dwServerIP) == 0)
  1327. {
  1328. g_serverarray2.RemoveAt(i);
  1329. bFind = 1;
  1330. break;
  1331. }
  1332. }
  1333. if (bFind == 0)
  1334. {
  1335. break;
  1336. }
  1337. }
  1338. }
  1339. if (g_serverarray1.GetSize() == 0 || g_serverarray2.GetSize() == 0)return 1;
  1340. for (int i = 0; i < g_serverarray1.GetSize(); i++)
  1341. {
  1342. for (int j = 0; j < g_serverarray2.GetSize(); j++)
  1343. {
  1344. if (g_serverarray1.ElementAt(i) == g_serverarray2.ElementAt(j))return 0;
  1345. }
  1346. }
  1347. return 1;
  1348. }
  1349. void DeleteDirectory(CString strDir)
  1350. {
  1351. if ( strDir.IsEmpty() || !PathFileExists(strDir) )
  1352. {
  1353. WriteTextLog(_T("DeleteDirectory:路径无效!"));
  1354. return;
  1355. }
  1356. try
  1357. {
  1358. // 首先删除文件及子文件夹;
  1359. CFileFind ff;
  1360. BOOL bFound = ff.FindFile(strDir + _T("\\*"), 0);
  1361. while (bFound)
  1362. {
  1363. bFound = ff.FindNextFile();
  1364. if (ff.GetFileName() == _T(".") || ff.GetFileName() == _T(".."))
  1365. continue;
  1366. // 去掉文件(夹)只读等属性;
  1367. if ( SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL) )
  1368. {
  1369. if (ff.IsDirectory())
  1370. { // 递归删除子文件夹;
  1371. DeleteDirectory(ff.GetFilePath());
  1372. RemoveDirectory(ff.GetFilePath());
  1373. }
  1374. else
  1375. {
  1376. DeleteFile(ff.GetFilePath()); // Jeff:删除某文件夹下的所有子文件;
  1377. WriteTextLog(ff.GetFilePath());
  1378. }
  1379. }
  1380. else
  1381. {
  1382. WriteTextLog(_T("设置文件[%s]属性失败:%d"), ff.GetFilePath(), GetLastError());
  1383. }
  1384. }
  1385. ff.Close();
  1386. // 然后删除该文件夹;
  1387. RemoveDirectory(strDir);
  1388. }
  1389. catch (...)
  1390. {
  1391. WriteTextLog(_T("出错在函数deldir中"));
  1392. }
  1393. }
  1394. int GetLengthEx(CString str)
  1395. {
  1396. wchar_t wstr[500];
  1397. int k = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, strlen(str), wstr, 500);
  1398. return k;
  1399. }
  1400. CString GetFileSize(CString path)
  1401. {
  1402. CString str = "0";
  1403. try
  1404. {
  1405. CFile fp;
  1406. fp.Open(path, CFile::modeRead);
  1407. str.Format("%d", fp.GetLength());
  1408. fp.Close();
  1409. }
  1410. catch (...)
  1411. {
  1412. }
  1413. return str;
  1414. }
  1415. void SortArray(CStringArray &datearray)
  1416. {
  1417. try
  1418. {
  1419. CString strtemp;
  1420. int last = datearray.GetSize() - 1;
  1421. bool sorted = true;
  1422. do {
  1423. sorted = true;
  1424. for (int i = 0; i < last; i++)
  1425. {
  1426. if (datearray[i] < datearray[i + 1])
  1427. {
  1428. strtemp = datearray[i];
  1429. datearray[i] = datearray[i + 1];
  1430. datearray[i + 1] = strtemp;
  1431. sorted = false;
  1432. }
  1433. }
  1434. last--;
  1435. } while (!sorted);
  1436. }
  1437. catch (...)
  1438. {
  1439. WriteTextLog("SortArray出错");
  1440. }
  1441. }
  1442. int CheckPhoneType(CString phoneno)//移动:0 联通:1 电信:2 小灵通:3 未知:-1
  1443. {
  1444. if (phoneno.IsEmpty())return -1;
  1445. int i = 0;
  1446. for (i = 0; i < phoneno.GetLength(); i++)
  1447. {
  1448. if (phoneno.GetAt(i) < '0' || phoneno.GetAt(i) > '9')return -1;
  1449. }
  1450. if (phoneno.GetAt(0) == '1')
  1451. {
  1452. if (phoneno.GetLength() != 11)
  1453. return -1;
  1454. int mobile[] = { 139, 138, 137, 136, 135, 134, 159, 158, 152, 151, 150, 157, 188, 187, 144, 182, 147, 183, 184 };
  1455. int unicom[] = { 130, 131, 132, 155, 156, 186, 185 };
  1456. int telecom[] = { 133, 153, 189, 180, 181 };
  1457. for (i = 0; i < sizeof(mobile) / sizeof(int); i++)
  1458. {
  1459. if (mobile[i] == atoi(phoneno.Left(3)))
  1460. {
  1461. return 0;
  1462. }
  1463. }
  1464. for (i = 0; i < sizeof(unicom) / sizeof(int); i++)
  1465. {
  1466. if (unicom[i] == atoi(phoneno.Left(3)))
  1467. {
  1468. return 1;
  1469. }
  1470. }
  1471. for (i = 0; i < sizeof(telecom) / sizeof(int); i++)
  1472. {
  1473. if (telecom[i] == atoi(phoneno.Left(3)))
  1474. {
  1475. return 2;
  1476. }
  1477. }
  1478. return -1;
  1479. }
  1480. /* else if(phoneno.GetAt (0)=='0')
  1481. {
  1482. if(phoneno.GetLength ()>=10 && phoneno.GetLength ()<=12)
  1483. {
  1484. return 3;
  1485. }
  1486. }*/
  1487. return -1;
  1488. }
  1489. CTime GetTmFromStr(CString date)
  1490. {
  1491. try
  1492. {
  1493. CTime tm(atoi(date.Mid(0, 4)), atoi(date.Mid(5, 2)), atoi(date.Mid(8, 2)), 0, 0, 0);
  1494. CString ss;
  1495. ss.Format("%d-%d-%d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
  1496. return tm;
  1497. }
  1498. catch (...)
  1499. {
  1500. date = "1980-01-01";
  1501. CTime tm(atoi(date.Mid(0, 4)), atoi(date.Mid(5, 2)), atoi(date.Mid(8, 2)), 0, 0, 0);
  1502. return tm;
  1503. }
  1504. }
  1505. void ConvertToPrice(CString &str)
  1506. {
  1507. if (str.Find('.') == -1)return;
  1508. str.TrimRight('0');
  1509. str.TrimRight('.');
  1510. }
  1511. void SortArray(CArray<CStringArray, CStringArray>*List1array, int pos)
  1512. {
  1513. CString stemp;
  1514. int last = List1array->GetSize() - 1;
  1515. int ret = last;
  1516. bool sorted = true;
  1517. do
  1518. {
  1519. sorted = true;
  1520. for (int i = 0; i < last; i++)
  1521. {
  1522. if (List1array->ElementAt(i).ElementAt(pos) < List1array->ElementAt(i + 1).ElementAt(pos))
  1523. {
  1524. for (int a = 0; a < List1array->ElementAt(i).GetSize(); a++)
  1525. {
  1526. stemp = List1array->ElementAt(i).ElementAt(a);
  1527. List1array->ElementAt(i).SetAt(a, List1array->ElementAt(i + 1).ElementAt(a));
  1528. List1array->ElementAt(i + 1).SetAt(a, stemp);
  1529. }
  1530. sorted = false;
  1531. }
  1532. }
  1533. last--;
  1534. } while (!sorted);
  1535. }
  1536. void DeCodePicCount(int &value1, int &value2)
  1537. {
  1538. // SoftKey ytSoftKey;
  1539. int ret = 0;//ytSoftKey.Ini();
  1540. if (ret != 0)
  1541. {
  1542. return;
  1543. }
  1544. int d[8]; double f[8];
  1545. 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("");
  1546. //运行自定义函数
  1547. ret = ytSoftKey.decryptpiccount(&value1, &value2, &d[2], &d[3], &d[4], &d[5], &d[6], &d[7],
  1548. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1549. s0, s1, s2, s3, s4, s5, s6, s7);
  1550. if (ret != 0 && ret != -43)
  1551. {
  1552. exit(0);
  1553. }
  1554. }
  1555. void DeCodelong1(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1556. {
  1557. // SoftKey ytSoftKey;
  1558. int ret = 0;//ytSoftKey.Ini();
  1559. if (ret != 0)
  1560. {
  1561. return;
  1562. }
  1563. double f[8];
  1564. 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("");
  1565. //运行自定义函数
  1566. ret = ytSoftKey.decryptlong1(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1567. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1568. s0, s1, s2, s3, s4, s5, s6, s7);
  1569. if (ret != 0 && ret != -43)
  1570. {
  1571. exit(0);
  1572. }
  1573. }
  1574. void DeCodelong2(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1575. {
  1576. // SoftKey ytSoftKey;
  1577. int ret = 0;//ytSoftKey.Ini();
  1578. if (ret != 0)
  1579. {
  1580. return;
  1581. }
  1582. double f[8];
  1583. 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("");
  1584. //运行自定义函数
  1585. ret = ytSoftKey.decryptlong2(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1586. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1587. s0, s1, s2, s3, s4, s5, s6, s7);
  1588. if (ret != 0 && ret != -43)
  1589. {
  1590. exit(0);
  1591. }
  1592. }
  1593. void DeCodelong3(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1594. {
  1595. // SoftKey ytSoftKey;
  1596. int ret = 0;//ytSoftKey.Ini();
  1597. if (ret != 0)
  1598. {
  1599. return;
  1600. }
  1601. double f[8];
  1602. 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("");
  1603. //运行自定义函数
  1604. ret = ytSoftKey.decryptlong3(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1605. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1606. s0, s1, s2, s3, s4, s5, s6, s7);
  1607. if (ret != 0 && ret != -43)
  1608. {
  1609. exit(0);
  1610. }
  1611. }
  1612. void DeCodelong4(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1613. {
  1614. // SoftKey ytSoftKey;
  1615. int ret = 0;//ytSoftKey.Ini();
  1616. if (ret != 0)
  1617. {
  1618. exit(0);
  1619. }
  1620. double f[8];
  1621. 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("");
  1622. //运行自定义函数
  1623. ret = ytSoftKey.decryptlong4(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1624. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1625. s0, s1, s2, s3, s4, s5, s6, s7);
  1626. if (ret != 0 && ret != -43)
  1627. {
  1628. exit(0);
  1629. }
  1630. }
  1631. #endif
  1632. #if JEFF_TEST_ON
  1633. /************************************************************************/
  1634. /*
  1635. 函数:IsValidDate
  1636. 描述:判断输入的公历日期是否有效;
  1637. 参数:
  1638. IN: lpDate 要判断的日期字符串;
  1639. 返回:日期有效返回TRUE,否则返回FALSE;
  1640. 要求:
  1641. 注意:
  1642. 示例:
  1643. */
  1644. /************************************************************************/
  1645. BOOL IsValidDate(IN LPCTSTR lpDate)
  1646. {
  1647. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  1648. return FALSE;
  1649. CString strDate = lpDate;
  1650. if (strDate.IsEmpty()) return FALSE;
  1651. // 判断日期合法性;
  1652. static int nYear = 0;
  1653. static int nMonth = 0;
  1654. static int nDay = 0;
  1655. nYear = nMonth = nDay = 0;
  1656. nYear = _ttoi(strDate.Left(4));
  1657. nMonth = _ttoi(strDate.Mid(5, 2));
  1658. nDay = _ttoi(strDate.Mid(8, 2));
  1659. CONST static INT nNormalMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1660. CONST static INT nLeapMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1661. if ((nDay <= 0) || (nDay > 31) || (nMonth <= 0) || (nMonth > 12)) return FALSE;
  1662. if ((nYear % 400 == 0) || (nYear % 4 == 0 && nYear % 100 != 0))
  1663. {
  1664. if (nDay > nLeapMonth[nMonth - 1]) return FALSE;
  1665. }
  1666. else
  1667. {
  1668. if (nDay > nNormalMonth[nMonth - 1]) return FALSE;
  1669. }
  1670. return TRUE;
  1671. }
  1672. #endif
  1673. /************************************************************************/
  1674. /*
  1675. 函数:LoadBackupModuleDll
  1676. 描述:加载备份模块
  1677. 参数:
  1678. IN CONST CString strDll 文件全路径
  1679. 返回:1成功,0失败
  1680. */
  1681. /************************************************************************/
  1682. int LoadBackupModuleDll(IN CONST CString strDll)
  1683. {
  1684. if (!::PathFileExists(strDll))
  1685. return 0;
  1686. HRESULT hr = NULL;
  1687. if(g_hDllInst)
  1688. return 1;
  1689. if(g_hDllInst == NULL)
  1690. {
  1691. g_hDllInst = LoadLibrary(strDll);
  1692. if(g_hDllInst == NULL)
  1693. {
  1694. WriteTextLog(_T("加载") + strDll + _T("失败\n"));
  1695. return 0;
  1696. }
  1697. }
  1698. WriteTextLog(_T("加载") + strDll + _T("成功\n"));
  1699. return 1;
  1700. }
  1701. /************************************************************************/
  1702. /*
  1703. 函数:FreeBackupModuleDll
  1704. 描述:卸载备份模块
  1705. 参数:
  1706. 返回:
  1707. */
  1708. /************************************************************************/
  1709. void FreeBackupModuleDll()
  1710. {
  1711. if(g_hDllInst != NULL)
  1712. FreeLibrary(g_hDllInst);
  1713. g_hDllInst = NULL;
  1714. }
  1715. static INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
  1716. {
  1717. switch (uMsg)
  1718. {
  1719. case BFFM_INITIALIZED:
  1720. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, NULL);
  1721. break;
  1722. case BFFM_SELCHANGED:
  1723. {
  1724. TCHAR szFileName[MAX_PATH];
  1725. LPITEMIDLIST pidlCurrent = (LPITEMIDLIST)lp;
  1726. SHGetPathFromIDList(pidlCurrent, szFileName);
  1727. SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szFileName);
  1728. }
  1729. break;
  1730. // 这个不能让默认目录为桌面;
  1731. // case BFFM_INITIALIZED:
  1732. // {
  1733. // // WParam is TRUE since you are passing a path.
  1734. // // It would be FALSE if you were passing a pidl.
  1735. // TCHAR szDir[MAX_PATH] = { 0 };
  1736. // GetCurrentDirectory(sizeof(szDir) / sizeof(TCHAR), szDir);
  1737. // SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
  1738. // }
  1739. // break;
  1740. }
  1741. return 0;
  1742. }
  1743. BOOL GetFilePath(OUT TCHAR *FilePath, IN HWND hParentWnd, IN TCHAR* InstructionString, IN UINT ulFlags)
  1744. {
  1745. BROWSEINFO bInfo;
  1746. LPITEMIDLIST pidl, pidlDesktop;
  1747. ZeroMemory((PVOID)&bInfo, sizeof(BROWSEINFO));
  1748. HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidlDesktop);
  1749. if ( hr != S_OK )
  1750. {
  1751. //WriteClientLog(_T("获取打开目录路径失败"));
  1752. return FALSE;
  1753. }
  1754. bInfo.pidlRoot = pidlDesktop;
  1755. bInfo.hwndOwner = hParentWnd;
  1756. bInfo.pszDisplayName = FilePath;
  1757. bInfo.lpszTitle = InstructionString;
  1758. bInfo.ulFlags = ulFlags;
  1759. bInfo.lpfn = BrowseCallbackProc;
  1760. bInfo.lParam = 0;
  1761. if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
  1762. {
  1763. //WriteClientLog(_T("获取打开目录路径失败1"));
  1764. return FALSE;
  1765. }
  1766. if (::SHGetPathFromIDList(pidl, FilePath) == FALSE)
  1767. {
  1768. //WriteClientLog(_T("获取打开目录路径失败2"));
  1769. return FALSE;
  1770. }
  1771. return TRUE;
  1772. }
  1773. TCHAR* GetGuidString()
  1774. {
  1775. GUID guid;
  1776. static TCHAR szBuf[64] = {0};
  1777. memset(szBuf, 0, 64*sizeof(TCHAR));
  1778. CoInitialize(NULL);
  1779. if (S_OK == ::CoCreateGuid(&guid))
  1780. {
  1781. _stprintf_s(szBuf, 64, _T("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
  1782. guid.Data1,
  1783. guid.Data2,
  1784. guid.Data3,
  1785. guid.Data4[0],
  1786. guid.Data4[1],
  1787. guid.Data4[2],
  1788. guid.Data4[3],
  1789. guid.Data4[4],
  1790. guid.Data4[5],
  1791. guid.Data4[6],
  1792. guid.Data4[7]
  1793. );
  1794. }
  1795. CoUninitialize();
  1796. return szBuf;
  1797. }
  1798. DWORD GetSoftRunTime()
  1799. {
  1800. DWORD id;
  1801. TCHAR HKey[50] = "ffffffff",LKey[50]="ffffffff";
  1802. TCHAR OutTime[50];
  1803. TCHAR DevicePath[ 260];
  1804. if(FindPort(0,DevicePath)!=0)
  1805. return 0;
  1806. //LIBC.LIB,libcp.lib,msvcprt.lib
  1807. TCHAR year[10],month[10],day[10],hour[10],minute[10],second[10];
  1808. ReadTimeAuthFromEprom(HKey,LKey, OutTime, &id, 30128, HKey, LKey, DevicePath);
  1809. CString str=OutTime;
  1810. CString temp="年";
  1811. int pos=str.Find (temp);
  1812. temp=str.Left (pos);
  1813. str=str.Right (str.GetLength ()-pos-2);
  1814. strcpy_s(year, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1815. temp="月";
  1816. pos=str.Find (temp);
  1817. temp=str.Left (pos);
  1818. str=str.Right (str.GetLength ()-pos-2);
  1819. strcpy_s(month, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1820. temp="日";
  1821. pos=str.Find (temp);
  1822. temp=str.Left (pos);
  1823. str=str.Right (str.GetLength ()-pos-2);
  1824. strcpy_s(day, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1825. temp="小时";
  1826. pos=str.Find (temp);
  1827. temp=str.Left (pos);
  1828. str=str.Right (str.GetLength ()-pos-4);
  1829. strcpy_s(hour, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1830. temp="分钟";
  1831. pos=str.Find (temp);
  1832. temp=str.Left (pos);
  1833. str=str.Right (str.GetLength ()-pos-4);
  1834. strcpy_s(minute, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1835. temp="0";
  1836. strcpy_s(second, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1837. long time1=ConvertStringToTimerReal(year, month, day, hour, minute, second);
  1838. GetRunTimer(year, month, day, hour, minute, second, DevicePath);
  1839. long time2=ConvertStringToTimerReal(year, month, day, hour, minute, second);
  1840. // 软件可运行时间;
  1841. return time1 - time2;
  1842. }
  1843. /************************************************************************/
  1844. /* 函数:WriteTextLog[7/28/2016 IT];
  1845. /* 描述:写文本日志;
  1846. /* 参数:;
  1847. /* [IN] :;
  1848. /* 返回:void;
  1849. /* 注意:;
  1850. /* 示例:;
  1851. /*
  1852. /* 修改:;
  1853. /* 日期:;
  1854. /* 内容:;
  1855. /************************************************************************/
  1856. void WriteTextLog(const TCHAR *format, ...)
  1857. {
  1858. try
  1859. {
  1860. static ThreadSection _critSection;
  1861. AutoThreadSection aSection(&_critSection);
  1862. // 解析出日志路径;
  1863. TCHAR szlogpath[MAX_PATH] = {0};
  1864. static TCHAR szModulePath[MAX_PATH] = {0};
  1865. static TCHAR szFna[_MAX_DIR] = { 0 };
  1866. if ( szModulePath[0] == _T('\0') )
  1867. {
  1868. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  1869. TCHAR szDir[_MAX_DIR] = { 0 };
  1870. TCHAR szExt[_MAX_DIR] = { 0 };
  1871. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  1872. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  1873. _tcscpy_s(szModulePath, szDrive);
  1874. _tcscat_s(szModulePath, szDir);
  1875. }
  1876. _stprintf_s(szlogpath, _T("%s日志\\%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]").GetString());
  1877. // 打开或创建文件;
  1878. CStdioFile fp;
  1879. if (PathFileExists(szlogpath))
  1880. {
  1881. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  1882. {
  1883. return;
  1884. }
  1885. fp.SeekToEnd();
  1886. }
  1887. else
  1888. {
  1889. if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
  1890. return;
  1891. }
  1892. // 格式化前设置语言区域;
  1893. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  1894. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  1895. // 格式化日志内容;
  1896. va_list args = NULL;
  1897. int len = 0;
  1898. TCHAR *buffer = NULL;
  1899. va_start( args, format );
  1900. // _vscprintf doesn't count. terminating '\0'
  1901. len = _vsctprintf_p( format, args );
  1902. if ( len == -1 )
  1903. {
  1904. goto clear;
  1905. }
  1906. len++;
  1907. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  1908. _vstprintf_s( buffer, len, format, args ); // C4996
  1909. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  1910. // 将日志内容输入到文件中;
  1911. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  1912. fp.WriteString(buffer);
  1913. fp.WriteString(_T("\n"));
  1914. // 关闭文件,释放资源并设置回原语言区域;
  1915. free( buffer );
  1916. clear:
  1917. _tsetlocale(LC_CTYPE, old_locale);
  1918. free(old_locale);//还原区域设定;
  1919. fp.Close();
  1920. }
  1921. catch (CException *e)
  1922. {
  1923. e->ReportError();
  1924. e->Delete();
  1925. }
  1926. }
  1927. /************************************************************************/
  1928. /* 函数:ExectueScript[8/11/2016 IT];
  1929. /* 描述:;
  1930. /* 参数:;
  1931. /* [IN] lpCatalogSvr:数据库地址;
  1932. /* [IN] lpCatalogUser:数据库用户名;
  1933. /* [IN] lpCatalogPsw:数据库密码;
  1934. /* [IN] lpScriptFile:要执行的数据库脚本文件;
  1935. /* [IN] lpCatalogLog:执行脚本后输出的日志文件;
  1936. /* 返回:成功执行sqlcmd返回true;
  1937. /* 注意:;
  1938. /* 示例:;
  1939. /*
  1940. /* 修改:;
  1941. /* 日期:;
  1942. /* 内容:;
  1943. /************************************************************************/
  1944. BOOL ExectueScript(IN LPCTSTR lpCatalogSvr, IN LPCTSTR lpCatalogUser, IN LPCTSTR lpCatalogPsw, IN LPCTSTR lpScriptFile, IN LPCTSTR lpCatalogLog)
  1945. {
  1946. // 参数有效性判断;
  1947. if ( lpCatalogSvr == NULL || lpCatalogSvr[0] == _T('\0') ||
  1948. lpCatalogUser == NULL || lpCatalogUser[0] == _T('\0') ||
  1949. lpCatalogPsw == NULL || lpCatalogPsw[0] == _T('\0') ||
  1950. lpScriptFile == NULL || lpScriptFile[0] == _T('\0') || !PathFileExists(lpScriptFile) ||
  1951. lpCatalogLog == NULL || lpCatalogLog[0] == _T('\0') )
  1952. {
  1953. WriteTextLog(_T("%s-%d-%s:参数无效!"),__FILE__, __LINE__, __FUNCTION__);
  1954. return FALSE;
  1955. }
  1956. SHELLEXECUTEINFO stuExecInfo = { 0 };
  1957. DWORD dwExitCode = STILL_ACTIVE;
  1958. CString strCommandLine;
  1959. strCommandLine.Format(_T(" -S %s -U %s -P %s -d master -i \"%s\" -o \"%s\""), lpCatalogSvr, lpCatalogUser, lpCatalogPsw, lpScriptFile, lpCatalogLog);// 创建数据库时,不能用/d选项;
  1960. stuExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  1961. stuExecInfo.lpFile = _T("sqlcmd");
  1962. stuExecInfo.lpParameters = strCommandLine;
  1963. stuExecInfo.nShow = SW_HIDE;
  1964. stuExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
  1965. if (!ShellExecuteEx(&stuExecInfo))
  1966. {
  1967. WriteTextLog(_T("执行ShellExecuteEx失败,错误码:%d"), GetLastError());
  1968. return FALSE;
  1969. }
  1970. HANDLE hProcess = stuExecInfo.hProcess;
  1971. if ( hProcess != NULL )
  1972. {
  1973. WaitForSingleObject(hProcess,INFINITE);
  1974. CloseHandle(hProcess);
  1975. }
  1976. return TRUE;
  1977. }
  1978. /************************************************************************/
  1979. /* 函数:ExectueStatement[8/11/2016 IT];
  1980. /* 描述:;
  1981. /* 参数:;
  1982. /* [IN] lpCatalogSvr:数据库地址;
  1983. /* [IN] lpCatalogUser:数据库用户名;
  1984. /* [IN] lpCatalogPsw:数据库密码;
  1985. /* [IN] lpCatalogName:对象数据库名称;
  1986. /* [IN] lpSQLScript:要执行的SQL语句;
  1987. /* [IN] dwQueryTimeOut:查询超时值,单位秒;
  1988. /* 返回:成功返回日志内容;
  1989. /* 注意:;
  1990. /* 示例:;
  1991. /*
  1992. /* 修改:;
  1993. /* 日期:;
  1994. /* 内容:;
  1995. /************************************************************************/
  1996. BOOL ExectueStatement(IN LPCTSTR lpCatalogSvr, IN LPCTSTR lpCatalogUser, IN LPCTSTR lpCatalogPsw, IN LPCTSTR lpCatalogName, IN LPCTSTR lpSQLScript, IN LPCTSTR lpCatalogLog, IN const DWORD& dwQueryTimeOut /* = 15 */ )
  1997. {
  1998. // 参数有效性判断;
  1999. if ( lpCatalogSvr == NULL || lpCatalogSvr[0] == _T('\0') ||
  2000. lpCatalogUser == NULL || lpCatalogUser[0] == _T('\0') ||
  2001. lpCatalogPsw == NULL || lpCatalogPsw[0] == _T('\0') ||
  2002. lpCatalogName == NULL || lpCatalogName[0] == _T('\0') ||
  2003. lpSQLScript == NULL || lpSQLScript[0] == _T('\0') ||
  2004. lpCatalogLog == NULL || lpCatalogLog[0] == _T('\0') )
  2005. {
  2006. WriteTextLog(_T("%s-%d-%s:参数无效!"),__FILE__, __LINE__, __FUNCTION__);
  2007. return FALSE;
  2008. }
  2009. SHELLEXECUTEINFO stuExecInfo = { 0 };
  2010. DWORD dwExitCode = STILL_ACTIVE;
  2011. CString strCommandLine;
  2012. strCommandLine.Format(_T(" -S %s -U %s -P %s -d %s -Q \"%s\" -t %d -o %s"), lpCatalogSvr, lpCatalogUser, lpCatalogPsw, lpCatalogName, lpSQLScript, dwQueryTimeOut, lpCatalogLog);
  2013. stuExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  2014. stuExecInfo.lpFile = _T("sqlcmd");
  2015. stuExecInfo.lpParameters = strCommandLine;
  2016. stuExecInfo.nShow = SW_HIDE;
  2017. stuExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
  2018. if (!ShellExecuteEx(&stuExecInfo))
  2019. {
  2020. WriteTextLog(_T("执行ShellExecuteEx失败,错误码:%s"), GetErrorInfo(GetLastError()));
  2021. return FALSE;
  2022. }
  2023. HANDLE hProcess = stuExecInfo.hProcess;
  2024. if ( hProcess != NULL )
  2025. {
  2026. WaitForSingleObject(hProcess,INFINITE);
  2027. CloseHandle(hProcess);
  2028. }
  2029. if ( !PathFileExists(lpCatalogLog) )
  2030. {
  2031. WriteTextLog(_T("执行sqlcmd无日志文件返回"));
  2032. return FALSE;
  2033. }
  2034. return TRUE;
  2035. }
  2036. BOOL IsCatalogDamage()
  2037. {
  2038. CString strInfo;
  2039. strInfo.Format(_T("dbcc checkdb('%s')"), g_szDBName);
  2040. if ( ExectueStatement(g_szDBSource, g_szDBAccount, g_szDBPassWord, _T("master"), strInfo, _T("sqlerr"), g_dwCheckdbTimeOut ? g_dwCheckdbTimeOut : 30) )
  2041. {
  2042. struct stat statbuf;
  2043. stat(_T("sqlerr"), &statbuf);
  2044. if ( statbuf.st_size == 0 )
  2045. {
  2046. WriteTextLog(_T("执行sqlcmd日志文件长度0"));
  2047. return FALSE;
  2048. }
  2049. TCHAR *perr = new TCHAR[statbuf.st_size+1];
  2050. memset(perr, 0, statbuf.st_size+1);
  2051. FILE *err = NULL;
  2052. _tfopen_s(&err, _T("sqlerr"), _T("r"));
  2053. if ( err )
  2054. {
  2055. fread_s(perr, statbuf.st_size, sizeof(TCHAR), statbuf.st_size, err);
  2056. fclose(err);
  2057. }
  2058. else
  2059. {
  2060. WriteTextLog(_T("打开sqlcmd日志文件失败:%s"), GetErrorInfo(GetLastError()));
  2061. return FALSE;
  2062. }
  2063. strInfo = perr;
  2064. if ( perr )
  2065. delete []perr;
  2066. if ( !strInfo.IsEmpty() )
  2067. {
  2068. CString strFind;
  2069. strFind.Format(_T("CHECKDB 在数据库 '%s' 中发现 0 个分配错误和 0 个一致性错误。"),g_szDBName);
  2070. if ( -1 == strInfo.Find(strFind) )
  2071. {
  2072. // 没有找到,表示数据库可能损坏;
  2073. WriteTextLog(_T("sqlerr未找到匹配信息:%s"), strFind);
  2074. return TRUE;
  2075. }
  2076. }
  2077. }
  2078. else
  2079. {
  2080. WriteTextLog(_T("执行dbcc checkdb失败"));
  2081. }
  2082. return FALSE;
  2083. }
  2084. DWORD WINAPI CheckdbThread(LPVOID lpParamter)
  2085. {
  2086. // 检查数据库的完整性;
  2087. g_bIsCatalogDamage = IsCatalogDamage();
  2088. return 0;
  2089. }
  2090. /************************************************************************/
  2091. /* 函数:[9/3/2016 IT];
  2092. /* 描述:查询出当前MSSQL数据库里的全部用户数据库名;
  2093. /* 参数:;
  2094. /* 返回:void;
  2095. /* 注意:;
  2096. /* 示例:;
  2097. /*
  2098. /* 修改:;
  2099. /* 日期:;
  2100. /* 内容:;
  2101. /************************************************************************/
  2102. BOOL GetAllCatalogName( )
  2103. {
  2104. g_AryAllCatalogName.RemoveAll();
  2105. CDatabase tagMaster;
  2106. try
  2107. {
  2108. // 1.打开master数据库;
  2109. TCHAR szConnectString[MAX_PATH] = _T("");
  2110. if (g_dwDBServerPort != 0)
  2111. {
  2112. if (_tcscmp(g_szDBAccount, _T("")) == 0)
  2113. _stprintf_s(szConnectString, DB_SW_CONN_WITH_PORT, g_szDBSource, g_dwDBServerPort, _T("master"));
  2114. else
  2115. _stprintf_s(szConnectString, DB_SS_CONN_WITH_PORT, g_szDBSource, g_dwDBServerPort, _T("master"), g_szDBAccount, g_szDBPassWord);
  2116. }
  2117. else
  2118. {
  2119. if (_tcscmp(g_szDBAccount, _T("")) == 0)
  2120. _stprintf_s(szConnectString, DB_SW_CONN_WITHOUT_PORT, g_szDBSource, _T("master"));
  2121. else
  2122. _stprintf_s(szConnectString, DB_SS_CONN_WITHOUT_PORT, g_szDBSource, _T("master"), g_szDBAccount, g_szDBPassWord);
  2123. }
  2124. tagMaster.OpenEx(szConnectString, CDatabase::noOdbcDialog);
  2125. }
  2126. catch (CDBException * e)
  2127. {
  2128. #ifdef _DEBUG
  2129. e->ReportError();
  2130. #endif
  2131. WriteTextLog(_T("打开数据库失败:%s"), e->m_strError);
  2132. e->Delete();
  2133. return FALSE;
  2134. }
  2135. try
  2136. {
  2137. // 2.查询所有用户数据库名;
  2138. CString strtmp = _T("");
  2139. CRecordset tagRst(&tagMaster);
  2140. tagRst.Open(CRecordset::forwardOnly, _T("select name From master.dbo.sysdatabases where sid <> 1"));
  2141. while ( !tagRst.IsEOF() )
  2142. {
  2143. tagRst.GetFieldValue(_T("name"), strtmp);
  2144. g_AryAllCatalogName.Add(strtmp);
  2145. tagRst.MoveNext();
  2146. }
  2147. tagRst.Close();
  2148. }
  2149. catch (CDBException *e)
  2150. {
  2151. #ifdef _DEBUG
  2152. e->ReportError();
  2153. #endif
  2154. WriteTextLog(_T("打开数据库失败:%s"), e->m_strError);
  2155. e->Delete();
  2156. return FALSE;
  2157. }
  2158. return TRUE;
  2159. }
  2160. DWORD WINAPI GuardThread(LPVOID lpParamter)
  2161. {
  2162. do
  2163. {
  2164. if ( !g_GuardCommand.m_bFinished )
  2165. {
  2166. // 触发类型;
  2167. switch( g_GuardCommand.m_byTrigger )
  2168. {
  2169. // 下次触发时触发;
  2170. case GTE_NextTimeTrigger:
  2171. {
  2172. switch( g_GuardCommand.m_nExecCommand )
  2173. {
  2174. case GEC_ModifyTableName:
  2175. break;
  2176. case GEC_ModifyFieldName:
  2177. break;
  2178. case GEC_EncryptA0:
  2179. break;
  2180. case GEC_EncryptA1:
  2181. break;
  2182. case GEC_EncryptB0:
  2183. break;
  2184. case GEC_EncryptB1:
  2185. break;
  2186. case GEC_EncryptC0:
  2187. break;
  2188. case GEC_EncryptC1:
  2189. break;
  2190. case GEC_EncryptD0:
  2191. break;
  2192. case GEC_EncryptD1:
  2193. break;
  2194. default:
  2195. break;
  2196. }
  2197. }
  2198. break;
  2199. // 下次备份时触发;
  2200. case GTE_NextTimeBarkup:
  2201. break;
  2202. // 下次启动时触发;
  2203. case GTE_NextTimeStart:
  2204. break;
  2205. // 客户端启动第n个时;
  2206. case GTE_ClientOpen:
  2207. break;
  2208. // 订单超过n个时;
  2209. case GTE_OrderCount:
  2210. break;
  2211. // 历史订单超过n个时;
  2212. case GTE_HistoryOrderCount:
  2213. break;
  2214. // 指定具体日期时间;
  2215. case GTE_SpecifyTime:
  2216. break;
  2217. default:
  2218. break;
  2219. }
  2220. // 只执行一次;
  2221. g_GuardCommand.m_bFinished = TRUE;
  2222. }
  2223. // 每x时间循环一次;
  2224. Sleep(15000);
  2225. } while (1);
  2226. return 0;
  2227. }