Global.cpp 59 KB

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