Global.cpp 50 KB

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