Global.cpp 56 KB

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