Global.cpp 60 KB

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