Global.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  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. // start+------------------------------------------------------------------
  41. // Jeff:以下为原本程序全局变量;
  42. #if THE_PRIMARY_GLOBAL_VARIABLE
  43. int g_TCPport = 5678; // Jeff.通信端口;
  44. BOOL g_bReg = 0;
  45. CString g_strdbpsw; // 数据库账号、密码;
  46. CString g_strdbServrename; // 数据库服务名(地址)8
  47. CString g_bakinfo;
  48. CString g_bakphoto;
  49. CString g_mainpath;
  50. SoftKey ytSoftKey;
  51. CString g_localname; // 本地计算机名;
  52. //CDatabase g_db; // 名为"db"的数据库,本年度使用的数据库;
  53. CArray<int, int> g_nYearArray; // 年份(用于命名历史数据库)
  54. //CArray<CDatabase*,CDatabase*>g_dbArray; // 历史数据库;
  55. CArray<CDatabase*, CDatabase*>g_dbHistoryArray; // 历史数据库;
  56. CArray<CDatabase*, CDatabase*>g_branchdbarray;
  57. CArray<CDatabase*, CDatabase*>g_branchdb2array;//除历史年度的分店DB
  58. CStringArray g_branchnamearray;
  59. CStringArray g_branchname2array;
  60. CStringArray g_branchiparray;
  61. CStringArray g_branchdomainarray;
  62. CDatabase *g_pMaindb = NULL;
  63. int g_conncount = -1; // Jeff.加密狗中的座席版数量;
  64. CString g_sdomain;
  65. CStringArray g_conniparray;
  66. CStringArray g_connidarray;
  67. BOOL g_bCheckAll = 0;
  68. BYTE *g_pSkinData = NULL;
  69. DWORD g_SkinLeng;
  70. BOOL g_bAutoRun = 0;
  71. CStringArray g_filedatearray;
  72. CStringArray g_filedatearraynew;
  73. CString g_bakdate;
  74. CString g_localip;
  75. BOOL g_bSteal = 0;
  76. CString g_bakserver1;
  77. CString g_bakserver2;
  78. CString g_bakserver3;
  79. CString g_bakserver4;
  80. DWORD g_tisks = 0;
  81. #if 0
  82. int g_nBakDays = 10; // 备份几天前的时间设置(单位:天数)
  83. int g_nBackupModel = 1;
  84. #endif //#if 0
  85. int g_bAutoCheckSvc = 1; // (1是/0否)自动检查服务(次/小时)
  86. HINSTANCE g_hDllInst = NULL;
  87. HINSTANCE g_Instance = NULL;
  88. #endif
  89. // endof+------------------------------------------------------------------
  90. /************************************************************************/
  91. /* 函数:InitSocketLibrary[2/29/2016 IT];
  92. /* 描述:Initialize Winsock library. This function calls WSAStartup;
  93. /* 参数:;
  94. /* [IN] wVersion:Winsock version use MAKEWORD macro if possible (e.g.: MAKEWORD(2,2));
  95. /* 返回:return true if successful;
  96. /* 注意:关联ReleaseSocketLibrary;
  97. /* 示例:;
  98. /*
  99. /* 修改:;
  100. /* 日期:;
  101. /* 内容:;
  102. /************************************************************************/
  103. bool InitSocketLibrary(IN WORD wVersion)
  104. {
  105. WSADATA WSAData = { 0 };
  106. return ( 0 == WSAStartup( wVersion, &WSAData ) );
  107. }
  108. /************************************************************************/
  109. /* 函数:ReleaseSocketLibrary[2/29/2016 IT];
  110. /* 描述:Release Winsock library;
  111. /* 参数:;
  112. /* 返回:return true if successful;
  113. /* 注意:关联InitSocketLibrary;
  114. /* 示例:;
  115. /*
  116. /* 修改:;
  117. /* 日期:;
  118. /* 内容:;
  119. /************************************************************************/
  120. bool ReleaseSocketLibrary()
  121. {
  122. return ( 0 == WSACleanup() );
  123. }
  124. /************************************************************************/
  125. /* 函数:[5/20/2016 IT];
  126. /* 描述:;
  127. /* 参数:;
  128. /* [IN] :;
  129. /* [OUT] :;
  130. /* [IN/OUT] :;
  131. /* 返回:void;
  132. /* 注意:;
  133. /* 示例:;
  134. /*
  135. /* 修改:;
  136. /* 日期:;
  137. /* 内容:;
  138. /************************************************************************/
  139. int GetIniInfo(const char *szPath, const char *szIniName)
  140. {
  141. TCHAR szDrive[_MAX_DRIVE];
  142. TCHAR szDir[_MAX_DIR];
  143. ::GetModuleFileName(NULL, g_ModulePath, sizeof(g_ModulePath) / sizeof(TCHAR));
  144. sprintf(g_ModuleFileName, "%s", g_ModulePath);
  145. _tsplitpath(g_ModulePath, szDrive, szDir, NULL, NULL);
  146. _tcscpy(g_ModulePath, szDrive);
  147. _tcscat(g_ModulePath, szDir);
  148. // -----------------------------------------------------//
  149. CHAR szFile[MAX_PATH + 1] = "";
  150. if (szPath != NULL && szIniName != NULL)
  151. wsprintf(szFile, "%s\\%s", szPath, szIniName);
  152. else
  153. wsprintf(szFile, "%s\\ServiceInfo.ini", g_ModulePath);
  154. TRACE("szFile =%s\n", szFile);
  155. HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  156. if (ERROR_FILE_NOT_FOUND == GetLastError())
  157. {
  158. return -1;
  159. }
  160. CloseHandle(hFile);
  161. hFile = NULL;
  162. // 获取服务器端信息;
  163. GetPrivateProfileString("DatabaseInfo", "dbSource", "", g_szDBSource, MAX_PATH, szFile);
  164. g_dwDBServerPort = GetPrivateProfileInt("DatabaseInfo", "dbServerPort", 0, szFile);
  165. GetPrivateProfileString("DatabaseInfo", "dbAccount", "", g_szDBAccount, MAX_PATH, szFile);
  166. GetPrivateProfileString("DatabaseInfo", "dbPassWord", "", g_szDBPassWord, MAX_PATH, szFile);
  167. GetPrivateProfileString("DatabaseInfo", "dbName", "", g_szDBName, MAX_PATH, szFile);
  168. g_dwDBPoolMaxCount = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolMaxObj"), 50, szFile);
  169. g_dwDBPoolDef = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolDef"), 3, szFile);
  170. #if USE_ADO_OPT_DB
  171. GetPrivateProfileString("DatabaseInfo", "LogdbName", "", g_szLogDBName, MAX_PATH, szFile);
  172. #endif//#if USE_ADO_OPT_DB
  173. g_dwCSPort = GetPrivateProfileInt("NetWorkInfo", "TCPChatPort", 5678, szFile);
  174. if (g_dwDBServerPort != 0)
  175. {
  176. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  177. sprintf(g_szConnectString, DB_SW_CONN_WITH_PORT, g_szDBSource, g_dwDBServerPort,g_szDBName);
  178. else
  179. sprintf(g_szConnectString, DB_SS_CONN_WITH_PORT,g_szDBSource, g_dwDBServerPort, g_szDBName, g_szDBAccount, g_szDBPassWord);
  180. }
  181. else
  182. {
  183. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  184. sprintf(g_szConnectString, DB_SW_CONN_WITHOUT_PORT, g_szDBSource, g_szDBName);
  185. else
  186. sprintf(g_szConnectString, DB_SS_CONN_WITHOUT_PORT,g_szDBSource, g_szDBName, g_szDBAccount, g_szDBPassWord);
  187. }
  188. #if 0
  189. g_nBakDays = GetPrivateProfileInt("Back", "uBakDays", 10, szFile);
  190. g_nBackupModel = GetPrivateProfileInt("Back", "uBackupModel", 1, szFile);
  191. #endif //#if 0
  192. g_bAutoCheckSvc = GetPrivateProfileInt("Back", "AutoCheckSvc", 1, szFile);
  193. // 局域网共享账号和密码;
  194. GetPrivateProfileString("NetWorkInfo", "ShareAccount", "", g_szShareAccount, MAX_PATH, szFile);
  195. GetPrivateProfileString("NetWorkInfo", "SharePasswd", "", g_szSharePasswd, MAX_PATH, szFile);
  196. // NemoInfo;
  197. GetPrivateProfileString("NemoInfo", "N8ViewSvr", "", g_szN8ViewSvrIP, MAX_PATH, szFile);
  198. GetPrivateProfileString("NemoInfo", "N8ViewUserDir", "", g_szN8ViewUserDir, MAX_PATH, szFile);
  199. GetPrivateProfileString("NemoInfo", "N8ViewStsDir", "", g_szN8ViewStsDir, MAX_PATH, szFile);
  200. GetPrivateProfileString("NemoInfo", "N8ViewProductDir", "", g_szN8ViewProductDir, MAX_PATH, szFile);
  201. GetPrivateProfileString("NemoInfo", "N8ViewShareDir", "", g_szN8ViewShareDir, MAX_PATH, szFile);
  202. GetPrivateProfileString("NemoInfo", "N8ViewShareName", "", g_szN8ViewShareName, MAX_PATH, szFile);
  203. //////////////////////////////////////////////////////////////////////////
  204. // SystemInfo;
  205. g_bExportBarCode = GetPrivateProfileInt(_T("SystemInfo"), _T("ExportBarCode"), 0, szFile);
  206. g_bEnableClound = GetPrivateProfileInt(_T("SystemInfo"), _T("EnableClound"), 0, szFile);
  207. // 云盘账号;
  208. GetPrivateProfileString(_T("SystemInfo"), _T("CloundDiskAccount"), _T(""), g_szCloundDiskAccount, MAX_PATH, szFile);
  209. // 云盘密码;
  210. GetPrivateProfileString(_T("SystemInfo"), _T("CloundDiskPsw"), _T(""), g_szCloundDiskPsw, MAX_PATH, szFile);
  211. // 解密云盘密码;
  212. CString strResult;
  213. des_crypt((CString)g_szCloundDiskPsw, strResult, DES_DECRYPT);
  214. _stprintf_s(g_szCloundDiskPsw, MAX_PATH, _T("%s"), strResult);
  215. // 是否检测数据库;
  216. g_bCheckdb = GetPrivateProfileInt(_T("SystemInfo"), _T("Checkdb"), 1, szFile);
  217. // 检测数据库的超时值;
  218. g_dwCheckdbTimeOut = GetPrivateProfileInt(_T("SystemInfo"), _T("CheckdbTimeOut"), 30, szFile);
  219. if ( !g_bCheckdb )
  220. {
  221. g_bIsCatalogDamage = TRUE;
  222. }
  223. else
  224. {
  225. g_dwCheckdbTimeOut = g_dwCheckdbTimeOut ? g_dwCheckdbTimeOut : 30;
  226. WritePrivateProfileString(_T("SystemInfo"), _T("CheckdbTimeOut"), _T("30"), szFile);
  227. }
  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. sprintf(szConnectString,DB_SW_CONN_WITH_PORT, g_szDBSource,g_dwDBServerPort, _T("master"), g_szDBName);
  693. else
  694. sprintf(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. sprintf(szConnectString,DB_SW_CONN_WITHOUT_PORT, g_szDBSource, _T("master"), g_szDBName);
  700. else
  701. sprintf(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. sprintf(szNewFileName, _T("%s"), lpNewFileName);
  975. _tsplitpath(szNewFileName, szDrive, szDir, szFna, szExt);
  976. int nIndex = 1;
  977. while (PathFileExists(szNewFileName))
  978. {
  979. sprintf(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. //--------------------------------------------------------------------------------
  1069. // Jeff add 2014.06.23;
  1070. // 函数:ErrorExit
  1071. // 描述:
  1072. // 参数:
  1073. // lpszFunction:函数名;
  1074. // dwError:错误码;
  1075. //
  1076. //--------------------------------------------------------------------------------
  1077. void ErrorExit(CString strDescription, const DWORD &dwError)
  1078. {
  1079. #if 1
  1080. LPVOID lpMsgBuf;
  1081. BOOL fOk = FormatMessage(
  1082. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1083. FORMAT_MESSAGE_FROM_SYSTEM |
  1084. FORMAT_MESSAGE_IGNORE_INSERTS,
  1085. NULL,
  1086. dwError,
  1087. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  1088. (LPTSTR)&lpMsgBuf,
  1089. 0, NULL);
  1090. if (!fOk)
  1091. {
  1092. // Is it a network-related error?
  1093. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1094. if (hDll != NULL)
  1095. {
  1096. FormatMessage(
  1097. FORMAT_MESSAGE_FROM_HMODULE |
  1098. FORMAT_MESSAGE_FROM_SYSTEM |
  1099. FORMAT_MESSAGE_IGNORE_INSERTS,
  1100. hDll,
  1101. dwError,
  1102. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1103. (LPTSTR)&lpMsgBuf,
  1104. 0,
  1105. NULL);
  1106. FreeLibrary(hDll);
  1107. }
  1108. }
  1109. if (lpMsgBuf != NULL)
  1110. {
  1111. WriteTextLog("%s.错误码=%d,Windows描述:%s", strDescription, dwError, (LPCTSTR)LocalLock(lpMsgBuf));
  1112. LocalFree(lpMsgBuf);
  1113. }
  1114. else
  1115. {
  1116. WriteTextLog(strDescription);
  1117. }
  1118. #else
  1119. HLOCAL hlocal = NULL; // Buffer that gets the error message string
  1120. // Get the error code's textual description
  1121. BOOL fOk = FormatMessage(
  1122. FORMAT_MESSAGE_FROM_SYSTEM |
  1123. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1124. FORMAT_MESSAGE_IGNORE_INSERTS,
  1125. NULL,
  1126. dwError,
  1127. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1128. (PTSTR)&hlocal,
  1129. 0,
  1130. NULL);
  1131. if (!fOk)
  1132. {
  1133. // Is it a network-related error?
  1134. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1135. if (hDll != NULL)
  1136. {
  1137. FormatMessage(
  1138. FORMAT_MESSAGE_FROM_HMODULE |
  1139. FORMAT_MESSAGE_FROM_SYSTEM |
  1140. FORMAT_MESSAGE_IGNORE_INSERTS,
  1141. hDll,
  1142. dwError,
  1143. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1144. (PTSTR)&hlocal,
  1145. 0,
  1146. NULL);
  1147. FreeLibrary(hDll);
  1148. }
  1149. }
  1150. if (hlocal != NULL)
  1151. {
  1152. CString strDisplay;
  1153. strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
  1154. WriteTextLog(strDisplay);
  1155. LocalFree(hlocal);
  1156. }
  1157. else
  1158. {
  1159. WriteTextLog("Error number not found.");
  1160. }
  1161. #endif
  1162. }
  1163. void MyGetIPByName(CString &name)
  1164. {
  1165. CString strIP = name;
  1166. name.Empty();
  1167. DWORD dwServerIP = 0;
  1168. HOSTENT *host = gethostbyname(strIP);
  1169. struct in_addr addr;
  1170. if (host != NULL)
  1171. {
  1172. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1173. {
  1174. memset(&addr, 0, sizeof(addr));
  1175. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1176. dwServerIP = ntohl(addr.S_un.S_addr);
  1177. }
  1178. BYTE* pIP = (BYTE*)&dwServerIP;
  1179. name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1180. }
  1181. }
  1182. void EncryptFile3(CString path)
  1183. {
  1184. CFile fp;
  1185. if (fp.Open(path, CFile::modeRead) == 0)return;
  1186. DWORD leng = fp.GetLength();
  1187. BYTE *pData = new BYTE[leng];
  1188. fp.Read(pData, leng);
  1189. fp.Close();
  1190. EncryptFile2(pData, leng, "z9871234abcd");
  1191. fp.Open(path, CFile::modeWrite);
  1192. fp.Write(pData, leng);
  1193. fp.Close();
  1194. delete[]pData;
  1195. }
  1196. int FindArray(CStringArray *pArray, CString Str)
  1197. {
  1198. for (int i = 0; i < pArray->GetSize(); i++)
  1199. {
  1200. if (pArray->ElementAt(i) == Str)
  1201. return i;
  1202. }
  1203. return -1;
  1204. }
  1205. BOOL isInnerIP(DWORD a_ip)
  1206. {
  1207. BOOL bValid = 0;
  1208. if ((a_ip >> 24 == 0xa) || (a_ip >> 16 == 0xc0a8) || (a_ip >> 22 == 0x2b0))
  1209. {
  1210. bValid = 1;
  1211. }
  1212. return bValid;
  1213. }
  1214. BOOL CompareIP(CString name1, CString name2)
  1215. {
  1216. CStringArray g_serverarray1;
  1217. CStringArray g_serverarray2;
  1218. CString strIP = name1;
  1219. DWORD dwServerIP = 0;
  1220. HOSTENT *host = gethostbyname(strIP);
  1221. struct in_addr addr;
  1222. if (host != NULL)
  1223. {
  1224. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1225. {
  1226. memset(&addr, 0, sizeof(addr));
  1227. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1228. dwServerIP = ntohl(addr.S_un.S_addr);
  1229. BYTE* pIP = (BYTE*)&dwServerIP;
  1230. strIP.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1231. g_serverarray1.Add(strIP);
  1232. }
  1233. }
  1234. while (g_serverarray1.GetSize() > 1)
  1235. {
  1236. CString name;
  1237. BOOL bFind = 0;
  1238. for (int i = 0; i < g_serverarray1.GetSize(); i++)
  1239. {
  1240. DWORD dwServerIP = inet_addr(g_serverarray1.ElementAt(i));
  1241. dwServerIP = htonl(dwServerIP);//加了才正确
  1242. if (isInnerIP(dwServerIP) == 0)
  1243. {
  1244. g_serverarray1.RemoveAt(i);
  1245. bFind = 1;
  1246. break;
  1247. }
  1248. }
  1249. if (bFind == 0)
  1250. {
  1251. break;
  1252. }
  1253. }
  1254. //////////
  1255. {
  1256. CString strIP = name2;
  1257. DWORD dwServerIP = 0;
  1258. HOSTENT *host = gethostbyname(strIP);
  1259. struct in_addr addr;
  1260. if (host != NULL)
  1261. {
  1262. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  1263. {
  1264. memset(&addr, 0, sizeof(addr));
  1265. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  1266. dwServerIP = ntohl(addr.S_un.S_addr);
  1267. BYTE* pIP = (BYTE*)&dwServerIP;
  1268. strIP.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  1269. g_serverarray2.Add(strIP);
  1270. }
  1271. }
  1272. while (g_serverarray2.GetSize() > 1)
  1273. {
  1274. CString name;
  1275. BOOL bFind = 0;
  1276. for (int i = 0; i < g_serverarray2.GetSize(); i++)
  1277. {
  1278. DWORD dwServerIP = inet_addr(g_serverarray2.ElementAt(i));
  1279. dwServerIP = htonl(dwServerIP);//加了才正确
  1280. if (isInnerIP(dwServerIP) == 0)
  1281. {
  1282. g_serverarray2.RemoveAt(i);
  1283. bFind = 1;
  1284. break;
  1285. }
  1286. }
  1287. if (bFind == 0)
  1288. {
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. if (g_serverarray1.GetSize() == 0 || g_serverarray2.GetSize() == 0)return 1;
  1294. for (int i = 0; i < g_serverarray1.GetSize(); i++)
  1295. {
  1296. for (int j = 0; j < g_serverarray2.GetSize(); j++)
  1297. {
  1298. if (g_serverarray1.ElementAt(i) == g_serverarray2.ElementAt(j))return 0;
  1299. }
  1300. }
  1301. return 1;
  1302. }
  1303. void DeleteDirectory(CString strDir)
  1304. {
  1305. if ( strDir.IsEmpty() || !PathFileExists(strDir) )
  1306. {
  1307. WriteTextLog(_T("DeleteDirectory:路径无效!"));
  1308. return;
  1309. }
  1310. try
  1311. {
  1312. // 首先删除文件及子文件夹;
  1313. CFileFind ff;
  1314. BOOL bFound = ff.FindFile(strDir + _T("\\*"), 0);
  1315. while (bFound)
  1316. {
  1317. bFound = ff.FindNextFile();
  1318. if (ff.GetFileName() == _T(".") || ff.GetFileName() == _T(".."))
  1319. continue;
  1320. // 去掉文件(夹)只读等属性;
  1321. if ( SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL) )
  1322. {
  1323. if (ff.IsDirectory())
  1324. { // 递归删除子文件夹;
  1325. DeleteDirectory(ff.GetFilePath());
  1326. RemoveDirectory(ff.GetFilePath());
  1327. }
  1328. else
  1329. {
  1330. DeleteFile(ff.GetFilePath()); // Jeff:删除某文件夹下的所有子文件;
  1331. WriteTextLog(ff.GetFilePath());
  1332. }
  1333. }
  1334. else
  1335. {
  1336. WriteTextLog(_T("设置文件[%s]属性失败:%d"), ff.GetFilePath(), GetLastError());
  1337. }
  1338. }
  1339. ff.Close();
  1340. // 然后删除该文件夹;
  1341. RemoveDirectory(strDir);
  1342. }
  1343. catch (...)
  1344. {
  1345. WriteTextLog(_T("出错在函数deldir中"));
  1346. }
  1347. }
  1348. int GetLengthEx(CString str)
  1349. {
  1350. wchar_t wstr[500];
  1351. int k = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, strlen(str), wstr, 500);
  1352. return k;
  1353. }
  1354. CString GetFileSize(CString path)
  1355. {
  1356. CString str = "0";
  1357. try
  1358. {
  1359. CFile fp;
  1360. fp.Open(path, CFile::modeRead);
  1361. str.Format("%d", fp.GetLength());
  1362. fp.Close();
  1363. }
  1364. catch (...)
  1365. {
  1366. }
  1367. return str;
  1368. }
  1369. void SortArray(CStringArray &datearray)
  1370. {
  1371. try
  1372. {
  1373. CString strtemp;
  1374. float ftemp;
  1375. int last = datearray.GetSize() - 1;
  1376. bool sorted = true;
  1377. do {
  1378. sorted = true;
  1379. for (int i = 0; i < last; i++)
  1380. {
  1381. if (datearray[i] < datearray[i + 1])
  1382. {
  1383. strtemp = datearray[i];
  1384. datearray[i] = datearray[i + 1];
  1385. datearray[i + 1] = strtemp;
  1386. sorted = false;
  1387. }
  1388. }
  1389. last--;
  1390. } while (!sorted);
  1391. }
  1392. catch (...)
  1393. {
  1394. WriteTextLog("SortArray出错");
  1395. }
  1396. }
  1397. int CheckPhoneType(CString phoneno)//移动:0 联通:1 电信:2 小灵通:3 未知:-1
  1398. {
  1399. if (phoneno.IsEmpty())return -1;
  1400. int i = 0;
  1401. for (i = 0; i < phoneno.GetLength(); i++)
  1402. {
  1403. if (phoneno.GetAt(i) < '0' || phoneno.GetAt(i) > '9')return -1;
  1404. }
  1405. if (phoneno.GetAt(0) == '1')
  1406. {
  1407. if (phoneno.GetLength() != 11)
  1408. return -1;
  1409. int mobile[] = { 139, 138, 137, 136, 135, 134, 159, 158, 152, 151, 150, 157, 188, 187, 144, 182, 147, 183, 184 };
  1410. int unicom[] = { 130, 131, 132, 155, 156, 186, 185 };
  1411. int telecom[] = { 133, 153, 189, 180, 181 };
  1412. for (i = 0; i < sizeof(mobile) / sizeof(int); i++)
  1413. {
  1414. if (mobile[i] == atoi(phoneno.Left(3)))
  1415. {
  1416. return 0;
  1417. }
  1418. }
  1419. for (i = 0; i < sizeof(unicom) / sizeof(int); i++)
  1420. {
  1421. if (unicom[i] == atoi(phoneno.Left(3)))
  1422. {
  1423. return 1;
  1424. }
  1425. }
  1426. for (i = 0; i < sizeof(telecom) / sizeof(int); i++)
  1427. {
  1428. if (telecom[i] == atoi(phoneno.Left(3)))
  1429. {
  1430. return 2;
  1431. }
  1432. }
  1433. return -1;
  1434. }
  1435. /* else if(phoneno.GetAt (0)=='0')
  1436. {
  1437. if(phoneno.GetLength ()>=10 && phoneno.GetLength ()<=12)
  1438. {
  1439. return 3;
  1440. }
  1441. }*/
  1442. return -1;
  1443. }
  1444. CTime GetTmFromStr(CString date)
  1445. {
  1446. try
  1447. {
  1448. CTime tm(atoi(date.Mid(0, 4)), atoi(date.Mid(5, 2)), atoi(date.Mid(8, 2)), 0, 0, 0);
  1449. CString ss;
  1450. ss.Format("%d-%d-%d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
  1451. return tm;
  1452. }
  1453. catch (...)
  1454. {
  1455. date = "1980-01-01";
  1456. CTime tm(atoi(date.Mid(0, 4)), atoi(date.Mid(5, 2)), atoi(date.Mid(8, 2)), 0, 0, 0);
  1457. return tm;
  1458. }
  1459. }
  1460. void ConvertToPrice(CString &str)
  1461. {
  1462. if (str.Find('.') == -1)return;
  1463. str.TrimRight('0');
  1464. str.TrimRight('.');
  1465. }
  1466. void SortArray(CArray<CStringArray, CStringArray>*List1array, int pos)
  1467. {
  1468. CString stemp;
  1469. int last = List1array->GetSize() - 1;
  1470. int ret = last;
  1471. bool sorted = true;
  1472. do
  1473. {
  1474. sorted = true;
  1475. for (int i = 0; i < last; i++)
  1476. {
  1477. if (List1array->ElementAt(i).ElementAt(pos) < List1array->ElementAt(i + 1).ElementAt(pos))
  1478. {
  1479. for (int a = 0; a < List1array->ElementAt(i).GetSize(); a++)
  1480. {
  1481. stemp = List1array->ElementAt(i).ElementAt(a);
  1482. List1array->ElementAt(i).SetAt(a, List1array->ElementAt(i + 1).ElementAt(a));
  1483. List1array->ElementAt(i + 1).SetAt(a, stemp);
  1484. }
  1485. sorted = false;
  1486. }
  1487. }
  1488. last--;
  1489. } while (!sorted);
  1490. }
  1491. void DeCodePicCount(int &value1, int &value2)
  1492. {
  1493. // SoftKey ytSoftKey;
  1494. int ret = 0;//ytSoftKey.Ini();
  1495. if (ret != 0)
  1496. {
  1497. return;
  1498. }
  1499. int d[8]; double f[8];
  1500. 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("");
  1501. //运行自定义函数
  1502. ret = ytSoftKey.decryptpiccount(&value1, &value2, &d[2], &d[3], &d[4], &d[5], &d[6], &d[7],
  1503. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1504. s0, s1, s2, s3, s4, s5, s6, s7);
  1505. if (ret != 0 && ret != -43)
  1506. {
  1507. exit(0);
  1508. }
  1509. }
  1510. void DeCodelong1(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1511. {
  1512. // SoftKey ytSoftKey;
  1513. int ret = 0;//ytSoftKey.Ini();
  1514. if (ret != 0)
  1515. {
  1516. return;
  1517. }
  1518. double f[8];
  1519. 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("");
  1520. //运行自定义函数
  1521. ret = ytSoftKey.decryptlong1(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  1522. &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
  1523. s0, s1, s2, s3, s4, s5, s6, s7);
  1524. if (ret != 0 && ret != -43)
  1525. {
  1526. exit(0);
  1527. }
  1528. }
  1529. void DeCodelong2(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
  1530. {
  1531. // SoftKey ytSoftKey;
  1532. int ret = 0;//ytSoftKey.Ini();
  1533. if (ret != 0)
  1534. {
  1535. return;
  1536. }
  1537. 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.decryptlong2(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
  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 DeCodelong3(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.decryptlong3(&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 DeCodelong4(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. exit(0);
  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.decryptlong4(&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. #endif
  1587. #if JEFF_TEST_ON
  1588. /************************************************************************/
  1589. /*
  1590. 函数:IsValidDate
  1591. 描述:判断输入的公历日期是否有效;
  1592. 参数:
  1593. IN: lpDate 要判断的日期字符串;
  1594. 返回:日期有效返回TRUE,否则返回FALSE;
  1595. 要求:
  1596. 注意:
  1597. 示例:
  1598. */
  1599. /************************************************************************/
  1600. BOOL IsValidDate(IN LPCTSTR lpDate)
  1601. {
  1602. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  1603. return FALSE;
  1604. CString strDate = lpDate;
  1605. if (strDate.IsEmpty()) return FALSE;
  1606. // 判断日期合法性;
  1607. static int nYear = 0;
  1608. static int nMonth = 0;
  1609. static int nDay = 0;
  1610. nYear = nMonth = nDay = 0;
  1611. nYear = _ttoi(strDate.Left(4));
  1612. nMonth = _ttoi(strDate.Mid(5, 2));
  1613. nDay = _ttoi(strDate.Mid(8, 2));
  1614. CONST static INT nNormalMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1615. CONST static INT nLeapMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1616. if ((nDay <= 0) || (nDay > 31) || (nMonth <= 0) || (nMonth > 12)) return FALSE;
  1617. if ((nYear % 400 == 0) || (nYear % 4 == 0 && nYear % 100 != 0))
  1618. {
  1619. if (nDay > nLeapMonth[nMonth - 1]) return FALSE;
  1620. }
  1621. else
  1622. {
  1623. if (nDay > nNormalMonth[nMonth - 1]) return FALSE;
  1624. }
  1625. return TRUE;
  1626. }
  1627. #endif
  1628. /************************************************************************/
  1629. /*
  1630. 函数:LoadBackupModuleDll
  1631. 描述:加载备份模块
  1632. 参数:
  1633. IN CONST CString strDll 文件全路径
  1634. 返回:1成功,0失败
  1635. */
  1636. /************************************************************************/
  1637. int LoadBackupModuleDll(IN CONST CString strDll)
  1638. {
  1639. if (!::PathFileExists(strDll))
  1640. return 0;
  1641. HRESULT hr = NULL;
  1642. if(g_hDllInst)
  1643. return 1;
  1644. if(g_hDllInst == NULL)
  1645. {
  1646. g_hDllInst = LoadLibrary(strDll);
  1647. if(g_hDllInst == NULL)
  1648. {
  1649. WriteTextLog(_T("加载") + strDll + _T("失败\n"));
  1650. return 0;
  1651. }
  1652. }
  1653. WriteTextLog(_T("加载") + strDll + _T("成功\n"));
  1654. return 1;
  1655. }
  1656. /************************************************************************/
  1657. /*
  1658. 函数:FreeBackupModuleDll
  1659. 描述:卸载备份模块
  1660. 参数:
  1661. 返回:
  1662. */
  1663. /************************************************************************/
  1664. void FreeBackupModuleDll()
  1665. {
  1666. if(g_hDllInst != NULL)
  1667. FreeLibrary(g_hDllInst);
  1668. g_hDllInst = NULL;
  1669. }
  1670. static INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
  1671. {
  1672. switch (uMsg)
  1673. {
  1674. case BFFM_INITIALIZED:
  1675. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, NULL);
  1676. break;
  1677. case BFFM_SELCHANGED:
  1678. {
  1679. TCHAR szFileName[MAX_PATH];
  1680. LPITEMIDLIST pidlCurrent = (LPITEMIDLIST)lp;
  1681. SHGetPathFromIDList(pidlCurrent, szFileName);
  1682. SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szFileName);
  1683. }
  1684. break;
  1685. // 这个不能让默认目录为桌面;
  1686. // case BFFM_INITIALIZED:
  1687. // {
  1688. // // WParam is TRUE since you are passing a path.
  1689. // // It would be FALSE if you were passing a pidl.
  1690. // TCHAR szDir[MAX_PATH] = { 0 };
  1691. // GetCurrentDirectory(sizeof(szDir) / sizeof(TCHAR), szDir);
  1692. // SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
  1693. // }
  1694. // break;
  1695. }
  1696. return 0;
  1697. }
  1698. BOOL GetFilePath(OUT TCHAR *FilePath, IN HWND hParentWnd, IN TCHAR* InstructionString, IN UINT ulFlags)
  1699. {
  1700. BROWSEINFO bInfo;
  1701. LPITEMIDLIST pidl, pidlDesktop;
  1702. ZeroMemory((PVOID)&bInfo, sizeof(BROWSEINFO));
  1703. HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidlDesktop);
  1704. if ( hr != S_OK )
  1705. {
  1706. //WriteClientLog(_T("获取打开目录路径失败"));
  1707. return FALSE;
  1708. }
  1709. bInfo.pidlRoot = pidlDesktop;
  1710. bInfo.hwndOwner = hParentWnd;
  1711. bInfo.pszDisplayName = FilePath;
  1712. bInfo.lpszTitle = InstructionString;
  1713. bInfo.ulFlags = ulFlags;
  1714. bInfo.lpfn = BrowseCallbackProc;
  1715. bInfo.lParam = 0;
  1716. if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
  1717. {
  1718. //WriteClientLog(_T("获取打开目录路径失败1"));
  1719. return FALSE;
  1720. }
  1721. if (::SHGetPathFromIDList(pidl, FilePath) == FALSE)
  1722. {
  1723. //WriteClientLog(_T("获取打开目录路径失败2"));
  1724. return FALSE;
  1725. }
  1726. return TRUE;
  1727. }
  1728. TCHAR* GetGuidString()
  1729. {
  1730. GUID guid;
  1731. static TCHAR szBuf[64] = {0};
  1732. memset(szBuf, 0, 64*sizeof(TCHAR));
  1733. CoInitialize(NULL);
  1734. if (S_OK == ::CoCreateGuid(&guid))
  1735. {
  1736. sprintf(szBuf, _T("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
  1737. guid.Data1,
  1738. guid.Data2,
  1739. guid.Data3,
  1740. guid.Data4[0],
  1741. guid.Data4[1],
  1742. guid.Data4[2],
  1743. guid.Data4[3],
  1744. guid.Data4[4],
  1745. guid.Data4[5],
  1746. guid.Data4[6],
  1747. guid.Data4[7]
  1748. );
  1749. }
  1750. CoUninitialize();
  1751. return szBuf;
  1752. }
  1753. DWORD GetSoftRunTime()
  1754. {
  1755. DWORD id;
  1756. TCHAR HKey[50] = "ffffffff",LKey[50]="ffffffff";
  1757. TCHAR OutTime[50];
  1758. TCHAR DevicePath[ 260];
  1759. if(FindPort(0,DevicePath)!=0)
  1760. return 0;
  1761. //LIBC.LIB,libcp.lib,msvcprt.lib
  1762. TCHAR year[10],month[10],day[10],hour[10],minute[10],second[10];
  1763. ReadTimeAuthFromEprom(HKey,LKey, OutTime, &id, 30128, HKey, LKey, DevicePath);
  1764. CString str=OutTime;
  1765. CString temp="年";
  1766. int pos=str.Find (temp);
  1767. temp=str.Left (pos);
  1768. str=str.Right (str.GetLength ()-pos-2);
  1769. strcpy(year, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1770. temp="月";
  1771. pos=str.Find (temp);
  1772. temp=str.Left (pos);
  1773. str=str.Right (str.GetLength ()-pos-2);
  1774. strcpy(month, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1775. temp="日";
  1776. pos=str.Find (temp);
  1777. temp=str.Left (pos);
  1778. str=str.Right (str.GetLength ()-pos-2);
  1779. strcpy(day, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1780. temp="小时";
  1781. pos=str.Find (temp);
  1782. temp=str.Left (pos);
  1783. str=str.Right (str.GetLength ()-pos-4);
  1784. strcpy(hour, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1785. temp="分钟";
  1786. pos=str.Find (temp);
  1787. temp=str.Left (pos);
  1788. str=str.Right (str.GetLength ()-pos-4);
  1789. strcpy(minute, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1790. temp="0";
  1791. strcpy(second, temp.GetBuffer (0));temp.ReleaseBuffer ();
  1792. long time1=ConvertStringToTimerReal(year, month, day, hour, minute, second);
  1793. GetRunTimer(year, month, day, hour, minute, second, DevicePath);
  1794. long time2=ConvertStringToTimerReal(year, month, day, hour, minute, second);
  1795. // 软件可运行时间;
  1796. return time1 - time2;
  1797. }
  1798. /************************************************************************/
  1799. /* 函数:WriteTextLog[7/28/2016 IT];
  1800. /* 描述:写文本日志;
  1801. /* 参数:;
  1802. /* [IN] :;
  1803. /* 返回:void;
  1804. /* 注意:;
  1805. /* 示例:;
  1806. /*
  1807. /* 修改:;
  1808. /* 日期:;
  1809. /* 内容:;
  1810. /************************************************************************/
  1811. void WriteTextLog(const TCHAR *format, ...)
  1812. {
  1813. try
  1814. {
  1815. static ThreadSection _critSection;
  1816. AutoThreadSection aSection(&_critSection);
  1817. // 解析出日志路径;
  1818. TCHAR szlogpath[MAX_PATH] = {0};
  1819. static TCHAR szModulePath[MAX_PATH] = {0};
  1820. static TCHAR szFna[_MAX_DIR] = { 0 };
  1821. if ( szModulePath[0] == _T('\0') )
  1822. {
  1823. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  1824. TCHAR szDir[_MAX_DIR] = { 0 };
  1825. TCHAR szExt[_MAX_DIR] = { 0 };
  1826. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  1827. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  1828. _tcscpy_s(szModulePath, szDrive);
  1829. _tcscat_s(szModulePath, szDir);
  1830. }
  1831. _stprintf_s(szlogpath, _T("%s日志\\%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]"));
  1832. // 打开或创建文件;
  1833. CStdioFile fp;
  1834. if (PathFileExists(szlogpath))
  1835. {
  1836. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  1837. {
  1838. return;
  1839. }
  1840. fp.SeekToEnd();
  1841. }
  1842. else
  1843. {
  1844. if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
  1845. return;
  1846. }
  1847. // 格式化前设置语言区域;
  1848. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  1849. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  1850. // 格式化日志内容;
  1851. va_list args = NULL;
  1852. int len = 0;
  1853. TCHAR *buffer = NULL;
  1854. va_start( args, format );
  1855. // _vscprintf doesn't count. terminating '\0'
  1856. len = _vsctprintf_p( format, args );
  1857. if ( len == -1 )
  1858. {
  1859. goto clear;
  1860. }
  1861. len++;
  1862. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  1863. _vstprintf_s( buffer, len, format, args ); // C4996
  1864. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  1865. // 将日志内容输入到文件中;
  1866. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  1867. fp.WriteString(buffer);
  1868. fp.WriteString(_T("\n"));
  1869. // 关闭文件,释放资源并设置回原语言区域;
  1870. free( buffer );
  1871. clear:
  1872. _tsetlocale(LC_CTYPE, old_locale);
  1873. free(old_locale);//还原区域设定;
  1874. fp.Close();
  1875. }
  1876. catch (CException *e)
  1877. {
  1878. e->ReportError();
  1879. e->Delete();
  1880. }
  1881. }
  1882. /************************************************************************/
  1883. /* 函数:ExectueScript[8/11/2016 IT];
  1884. /* 描述:;
  1885. /* 参数:;
  1886. /* [IN] lpCatalogSvr:数据库地址;
  1887. /* [IN] lpCatalogUser:数据库用户名;
  1888. /* [IN] lpCatalogPsw:数据库密码;
  1889. /* [IN] lpScriptFile:要执行的数据库脚本文件;
  1890. /* [IN] lpCatalogLog:执行脚本后输出的日志文件;
  1891. /* 返回:成功执行sqlcmd返回true;
  1892. /* 注意:;
  1893. /* 示例:;
  1894. /*
  1895. /* 修改:;
  1896. /* 日期:;
  1897. /* 内容:;
  1898. /************************************************************************/
  1899. BOOL ExectueScript(IN LPCTSTR lpCatalogSvr, IN LPCTSTR lpCatalogUser, IN LPCTSTR lpCatalogPsw, IN LPCTSTR lpScriptFile, IN LPCTSTR lpCatalogLog)
  1900. {
  1901. // 参数有效性判断;
  1902. if ( lpCatalogSvr == NULL || lpCatalogSvr[0] == _T('\0') ||
  1903. lpCatalogUser == NULL || lpCatalogUser[0] == _T('\0') ||
  1904. lpCatalogPsw == NULL || lpCatalogPsw[0] == _T('\0') ||
  1905. lpScriptFile == NULL || lpScriptFile[0] == _T('\0') || !PathFileExists(lpScriptFile) ||
  1906. lpCatalogLog == NULL || lpCatalogLog[0] == _T('\0') )
  1907. {
  1908. WriteTextLog(_T("%s-%d-%s:参数无效!"),__FILE__, __LINE__, __FUNCTION__);
  1909. return FALSE;
  1910. }
  1911. SHELLEXECUTEINFO stuExecInfo = { 0 };
  1912. DWORD dwExitCode = STILL_ACTIVE;
  1913. CString strCommandLine;
  1914. strCommandLine.Format(_T(" -S %s -U %s -P %s -d master -i \"%s\" -o \"%s\""), lpCatalogSvr, lpCatalogUser, lpCatalogPsw, lpScriptFile, lpCatalogLog);// 创建数据库时,不能用/d选项;
  1915. stuExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  1916. stuExecInfo.lpFile = _T("sqlcmd");
  1917. stuExecInfo.lpParameters = strCommandLine;
  1918. stuExecInfo.nShow = SW_HIDE;
  1919. stuExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
  1920. if (!ShellExecuteEx(&stuExecInfo))
  1921. {
  1922. WriteTextLog(_T("执行ShellExecuteEx失败,错误码:%d"), GetLastError());
  1923. return FALSE;
  1924. }
  1925. HANDLE hProcess = stuExecInfo.hProcess;
  1926. if ( hProcess != NULL )
  1927. {
  1928. WaitForSingleObject(hProcess,INFINITE);
  1929. CloseHandle(hProcess);
  1930. }
  1931. return TRUE;
  1932. }
  1933. /************************************************************************/
  1934. /* 函数:ExectueStatement[8/11/2016 IT];
  1935. /* 描述:;
  1936. /* 参数:;
  1937. /* [IN] lpCatalogSvr:数据库地址;
  1938. /* [IN] lpCatalogUser:数据库用户名;
  1939. /* [IN] lpCatalogPsw:数据库密码;
  1940. /* [IN] lpCatalogName:对象数据库名称;
  1941. /* [IN] lpSQLScript:要执行的SQL语句;
  1942. /* [IN] dwQueryTimeOut:查询超时值,单位秒;
  1943. /* 返回:成功返回日志内容;
  1944. /* 注意:;
  1945. /* 示例:;
  1946. /*
  1947. /* 修改:;
  1948. /* 日期:;
  1949. /* 内容:;
  1950. /************************************************************************/
  1951. CString ExectueStatement(IN LPCTSTR lpCatalogSvr, IN LPCTSTR lpCatalogUser, IN LPCTSTR lpCatalogPsw, IN LPCTSTR lpCatalogName, IN LPCTSTR lpSQLScript, IN const DWORD& dwQueryTimeOut /* = 15 */ )
  1952. {
  1953. // 参数有效性判断;
  1954. if ( lpCatalogSvr == NULL || lpCatalogSvr[0] == _T('\0') ||
  1955. lpCatalogUser == NULL || lpCatalogUser[0] == _T('\0') ||
  1956. lpCatalogPsw == NULL || lpCatalogPsw[0] == _T('\0') ||
  1957. lpCatalogName == NULL || lpCatalogName[0] == _T('\0') ||
  1958. lpSQLScript == NULL || lpSQLScript[0] == _T('\0') )
  1959. {
  1960. WriteTextLog(_T("%s-%d-%s:参数无效!"),__FILE__, __LINE__, __FUNCTION__);
  1961. return _T("");
  1962. }
  1963. SHELLEXECUTEINFO stuExecInfo = { 0 };
  1964. DWORD dwExitCode = STILL_ACTIVE;
  1965. CString strCommandLine;
  1966. strCommandLine.Format(_T(" -S %s -U %s -P %s -d %s -Q \"%s\" -t %d -o sqlerr"), lpCatalogSvr, lpCatalogUser, lpCatalogPsw, lpCatalogName, lpSQLScript, dwQueryTimeOut);
  1967. stuExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  1968. stuExecInfo.lpFile = _T("sqlcmd");
  1969. stuExecInfo.lpParameters = strCommandLine;
  1970. stuExecInfo.nShow = SW_HIDE;
  1971. stuExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
  1972. if (!ShellExecuteEx(&stuExecInfo))
  1973. {
  1974. WriteTextLog(_T("执行ShellExecuteEx失败,错误码:%d"), GetLastError());
  1975. return _T("");
  1976. }
  1977. HANDLE hProcess = stuExecInfo.hProcess;
  1978. if ( hProcess != NULL )
  1979. {
  1980. WaitForSingleObject(hProcess,INFINITE);
  1981. CloseHandle(hProcess);
  1982. }
  1983. if ( !PathFileExists(_T("sqlerr")) )
  1984. {
  1985. WriteTextLog(_T("执行sqlcmd无日志文件返回"));
  1986. return _T("");
  1987. }
  1988. struct stat statbuf;
  1989. stat(_T("sqlerr"), &statbuf);
  1990. if ( statbuf.st_size == 0 )
  1991. return _T("");
  1992. TCHAR *perr = new TCHAR[statbuf.st_size+1];
  1993. memset(perr, 0, statbuf.st_size+1);
  1994. FILE *err = NULL;
  1995. _tfopen_s(&err, _T("sqlerr"), _T("r"));
  1996. if ( err )
  1997. {
  1998. fread_s(perr, statbuf.st_size, sizeof(TCHAR), statbuf.st_size, err);
  1999. fclose(err);
  2000. }
  2001. else
  2002. {
  2003. WriteTextLog(_T("打开sqlcmd日志文件失败"));
  2004. return _T("");
  2005. }
  2006. CString strErr = perr;
  2007. if ( perr )
  2008. delete []perr;
  2009. return strErr;
  2010. }
  2011. BOOL IsCatalogDamage()
  2012. {
  2013. CString strSQL;
  2014. strSQL.Format(_T("dbcc checkdb('%s')"), g_szDBName);
  2015. CString strErr = ExectueStatement(g_szDBSource, g_szDBAccount, g_szDBPassWord, _T("master"), strSQL, g_dwCheckdbTimeOut ? g_dwCheckdbTimeOut : 30);
  2016. if ( !strErr.IsEmpty() )
  2017. {
  2018. strSQL.Format(_T("CHECKDB 在数据库 '%s' 中发现 0 个分配错误和 0 个一致性错误。"),g_szDBName);
  2019. if ( -1 == strErr.Find(strSQL) )
  2020. {
  2021. return FALSE;
  2022. }
  2023. }
  2024. return TRUE;
  2025. }