1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534 |
- #include "StdAfx.h"
- #include "Global.h"
- #include "CritSection.h"
- #include "./helper/ffsco.h"
- TCHAR g_ModulePath[MAX_PATH] = _T(""); // 软件目录;
- TCHAR g_ModuleFileName[MAX_PATH] = _T(""); // 软件名称;
- TCHAR g_szConnectString[MAX_PATH] = _T(""); // DB连接串;
- TCHAR g_szIniFile[MAX_PATH] = _T("");
- // 服务器信息;
- TCHAR g_szDBSource[MAX_PATH] = _T(""); // 数据库源(服务所在IP或计算机名);
- TCHAR g_szDBSourcePort[MAX_PATH] = _T(""); // 数据库源端口;
- DWORD g_dwDBServerPort = 0; // 数据库源端口;
- TCHAR g_szDBAccount[MAX_PATH] = _T(""); // 数据库登录用户;
- TCHAR g_szDBPassWord[MAX_PATH] = _T(""); // 数据库登录密码;
- TCHAR g_szDBName[MAX_PATH] = _T(""); // 数据库名称;
- // start+------------------------------------------------------------------
- // Jeff:以下为原本程序全局变量;
- #if THE_PRIMARY_GLOBAL_VARIABLE
- int g_dwTCPPort = 5678; // Jeff.通信端口;
- BOOL g_bReg = 0;
- CString g_strdbpsw; // 数据库账号、密码;
- CString g_strdbServrename; // 数据库服务名(地址)
- CString g_bakinfo;
- CString g_bakphoto;
- CString g_mainpath;
- CString g_localname; // 本地计算机名;
- CDatabase g_db; // 名为"db"的数据库,本年度使用的数据库;
- CArray<int, int> g_nYearArray; // 年份(用于命名历史数据库)
- //CArray<CDatabase*,CDatabase*>g_dbArray; // 历史数据库;
- CArray<CDatabase*, CDatabase*>g_dbHistoryArray; // 历史数据库;
- CArray<CDatabase*, CDatabase*>g_branchdbarray;
- CArray<CDatabase*, CDatabase*>g_branchdb2array;//除历史年度的分店DB
- CStringArray g_branchnamearray;
- CStringArray g_branchname2array;
- CStringArray g_branchiparray;
- CStringArray g_branchdomainarray;
- CDatabase *g_curdb = NULL;
- int g_conncount = -1; // Jeff.加密狗中的座席版数量;
- CString g_sdomain;
- CStringArray g_conniparray;
- CStringArray g_connidarray;
- BOOL g_bCheckAll = 0;
- BYTE *g_pSkinData = NULL;
- DWORD g_SkinLeng;
- BOOL g_bAutoRun = 0;
- CStringArray g_filedatearray;
- CStringArray g_filedatearraynew;
- CString g_bakdate;
- CString g_localip;
- BOOL g_bSteal = 0;
- CString g_bakserver1;
- CString g_bakserver2;
- CString g_bakserver3;
- CString g_bakserver4;
- DWORD g_tisks = 0;
- #endif
- // endof+------------------------------------------------------------------
- //************************************//
- // 数据库地址;
- // 数据库端口;
- // 数据库管理员账号;
- // 数据库管理员密码;
- // 数据库名称;
- //
- // [函数]:GetIniInfo
- // [描述]:获取指定ini信息;
- // [参数]:
- // szPath:ini所在目录;
- // szIniName:ini名;
- // [返回]:void
- //
- //************************************//
- int GetIniInfo(const char *szPath, const char *szIniName)
- {
- TCHAR szDrive[_MAX_DRIVE];
- TCHAR szDir[_MAX_DIR];
- ::GetModuleFileName(NULL, g_ModulePath, sizeof(g_ModulePath) / sizeof(TCHAR));
- sprintf(g_ModuleFileName, "%s", g_ModulePath);
- _tsplitpath(g_ModulePath, szDrive, szDir, NULL, NULL);
- _tcscpy(g_ModulePath, szDrive);
- _tcscat(g_ModulePath, szDir);
- // -----------------------------------------------------//
- if (szPath != NULL && szIniName != NULL)
- wsprintf(g_szIniFile, "%s\\%s", szPath, szIniName);
- else
- wsprintf(g_szIniFile, "%s\\ServiceInfo.ini", g_ModulePath);
- TRACE("szFile =%s\n", g_szIniFile);
- OFSTRUCT ofStruct;
- OpenFile(g_szIniFile, &ofStruct, OF_EXIST);
- if (ERROR_FILE_NOT_FOUND == GetLastError())
- {
- return -1;
- }
- // 获取服务器端信息;
- GetPrivateProfileString("DatabaseInfo", "dbSource", "", g_szDBSource, MAX_PATH, g_szIniFile);
- g_dwDBServerPort = GetPrivateProfileInt("DatabaseInfo", "dbServerPort", 0, g_szIniFile);
- GetPrivateProfileString("DatabaseInfo", "dbAccount", "", g_szDBAccount, MAX_PATH, g_szIniFile);
- GetPrivateProfileString("DatabaseInfo", "dbPassWord", "", g_szDBPassWord, MAX_PATH, g_szIniFile);
- GetPrivateProfileString("DatabaseInfo", "dbName", "", g_szDBName, MAX_PATH, g_szIniFile);
- g_dwTCPPort = GetPrivateProfileInt("NetWorkInfo", "TCPChatPort", 5678, g_szIniFile);
- if (g_dwDBServerPort != 0)
- sprintf(g_szConnectString, "driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s",
- g_szDBSource, g_dwDBServerPort, g_szDBName, g_szDBAccount, g_szDBPassWord);
- else
- sprintf(g_szConnectString, "driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s",
- g_szDBSource, g_szDBName, g_szDBAccount, g_szDBPassWord);
- return 0;
- }
- //------------------------------------------------------------
- // 以下全局函数为原程序所有;
- #if THE_PRIMARY_GLOBAL_FUNCTION
- BOOL AddConn(CDatabase *m_conndb, CString dbname)
- {
- try {
- CString strCon;
- strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=%s;%s"), g_strdbServrename, dbname, g_strdbpsw);
- m_conndb->OpenEx(strCon, CDatabase::noOdbcDialog);
- }
- catch (CDBException * e)
- {
- WriteTextLog(_T("新建连接时出错:") + e->m_strError);
- e->Delete();
- return 0;
- }
- return 1;
- }
- void WriteError(CString str)
- {
- MyLock lock("DBServerWriteError");
- CStdioFile fp;
- CString path = g_mainpath + "\\MsgError.txt";
- if (::PathFileExists(path))
- {
- fp.Open(path, CFile::modeWrite);
- int length = fp.GetLength();
- if (length > 1024 * 1024)
- {
- fp.Close();
- ::DeleteFile(path); // 删除MsgError.txt
- return;
- }
- fp.SeekToEnd();
- }
- else
- fp.Open(path, CFile::modeCreate | CFile::modeWrite);
- fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
- fp.Close();
- }
- void WritePswWrong(CString str)
- {
- MyLock lock("DBServerWritePswWrong");
- CStdioFile fp;
- CString path = g_mainpath + "\\PswWrong.txt";
- if (::PathFileExists(path))
- {
- fp.Open(path, CFile::modeWrite);
- int length = fp.GetLength();
- if (length > 1024 * 1024)
- {
- fp.Close();
- ::DeleteFile(path); // 删除PswWrong.txt
- return;
- }
- fp.SeekToEnd();
- }
- else
- fp.Open(path, CFile::modeCreate | CFile::modeWrite);
- fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
- fp.Close();
- }
- void WriteNullBalance(CString str)
- {
- MyLock lock("DBServerWriteNullBalance");
- CStdioFile fp;
- CString path = g_mainpath + "\\NullBalance.txt";
- if (::PathFileExists(path))
- {
- fp.Open(path, CFile::modeWrite);
- int length = fp.GetLength();
- if (length > 1024 * 1024)
- {
- fp.Close();
- ::DeleteFile(path); // 删除NullBalance.txt
- return;
- }
- fp.SeekToEnd();
- }
- else
- fp.Open(path, CFile::modeCreate | CFile::modeWrite);
- fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
- fp.Close();
- }
- void WriteUnallowIP(CString str)
- {
- CStdioFile fp;
- CString path = g_mainpath + "\\UnallowIP.txt";
- if (::PathFileExists(path))
- {
- fp.Open(path, CFile::modeWrite);
- int length = fp.GetLength();
- if (length > 1024 * 1024)
- {
- fp.Close();
- ::DeleteFile(path); // 删除UnallowIP.txt
- return;
- }
- fp.SeekToEnd();
- }
- else
- fp.Open(path, CFile::modeCreate | CFile::modeWrite);
- fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S ") + str + "\n");
- fp.Close();
- }
- int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
- {
- UINT num = 0; // number of image encoders
- UINT size = 0; // size of the image encoder array in bytes CheckFolderFileExist
- ImageCodecInfo* pImageCodecInfo = NULL;
- GetImageEncodersSize(&num, &size);
- if (size == 0)
- return -1; // Failure
- pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
- if (pImageCodecInfo == NULL)
- return -1; // Failure
- GetImageEncoders(num, size, pImageCodecInfo);
- for (UINT j = 0; j < num; ++j)
- {
- if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
- {
- *pClsid = pImageCodecInfo[j].Clsid;
- free(pImageCodecInfo);
- return j; // Success
- }
- }
- free(pImageCodecInfo);
- return -1; // Failure
- }
- void SaveImageToFile(Image *img, CString path)
- {
- try
- {
- if (img == NULL)return;
- CLSID encoderClsid;
- BSTR bstr = path.AllocSysString();
- path.MakeLower();
- if (path.Right(3) == "bmp")
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- img->Save(bstr, &encoderClsid, NULL);
- }
- else if (path.Right(3) == "png")
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- img->Save(bstr, &encoderClsid, NULL);
- }
- else// if(path.Right (3)=="jpg")
- {
- GetEncoderClsid(L"image/jpeg", &encoderClsid);
- EncoderParameters encoderParameters;
- ULONG quality;
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
- encoderParameters.Parameter[0].NumberOfValues = 1;
- // Save the image as a JPEG with quality level 100.
- quality = 100;
- encoderParameters.Parameter[0].Value = &quality;
- img->Save(bstr, &encoderClsid, &encoderParameters);
- }
- SysFreeString(bstr);
- }
- catch (...)
- {
- }
- }
- void SaveImageToFile2(Image *img, CString path, ULONG quality)
- {
- try
- {
- if (img == NULL)return;
- CLSID encoderClsid;
- BSTR bstr = path.AllocSysString();
- path.MakeLower();
- if (path.Right(3) == "bmp")
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- img->Save(bstr, &encoderClsid, NULL);
- }
- else if (path.Right(3) == "png")
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- img->Save(bstr, &encoderClsid, NULL);
- }
- else// if(path.Right (3)=="jpg")
- {
- GetEncoderClsid(L"image/jpeg", &encoderClsid);
- EncoderParameters encoderParameters;
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
- encoderParameters.Parameter[0].NumberOfValues = 1;
- // Save the image as a JPEG with quality level 100.
- encoderParameters.Parameter[0].Value = &quality;
- img->Save(bstr, &encoderClsid, &encoderParameters);
- }
- SysFreeString(bstr);
- }
- catch (...)
- {
- }
- }
- void RectFitDes(int width, int height, CRect &rc)
- {
- float fscale = (float)width / (float)height;
- float rcscale = ((float)rc.Width()) / ((float)rc.Height());
- int rcwid = rc.Width();
- int rchei = rc.Height();
- int dt = 0;
- if (rcscale < fscale)
- {
- dt = (rchei - rcwid / fscale) / 2;
- rc.top += dt;
- rc.bottom -= dt;
- }
- else
- {
- dt = (rcwid - rchei*fscale) / 2;
- rc.left += dt;
- rc.right -= dt;
- }
- }
- ///目录是否存在的检查: 支持网络文件,判断文件时有漏洞
- bool CheckFolderFileExist(CString &strPath)
- {
- strPath.MakeLower();
- 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)
- {
- return ::PathFileExists(strPath);
- }
- CString path = strPath;
- CFile fp;
- if (fp.Open(path, CFile::modeRead))
- {
- fp.Close();
- return 1;
- }
- if (strPath.Right(1) != "\\")
- path += "\\";
- path += "zaqw1234.dat";
- ::DeleteFile(path);// 删除zaqw1234.dat文件;
- if (fp.Open(path, CFile::modeCreate) == 0)
- return 0;
- fp.Close();
- ::DeleteFile(path); // 删除zaqw1234.dat文件;
- return 1;
- }
- bool CheckFileExist(CString &strPath)
- {
- strPath.MakeLower();
- 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)
- {
- return ::PathFileExists(strPath);
- }
- CString path = strPath;
- CFile fp;
- if (fp.Open(path, CFile::modeRead))
- {
- fp.Close();
- return 1;
- }
- return 0;
- }
- BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
- {
- try
- {
- HCRYPTPROV hCryptProv;
- HCRYPTKEY hKey;
- HCRYPTHASH hHash;
- PBYTE pbBuffer;
- DWORD dwBlockLen;
- DWORD dwBufferLen;
- DWORD dwCount;
- //以下获得一个CSP句柄
- if (CryptAcquireContext(
- &hCryptProv,
- NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
- NULL,
- PROV_RSA_FULL,
- 0))
- {
- printf("A cryptographic provider has been acquired. \n");
- }
- else//密钥容器不存在
- {
- if (CryptAcquireContext(
- &hCryptProv,
- NULL,
- NULL,
- PROV_RSA_FULL,
- CRYPT_NEWKEYSET))//创建密钥容器
- {
- //创建密钥容器成功,并得到CSP句柄
- printf("A new key container has been created.\n");
- }
- else
- {
- return 0;
- }
- }
- //--------------------------------------------------------------------
- // 创建一个会话密钥(session key)
- // 会话密钥也叫对称密钥,用于对称加密算法。
- // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
- // CryptReleaseContext 期间的阶段。)
- //--------------------------------------------------------------------
- // Create a hash object.
- if (CryptCreateHash(
- hCryptProv,
- CALG_MD5,
- 0,
- 0,
- &hHash))
- {
- printf("A hash object has been created. \n");
- }
- else
- {
- return 0;
- }
- //--------------------------------------------------------------------
- // 用输入的密码产生一个散列
- if (CryptHashData(
- hHash,
- (BYTE *)szPassword,
- strlen(szPassword),
- 0))
- {
- printf("The password has been added to the hash. \n");
- }
- else
- {
- return 0;
- }
- //--------------------------------------------------------------------
- // 通过散列生成会话密钥(session key)
- if (CryptDeriveKey(
- hCryptProv,
- ENCRYPT_ALGORITHM,
- hHash,
- KEYLENGTH,
- &hKey))
- {
- printf("An encryption key is derived from the password hash. \n");
- }
- else
- {
- return 0;
- }
- //--------------------------------------------------------------------
- // Destroy the hash object.
- CryptDestroyHash(hHash);
- hHash = NULL;
- //--------------------------------------------------------------------
- // The session key is now ready.
- //--------------------------------------------------------------------
- // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
- // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
- // 数据长度。
- dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
- //--------------------------------------------------------------------
- // Determine the block size. If a block cipher is used,
- // it must have room for an extra block.
- if (ENCRYPT_BLOCK_SIZE > 1)
- dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
- else
- dwBufferLen = dwBlockLen;
- dwCount = dwBufferLen;
- //--------------------------------------------------------------------
- // In a do loop, encrypt the source file and write to the source file.
- int count;
- // if(leng%dwBlockLen==0)
- count = leng / dwBufferLen;
- // else
- // count=leng/dwBlockLen+1;
- int i = 0;
- for ( i = 0; i < count; i++)
- {
- pbBuffer = buffer + i*dwBufferLen;
- // 加密数据
- if (!CryptEncrypt(
- hKey, //密钥
- 0, //如果数据同时进行散列和加密,这里传入一个散列对象
- 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
- //这里通过判断是否到文件尾来决定是否为最后一块。
- 0, //保留
- pbBuffer, //输入被加密数据,输出加密后的数据
- &dwCount, //输入被加密数据实际长度,输出加密后数据长度
- dwBufferLen)) //pbBuffer的大小。
- {
- return 0;
- }
- }
- if (leng%dwBlockLen)
- {
- pbBuffer = buffer + i*dwBufferLen;
- dwCount = leng - i*dwBufferLen;
- if (!CryptEncrypt(
- hKey, //密钥
- 0, //如果数据同时进行散列和加密,这里传入一个散列对象
- TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
- //这里通过判断是否到文件尾来决定是否为最后一块。
- 0, //保留
- pbBuffer, //输入被加密数据,输出加密后的数据
- &dwCount, //输入被加密数据实际长度,输出加密后数据长度
- dwBufferLen)) //pbBuffer的大小。
- {
- return 0;
- }
- }
- //--------------------------------------------------------------------
- // Destroy session key.
- if (hKey)
- CryptDestroyKey(hKey);
- //--------------------------------------------------------------------
- // Destroy hash object.
- if (hHash)
- CryptDestroyHash(hHash);
- //--------------------------------------------------------------------
- // Release provider handle.
- if (hCryptProv)
- CryptReleaseContext(hCryptProv, 0);
- return(TRUE);
- }
- catch (...)
- {
- }
- }
- //-------------------------------------------------------------------------------
- // Remark by Jeff
- // 函数:BakData2
- // 描述:
- // 参数:
- // savedir : 存储备份数据库文件的目录;
- // bToday : 是否备份今天的数据库;
- // bBakHis : 是否备份历史数据库;
- //
- // 返回:无;
- //-------------------------------------------------------------------------------
- void BakData2(CString savedir, BOOL bToday, BOOL bBakHis)
- {
- // 1.使用master系统数据库来备份;
- CDatabase g_masterdb;
- try
- {
- #if 0
- CString strCon;
- strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=master;%s"), g_strdbServrename, g_strdbpsw);
- g_masterdb.OpenEx(strCon, CDatabase::noOdbcDialog);
- #else
- TCHAR szConnectString[MAX_PATH] = _T("");
- if (g_dwDBServerPort != 0)
- sprintf(szConnectString, "driver={SQL Server};Server=%s,%d;database=master;uid=%s;pwd=%s",
- g_szDBSource, g_dwDBServerPort, g_szDBAccount, g_szDBPassWord);
- else
- sprintf(szConnectString, "driver={SQL Server};Server=%s;database=master;uid=%s;pwd=%s",
- g_szDBSource, g_szDBAccount, g_szDBPassWord);
- g_masterdb.OpenEx(szConnectString, CDatabase::noOdbcDialog);
- #endif
- }
- catch (CDBException *e)
- {
- e->Delete();
- }
- try
- {
- // 2.获取昨天的日期tm2;
- CTime tm = CTime::GetCurrentTime();
- CTimeSpan dt(1, 0, 0, 0);
- CTime tm2;
- if (bToday)
- tm2 = tm;
- else
- tm2 = tm - dt;
- CString savepath;
- savepath.Format("%s\\%s(sql).bak", savedir, tm2.Format("%Y%m%d"));
- BOOL bBak = 0; // 是否备份成功;
- BOOL bBak2 = 0; // 是否备份历史年限数据库成功;
- // 3.如果没找到<昨天>备份数据库,则进行备份;
- if (::PathFileExists(savepath) == 0)
- {
- g_masterdb.SetQueryTimeout(600);
- {
- CString sql;
- #if 0 // Jeff.deal;
- sql.Format("BACKUP DATABASE db TO DISK='%s' WITH FORMAT", savepath);
- #else
- sql.Format("BACKUP DATABASE %s TO DISK='%s' WITH FORMAT", g_szDBName, savepath);
- #endif
- try
- {
- g_masterdb.ExecuteSQL(sql);
- bBak = 1;
- g_bakinfo = "资料备份成功";
- }
- catch (CDBException * e)
- {
- WriteTextLog(_T("%s,备份失败"), e->m_strError);
- e->Delete();
- }
- }
- g_masterdb.SetQueryTimeout(100);
- }
- // 4.Jeff.是否备份历史数据库;
- if (bBakHis)
- {
- for (int i = CTime::GetCurrentTime().GetYear() - 1; i >= 2009; i--)
- {
- g_masterdb.SetQueryTimeout(600);
- {
- CString savepath2;
- savepath2.Format("%s\\%s-%d(sql).bak", savedir, tm2.Format("%Y%m%d"), i);
- CString sql;
- sql.Format("BACKUP DATABASE [%d] TO DISK='%s' WITH FORMAT", i, savepath2);
- try
- {
- g_masterdb.ExecuteSQL(sql);
- bBak2 = 1;
- }
- catch (CDBException * e)
- {
- e->Delete();
- break;
- }
- }
- g_masterdb.SetQueryTimeout(100);
- }
- }
- // 5.昨天的数据库备份是否备份成功;
- if (bBak)
- {
- CTimeSpan dt2(31, 0, 0, 0);
- CTimeSpan dt3(365, 0, 0, 0);
- // tm:今天, tm3:1个月前, tm4:1年前;
- CTime tm3 = tm - dt2;
- CTime tm4 = tm - dt3;
- // 6.循环删除数据库备份;
- while (tm3 > tm4)
- {
- savepath.Format("%s\\%s.bak", savedir, tm3.Format("%Y%m%d"));
- #ifdef SQLSERVER_VERSION
- savepath.Format("%s\\%s(sql).bak", savedir, tm3.Format("%Y%m%d"));
- #endif
- // Jeff.删除一个月前的数据库备份;
- if (!CheckFileExist(savepath))
- break;
- ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
- tm3 -= dt; // dt 1天的间隔;
- }
- }
- if (bBak2)
- {
- CTimeSpan dt2(15, 0, 0, 0);
- CTimeSpan dt3(365, 0, 0, 0);
- for (int i = tm.GetYear() - 1; i >= 2009; i--)
- {
- CTime tm3 = tm - dt2;
- CTime tm4 = tm - dt3;
- while (tm3 > tm4)
- {
- savepath.Format("%s\\%s-%d.bak", savedir, tm3.Format("%Y%m%d"), i);
- #ifdef SQLSERVER_VERSION
- savepath.Format("%s\\%s-%d(sql).bak", savedir, tm3.Format("%Y%m%d"), i);
- #endif
- if (!CheckFileExist(savepath))
- break;
- ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
- tm3 -= dt;
- }
- }
- }
- }
- catch (...)
- {
- WriteTextLog("出错在函数BakData2中");
- }
- g_masterdb.Close();
- }
- void BakData(CString savedir, BOOL bToday, BOOL bBakHis)// back data
- {
- #ifdef SQLSERVER_VERSION
- BakData2(savedir, bToday, bBakHis);
- return;
- #endif
- try
- {
- CTime tm = CTime::GetCurrentTime();
- CTimeSpan dt(1, 0, 0, 0);
- CTime tm2;
- if (bToday)
- tm2 = tm;
- else
- tm2 = tm - dt;
- CString savepath;
- savepath.Format("%s\\%s.bak", savedir, tm2.Format("%Y%m%d"));
- #ifdef SQLSERVER_VERSION
- savepath.Format("%s\\%s(sql).bak", savedir, tm2.Format("%Y%m%d"));
- #endif
- BOOL bBak = 0;
- BOOL bBak2 = 0;
- {
- TCHAR szDBFile[MAX_PATH] = _T(""); // Jeff.add
- #ifdef SQLSERVER_VERSION
- sprintf(szDBFile, "%s\\数据\\%s.mdf", g_ModulePath, g_szDBName);
- //if(::CopyFile (g_mainpath+"\\数据\\db.mdf", savepath, 0))
- if (::CopyFile(szDBFile, savepath, 0))
- #else
- sprintf(szDBFile, "%s\\数据\\%s.mdb", g_ModulePath, g_szDBName);
- //if(::CopyFile (g_mainpath+"\\数据\\db.mdb", savepath, 0))
- if (::CopyFile(szDBFile, savepath, 0))
- #endif
- {
- bBak = 1;
- g_bakinfo = "资料备份成功";
- }
- }
- for (int i = tm.GetYear() - 1; i >= 1900; i--)
- {
- #ifdef SQLSERVER_VERSION
- savepath.Format("%s\\数据\\%d.mdf", g_mainpath, i);
- #else
- savepath.Format("%s\\数据\\%d.mdb", g_mainpath, i);
- #endif
- if (CheckFileExist(savepath) == 0)break;
- CString savepath2;
- savepath2.Format("%s\\%s-%d.bak", savedir, tm2.Format("%Y%m%d"), i);
- #ifdef SQLSERVER_VERSION
- savepath2.Format("%s\\%s-%d(sql).bak", savedir, tm2.Format("%Y%m%d"), i);
- #endif
- if (::CopyFile(savepath, savepath2, 0))
- bBak2 = 1;
- }
- if (bBak)
- {
- CTimeSpan dt2(15, 0, 0, 0);
- CTimeSpan dt3(365, 0, 0, 0);
- CTime tm3 = tm - dt2;
- CTime tm4 = tm - dt3;
- while (tm3 > tm4)
- {
- savepath.Format("%s\\%s.bak", savedir, tm3.Format("%Y%m%d"));
- #ifdef SQLSERVER_VERSION
- savepath.Format("%s\\%s(sql).bak", savedir, tm3.Format("%Y%m%d"));
- #endif
- if (!CheckFileExist(savepath))
- break;
- ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
- tm3 -= dt;
- }
- }
- if (bBak2)
- {
- CTimeSpan dt2(7, 0, 0, 0);
- CTimeSpan dt3(365, 0, 0, 0);
- for (int i = tm.GetYear() - 1; i >= 1900; i--)
- {
- #ifdef SQLSERVER_VERSION
- savepath.Format("%s\\数据\\%d.mdf", g_mainpath, i);
- #else
- savepath.Format("%s\\数据\\%d.mdb", g_mainpath, i);
- #endif
- if (CheckFileExist(savepath) == 0)break;
- CTime tm3 = tm - dt2;
- CTime tm4 = tm - dt3;
- while (tm3 > tm4)
- {
- savepath.Format("%s\\%s-%d.bak", savedir, tm3.Format("%Y%m%d"), i);
- #ifdef SQLSERVER_VERSION
- savepath.Format("%s\\%s-%d(sql).bak", savedir, tm3.Format("%Y%m%d"), i);
- #endif
- if (!CheckFileExist(savepath))
- break;
- ::DeleteFile(savepath); // 删除备份的数据库文件*.bak;
- tm3 -= dt;
- }
- }
- }
- }
- catch (...)
- {
- WriteTextLog("出错在函数BakData中");
- }
- }
- DWORD FindAppProcessID(CString path)
- {
- try
- {
- HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
- PROCESSENTRY32 Info;
- Info.dwSize = sizeof(PROCESSENTRY32);
- path.MakeLower();
- if (::Process32First(handle, &Info))
- {
- do
- {
- CString ss = Info.szExeFile;
- ss.MakeLower();
- if (ss == path)
- {
- ::CloseHandle(handle);
- return Info.th32ProcessID;
- }
- } while (::Process32Next(handle, &Info));
- ::CloseHandle(handle);
- }
- return -1;
- }
- catch (...)
- {
- }
- }
- void GetPathFromNetShareName(CString name, CString &sRet)
- {
- if (sRet.Find(":") != -1)return;
- NET_API_STATUS res;
- SHARE_INFO_502 * psi = NULL;
- WCHAR wszShareName[255];
- TCHAR szPathName[255];
- TCHAR path[255];
- memset(path, 0, 255);
- //转换成宽字符
- MultiByteToWideChar(CP_ACP, 0, name, -1, wszShareName, 255);
- #if 0
- res = NetShareGetInfo(NULL, (char *)wszShareName, 502, (LPBYTE *)&psi);
- #else
- res = NetShareGetInfo(NULL, wszShareName, 502, (LPBYTE *)&psi);
- #endif
- if (res == NERR_Success)
- {
- wsprintf((char*)szPathName, "%S ", psi->shi502_path);
- sRet = szPathName;
- sRet.TrimRight();
- }
- else
- {
- // CString str;
- //#if 0 // Jeff delete;
- // str.Format("转换路径失败-%d", res);
- // ::WriteTextLog(str);//Jeff delete,此处使用了系统域符号,其实完全没作用.
- //#else
- // str.Format("转换路径失败(\"%s\"),", sRet);
- // ErrorExit(str, res);
- //#endif
- }
- //释放缓冲
- NetApiBufferFree(psi);
- }
- CString GetModifyTime2(CString path)
- {
- try
- {
- CFileFind finder;
- BOOL bWorking = finder.FindFile(path);
- if (bWorking)
- {
- finder.FindNextFile();
- CTime tm;
- finder.GetLastWriteTime(tm);
- CString stime;
- stime.Format("%d%02d%02d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
- return stime;
- }
- return "";
- }
- catch (CFileException *e)
- {
- CString strError;
- strError.Format("2.读文件[%s]修改时间出错:%d", e->m_strFileName, e->m_lOsError);
- WriteTextLog(strError);
- e->Delete();
- }
- }
- CString GetModifyTime(CString path)
- {
- try
- {
- HANDLE hFile;
- WIN32_FIND_DATA wfd;
- // SYSTEMTIME systime;
- FILETIME localtime;
- CString stime; //输出时间
- memset(&wfd, 0, sizeof(wfd));
- if ((hFile = FindFirstFile(path, &wfd)) == INVALID_HANDLE_VALUE)
- return "";
- //ok,转换时间
- FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localtime);
- stime.Format("%16d%16d", localtime.dwHighDateTime, localtime.dwLowDateTime);
- return stime;
- }
- catch (CFileException *e)
- {
- CString strError;
- strError.Format("读文件[%s]修改时间出错:%d", e->m_strFileName, e->m_lOsError);
- WriteTextLog(strError);
- e->Delete();
- }
- }
- //--------------------------------------------------------------------------------
- // Jeff add 2014.06.23;
- // 函数:ErrorExit
- // 描述:
- // 参数:
- // lpszFunction:函数名;
- // dwError:错误码;
- //
- //--------------------------------------------------------------------------------
- void ErrorExit(CString strDescription, const DWORD &dwError)
- {
- #if 1
- LPVOID lpMsgBuf;
- BOOL fOk = FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dwError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR)&lpMsgBuf,
- 0, NULL);
- if (!fOk)
- {
- // Is it a network-related error?
- HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
- if (hDll != NULL)
- {
- FormatMessage(
- FORMAT_MESSAGE_FROM_HMODULE |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- hDll,
- dwError,
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (LPTSTR)&lpMsgBuf,
- 0,
- NULL);
- FreeLibrary(hDll);
- }
- }
- if (lpMsgBuf != NULL)
- {
- CString strDisplay;
- strDisplay.Format("%s.错误码=%d,Windows描述:%s", strDescription, dwError, (PCTSTR)LocalLock(lpMsgBuf));
- WriteTextLog(strDisplay);
- LocalFree(lpMsgBuf);
- }
- else
- {
- WriteTextLog(strDescription);
- }
- #else
- HLOCAL hlocal = NULL; // Buffer that gets the error message string
- // Get the error code's textual description
- BOOL fOk = FormatMessage(
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dwError,
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (PTSTR)&hlocal,
- 0,
- NULL);
- if (!fOk)
- {
- // Is it a network-related error?
- HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
- if (hDll != NULL)
- {
- FormatMessage(
- FORMAT_MESSAGE_FROM_HMODULE |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- hDll,
- dwError,
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (PTSTR)&hlocal,
- 0,
- NULL);
- FreeLibrary(hDll);
- }
- }
- if (hlocal != NULL)
- {
- CString strDisplay;
- strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
- WriteLogin(strDisplay);
- LocalFree(hlocal);
- }
- else
- {
- WriteLogin("Error number not found.");
- }
- #endif
- }
- void MyGetIPByName(CString &name)
- {
- CString strIP = name;
- name.Empty();
- DWORD dwServerIP = 0;
- HOSTENT *host = gethostbyname(strIP);
- struct in_addr addr;
- if (host != NULL)
- {
- for (int i = 0; host->h_addr_list[i] != NULL; i++)
- {
- memset(&addr, 0, sizeof(addr));
- memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
- dwServerIP = ntohl(addr.S_un.S_addr);
- }
- BYTE* pIP = (BYTE*)&dwServerIP;
- name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
- }
- }
- void EncryptFile3(CString path)
- {
- CFile fp;
- if (fp.Open(path, CFile::modeRead) == 0)return;
- DWORD leng = fp.GetLength();
- BYTE *pData = new BYTE[leng];
- fp.Read(pData, leng);
- fp.Close();
- EncryptFile2(pData, leng, "z9871234abcd");
- fp.Open(path, CFile::modeWrite);
- fp.Write(pData, leng);
- fp.Close();
- delete[]pData;
- }
- int FindArray(CStringArray *pArray, CString Str)
- {
- for (int i = 0; i < pArray->GetSize(); i++)
- {
- if (pArray->ElementAt(i) == Str)
- return i;
- }
- return -1;
- }
- BOOL isInnerIP(DWORD a_ip)
- {
- BOOL bValid = 0;
- if ((a_ip >> 24 == 0xa) || (a_ip >> 16 == 0xc0a8) || (a_ip >> 22 == 0x2b0))
- {
- bValid = 1;
- }
- return bValid;
- }
- BOOL CompareIP(CString name1, CString name2)
- {
- CStringArray g_serverarray1;
- CStringArray g_serverarray2;
- CString strIP = name1;
- DWORD dwServerIP = 0;
- HOSTENT *host = gethostbyname(strIP);
- struct in_addr addr;
- if (host != NULL)
- {
- for (int i = 0; host->h_addr_list[i] != NULL; i++)
- {
- memset(&addr, 0, sizeof(addr));
- memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
- dwServerIP = ntohl(addr.S_un.S_addr);
- BYTE* pIP = (BYTE*)&dwServerIP;
- strIP.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
- g_serverarray1.Add(strIP);
- }
- }
- while (g_serverarray1.GetSize() > 1)
- {
- CString name;
- BOOL bFind = 0;
- for (int i = 0; i < g_serverarray1.GetSize(); i++)
- {
- DWORD dwServerIP = inet_addr(g_serverarray1.ElementAt(i));
- dwServerIP = htonl(dwServerIP);//加了才正确
- if (isInnerIP(dwServerIP) == 0)
- {
- g_serverarray1.RemoveAt(i);
- bFind = 1;
- break;
- }
- }
- if (bFind == 0)
- {
- break;
- }
- }
- //////////
- {
- CString strIP = name2;
- DWORD dwServerIP = 0;
- HOSTENT *host = gethostbyname(strIP);
- struct in_addr addr;
- if (host != NULL)
- {
- for (int i = 0; host->h_addr_list[i] != NULL; i++)
- {
- memset(&addr, 0, sizeof(addr));
- memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
- dwServerIP = ntohl(addr.S_un.S_addr);
- BYTE* pIP = (BYTE*)&dwServerIP;
- strIP.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
- g_serverarray2.Add(strIP);
- }
- }
- while (g_serverarray2.GetSize() > 1)
- {
- CString name;
- BOOL bFind = 0;
- for (int i = 0; i < g_serverarray2.GetSize(); i++)
- {
- DWORD dwServerIP = inet_addr(g_serverarray2.ElementAt(i));
- dwServerIP = htonl(dwServerIP);//加了才正确
- if (isInnerIP(dwServerIP) == 0)
- {
- g_serverarray2.RemoveAt(i);
- bFind = 1;
- break;
- }
- }
- if (bFind == 0)
- {
- break;
- }
- }
- }
- if (g_serverarray1.GetSize() == 0 || g_serverarray2.GetSize() == 0)return 1;
- for (int i = 0; i < g_serverarray1.GetSize(); i++)
- {
- for (int j = 0; j < g_serverarray2.GetSize(); j++)
- {
- if (g_serverarray1.ElementAt(i) == g_serverarray2.ElementAt(j))return 0;
- }
- }
- return 1;
- }
- void DeleteDirectory(CString strDir)
- {
- try
- {
- // 首先删除文件及子文件夹
- CFileFind ff;
- BOOL bFound = ff.FindFile(strDir + "\\*", 0);
- while (bFound)
- {
- bFound = ff.FindNextFile();
- if (ff.GetFileName() == "." || ff.GetFileName() == "..")
- continue;
- // 去掉文件(夹)只读等属性
- SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
- if (ff.IsDirectory())
- { // 递归删除子文件夹
- DeleteDirectory(ff.GetFilePath());
- RemoveDirectory(ff.GetFilePath());
- }
- else
- {
- DeleteFile(ff.GetFilePath()); // Jeff:删除某文件夹下的所有子文件;
- }
- }
- ff.Close();
- // 然后删除该文件夹
- RemoveDirectory(strDir);
- }
- catch (...)
- {
- WriteTextLog("出错在函数deldir中");
- }
- // int SHFileOperation( LPSHFILEOPSTRUCT lpFileOp );
- }
- int GetLengthEx(CString str)
- {
- wchar_t wstr[500];
- int k = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, strlen(str), wstr, 500);
- return k;
- }
- CString GetFileSize(CString path)
- {
- CString str = "0";
- try
- {
- CFile fp;
- fp.Open(path, CFile::modeRead);
- str.Format("%d", fp.GetLength());
- fp.Close();
- }
- catch (...)
- {
- }
- return str;
- }
- void SortArray(CStringArray &datearray)
- {
- try
- {
- CString strtemp;
- float ftemp;
- int last = datearray.GetSize() - 1;
- bool sorted = true;
- do {
- sorted = true;
- for (int i = 0; i < last; i++)
- {
- if (datearray[i] < datearray[i + 1])
- {
- strtemp = datearray[i];
- datearray[i] = datearray[i + 1];
- datearray[i + 1] = strtemp;
- sorted = false;
- }
- }
- last--;
- } while (!sorted);
- }
- catch (...)
- {
- WriteTextLog("SortArray出错");
- }
- }
- int CheckPhoneType(CString phoneno)//移动:0 联通:1 电信:2 小灵通:3 未知:-1
- {
- int i = 0;
- if (phoneno.IsEmpty())return -1;
- for ( i = 0; i < phoneno.GetLength(); i++)
- {
- if (phoneno.GetAt(i) < '0' || phoneno.GetAt(i) > '9')return -1;
- }
- if (phoneno.GetAt(0) == '1')
- {
- if (phoneno.GetLength() != 11)
- return -1;
- int mobile[] = { 139, 138, 137, 136, 135, 134, 159, 158, 152, 151, 150, 157, 188, 187, 144, 182, 147, 183, 184 };
- int unicom[] = { 130, 131, 132, 155, 156, 186, 185 };
- int telecom[] = { 133, 153, 189, 180, 181 };
- for (i = 0; i < sizeof(mobile) / sizeof(int); i++)
- {
- if (mobile[i] == atoi(phoneno.Left(3)))
- {
- return 0;
- }
- }
- for (i = 0; i < sizeof(unicom) / sizeof(int); i++)
- {
- if (unicom[i] == atoi(phoneno.Left(3)))
- {
- return 1;
- }
- }
- for (i = 0; i < sizeof(telecom) / sizeof(int); i++)
- {
- if (telecom[i] == atoi(phoneno.Left(3)))
- {
- return 2;
- }
- }
- return -1;
- }
- /* else if(phoneno.GetAt (0)=='0')
- {
- if(phoneno.GetLength ()>=10 && phoneno.GetLength ()<=12)
- {
- return 3;
- }
- }*/
- return -1;
- }
- CTime GetTmFromStr(CString date)
- {
- try
- {
- CTime tm(atoi(date.Mid(0, 4)), atoi(date.Mid(5, 2)), atoi(date.Mid(8, 2)), 0, 0, 0);
- CString ss;
- ss.Format("%d-%d-%d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
- return tm;
- }
- catch (...)
- {
- date = "1980-01-01";
- CTime tm(atoi(date.Mid(0, 4)), atoi(date.Mid(5, 2)), atoi(date.Mid(8, 2)), 0, 0, 0);
- return tm;
- }
- }
- void ConvertToPrice(CString &str)
- {
- if (str.Find('.') == -1)return;
- str.TrimRight('0');
- str.TrimRight('.');
- }
- void SortArray(CArray<CStringArray, CStringArray>*List1array, int pos)
- {
- CString stemp;
- int last = List1array->GetSize() - 1;
- int ret = last;
- bool sorted = true;
- do
- {
- sorted = true;
- for (int i = 0; i < last; i++)
- {
- if (List1array->ElementAt(i).ElementAt(pos) < List1array->ElementAt(i + 1).ElementAt(pos))
- {
- for (int a = 0; a < List1array->ElementAt(i).GetSize(); a++)
- {
- stemp = List1array->ElementAt(i).ElementAt(a);
- List1array->ElementAt(i).SetAt(a, List1array->ElementAt(i + 1).ElementAt(a));
- List1array->ElementAt(i + 1).SetAt(a, stemp);
- }
- sorted = false;
- }
- }
- last--;
- } while (!sorted);
- }
- #endif
- /************************************************************************/
- /* 函数:WriteTextLog[7/28/2016 IT];
- /* 描述:写文本日志;
- /* 参数:;
- /* [IN] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void WriteTextLog(const TCHAR *format, ...)
- {
- try
- {
- static ThreadSection _critSection;
- AutoThreadSection aSection(&_critSection);
- // 解析出日志路径;
- TCHAR szlogpath[MAX_PATH] = {0};
- static TCHAR szModulePath[MAX_PATH] = {0};
- static TCHAR szFna[_MAX_DIR] = { 0 };
- if ( szModulePath[0] == _T('\0') )
- {
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szExt[_MAX_DIR] = { 0 };
- ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
- _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
- _tcscpy_s(szModulePath, szDrive);
- _tcscat_s(szModulePath, szDir);
- }
- _stprintf_s(szlogpath, _T("%s日志\\%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]").GetString());
- // 打开或创建文件;
- CStdioFile fp;
- if (PathFileExists(szlogpath))
- {
- if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
- {
- return;
- }
- fp.SeekToEnd();
- }
- else
- {
- if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
- return;
- }
- // 格式化前设置语言区域;
- TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
- _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
- // 格式化日志内容;
- va_list args = NULL;
- int len = 0;
- TCHAR *buffer = NULL;
- va_start( args, format );
- // _vscprintf doesn't count. terminating '\0'
- len = _vsctprintf_p( format, args );
- if ( len == -1 )
- {
- goto clear;
- }
- len++;
- buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
- _vstprintf_s( buffer, len, format, args ); // C4996
- // Note: vsprintf is deprecated; consider using vsprintf_s instead
- // 将日志内容输入到文件中;
- fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
- fp.WriteString(buffer);
- fp.WriteString(_T("\n"));
- // 关闭文件,释放资源并设置回原语言区域;
- free( buffer );
- clear:
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- fp.Close();
- }
- catch (CException *e)
- {
- e->ReportError();
- e->Delete();
- }
- }
|