Global.cpp 62 KB

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