12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685 |
- #include "StdAfx.h"
- #include "Global.h"
- #include "SoftKey.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_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;
- SoftKey ytSoftKey;
- 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);
- // -----------------------------------------------------//
- CHAR szFile[MAX_PATH + 1] = "";
- if (szPath != NULL && szIniName != NULL)
- wsprintf(szFile, "%s\\%s", szPath, szIniName);
- else
- wsprintf(szFile, "%s\\ServiceInfo.ini", g_ModulePath);
- TRACE("szFile =%s\n", szFile);
- OFSTRUCT ofStruct;
- OpenFile(szFile, &ofStruct, OF_EXIST);
- if (ERROR_FILE_NOT_FOUND == GetLastError())
- {
- return -1;
- }
- // 获取服务器端信息;
- GetPrivateProfileString("DatabaseInfo", "dbSource", "", g_szDBSource, MAX_PATH, szFile);
- g_dwDBServerPort = GetPrivateProfileInt("DatabaseInfo", "dbServerPort", 0, szFile);
- GetPrivateProfileString("DatabaseInfo", "dbAccount", "", g_szDBAccount, MAX_PATH, szFile);
- GetPrivateProfileString("DatabaseInfo", "dbPassWord", "", g_szDBPassWord, MAX_PATH, szFile);
- GetPrivateProfileString("DatabaseInfo", "dbName", "", g_szDBName, MAX_PATH, szFile);
- g_dwTCPPort = GetPrivateProfileInt("NetWorkInfo", "TCPChatPort", 5678, szFile);
- 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)
- {
- WriteLogin("新建连接时出错:" + 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)
- {
- WriteLogin(e->m_strError);
- WriteLogin("备份失败!");
- 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 (...)
- {
- WriteLogin("出错在函数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 (...)
- {
- WriteLogin("出错在函数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 WriteLogin(CString str)
- {
- MyLock lock("CDBServerDlgWriteLogin");
- try
- {
- CStdioFile fp;
- CString path = g_mainpath + "\\日志.txt";
- if (CheckFileExist(path))
- {
- fp.Open(path, CFile::modeWrite);
- int length = fp.GetLength();
- if (length > 5 * 1024 * 1024)
- {
- fp.Close();
- ::DeleteFile(path); // 删除备份的 日志.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();
- }
- 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 "";
- }
- #if JEFF_TEST_ON
- catch (CFileException *e)
- {
- CString strError;
- strError.Format("2.读文件[%s]修改时间出错:%d", e->m_strFileName, e->m_lOsError);
- WriteLogin(strError);
- e->Delete();
- }
- #else
- catch (...)
- {
- WriteLogin("出错");
- }
- #endif
- }
- 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;
- }
- #if JEFF_TEST_ON
- catch (CFileException *e)
- {
- CString strError;
- strError.Format("读文件[%s]修改时间出错:%d", e->m_strFileName, e->m_lOsError);
- WriteLogin(strError);
- e->Delete();
- }
- #else catch (...)
- {
- WriteLogin("出错");
- }
- #endif
- }
- //--------------------------------------------------------------------------------
- // 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));
- WriteLogin(strDisplay);
- LocalFree(lpMsgBuf);
- }
- else
- {
- WriteLogin(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 (...)
- {
- WriteLogin("出错在函数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 (...)
- {
- WriteLogin("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);
- }
- void DeCodePicCount(int &value1, int &value2)
- {
- // SoftKey ytSoftKey;
- int ret = 0;//ytSoftKey.Ini();
- if (ret != 0)
- {
- return;
- }
- int d[8]; double f[8];
- 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("");
- //运行自定义函数
- ret = ytSoftKey.decryptpiccount(&value1, &value2, &d[2], &d[3], &d[4], &d[5], &d[6], &d[7],
- &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
- s0, s1, s2, s3, s4, s5, s6, s7);
- if (ret != 0 && ret != -43)
- {
- exit(0);
- }
- }
- void DeCodelong1(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
- {
- // SoftKey ytSoftKey;
- int ret = 0;//ytSoftKey.Ini();
- if (ret != 0)
- {
- return;
- }
- double f[8];
- 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("");
- //运行自定义函数
- ret = ytSoftKey.decryptlong1(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
- &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
- s0, s1, s2, s3, s4, s5, s6, s7);
- if (ret != 0 && ret != -43)
- {
- exit(0);
- }
- }
- void DeCodelong2(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
- {
- // SoftKey ytSoftKey;
- int ret = 0;//ytSoftKey.Ini();
- if (ret != 0)
- {
- return;
- }
- double f[8];
- 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("");
- //运行自定义函数
- ret = ytSoftKey.decryptlong2(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
- &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
- s0, s1, s2, s3, s4, s5, s6, s7);
- if (ret != 0 && ret != -43)
- {
- exit(0);
- }
- }
- void DeCodelong3(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
- {
- // SoftKey ytSoftKey;
- int ret = 0;//ytSoftKey.Ini();
- if (ret != 0)
- {
- return;
- }
- double f[8];
- 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("");
- //运行自定义函数
- ret = ytSoftKey.decryptlong3(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
- &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
- s0, s1, s2, s3, s4, s5, s6, s7);
- if (ret != 0 && ret != -43)
- {
- exit(0);
- }
- }
- void DeCodelong4(int &value1, int &value2, int &value3, int &value4, int &value5, int &value6, int &value7, int &value8)
- {
- // SoftKey ytSoftKey;
- int ret = 0;//ytSoftKey.Ini();
- if (ret != 0)
- {
- exit(0);
- }
- double f[8];
- 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("");
- //运行自定义函数
- ret = ytSoftKey.decryptlong4(&value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8,
- &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7],
- s0, s1, s2, s3, s4, s5, s6, s7);
- if (ret != 0 && ret != -43)
- {
- exit(0);
- }
- }
- #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();
- }
- }
|