GpsSC.cpp
上传用户:yffx2008
上传日期:2014-10-12
资源大小:12414k
文件大小:18k
- // GpsSC.cpp : Defines the class behaviors for the application.
- //
- #include "stdafx.h"
- #include "GpsSC.h"
- #include "MainFrm.h"
- #include "ChildFrm.h"
- #include "GpsSCDoc.h"
- #include "GpsSCView.h"
- #include "SmscnumbSetDlg.h"
- #include "MainWTNumberSetDlg.h"
- #include "PassWordDlg.h"
- #include "Splash.h"
- #include "afxdb.h"
- #include "odbcinst.h"
- #include "GetHardDriveID.h"
- #include "Registry.h"
- #include "MyDlg.h"
- #include "IniFile.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CGpsSCApp
- CString str_GetTreeItemText; //全局变量(获取树形控件子项)
- extern BOOL GetHardDiskID(char szBuff[128]);
- BEGIN_MESSAGE_MAP(CGpsSCApp, CWinApp)
- //{{AFX_MSG_MAP(CGpsSCApp)
- ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
- ON_COMMAND(ID_SMSCNUMBERSETTING, OnSmscnumbersetting)
- ON_UPDATE_COMMAND_UI(ID_SMSCNUMBERSETTING, OnUpdateSmscnumbersetting)
- //}}AFX_MSG_MAP
- // Standard file based document commands
- ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
- ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CGpsSCApp construction
- CGpsSCApp::CGpsSCApp()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- strPassWord = "";
- password = FALSE;
- str_GetTreeItemText.Empty();
- }
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CGpsSCApp object
- CGpsSCApp theApp;
- /////////////////////////////////////////////////////////////////////////////
- // CGpsSCApp initialization
- extern BOOL GetHardDiskID(char szBuff[128]);
- BOOL CGpsSCApp::InitInstance()
- {
- /* //注册用
- char szBuff[20]="";
- CString strBuff=GetCDiskID();
- for(int nIndex=0;nIndex<8;nIndex++)
- {
- szBuff[nIndex]=strBuff.GetAt(nIndex);
- }
- szBuff[8]=' ';
- if(RegiesterNow(szBuff)==false)
- return false;
- */
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // CG: The following block was added by the Splash Screen component.
-
- //------------------注册DSN------------------------------
- CString sPath;
- char CurrPath[500];
- long FatLen = 0;
- long j;
- for(j = 0; j < 500; j++) {
- CurrPath[j] = 0;
- }
- //获取工程目录
- ::GetCurrentDirectory(500,CurrPath);
- sPath = CurrPath;
- m_strCurrPath = sPath;
- m_strCurrPath += "\Log\";
- CString lpszFile = sPath + "\DataBase" + "\CenterDataBase.mdb";
-
- if (!IsFileExist(lpszFile))
- {
- AfxMessageBox("数据库文件路径不对!");
- return FALSE;
- }
- SetFileReadOnly(lpszFile,false);
- char* szDesc;
- int mlen;
- szDesc=new char[256];
- sprintf(szDesc,"DSN=%s? DESCRIPTION=TOC support source? DBQ=%s? FIL=MicrosoftAccess? DEFAULTDIR=%s?? ","CenterDataBase",lpszFile,sPath);
- mlen = strlen(szDesc);
- for (int i=0; i<mlen; i++)
- {
- if (szDesc[i] == '?')
- szDesc[i] = ' ';
- }
- CDatabase m_DB;
- if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb) ",(LPCSTR)szDesc))
- AfxMessageBox("SQLConfigDataSource Failed");
- try
- {
- CString strConnect;
- strConnect.Format("DSN=CenterDataBase;");
- if(!m_DB.OpenEx(strConnect,CDatabase::useCursorLib))
- {
- AfxMessageBox("不能连接数据库!");
- return FALSE ;
- }
- }
- catch(CDBException *pE)
- {
- AfxMessageBox(pE->ReportError());
- pE->Delete();
- return FALSE;
- }
- //-------------------------------------------------------
- CString FileName = m_strCurrPath+"log.ini";
- SetFileStatus(FileName,false);
- ReadLog(); //读日志
-
- {
- CCommandLineInfo cmdInfo;
- ParseCommandLine(cmdInfo);
- CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
-
-
- }
-
- if (!AfxSocketInit())
- {
- AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
- return FALSE;
- }
- AfxEnableControlContainer();
- // Standard initialization
- // If you are not using these features and wish to reduce the size
- // of your final executable, you should remove from the following
- // the specific initialization routines you do not need.
- #ifdef _AFXDLL
- Enable3dControls(); // Call this when using MFC in a shared DLL
- #else
- Enable3dControlsStatic(); // Call this when linking to MFC statically
- #endif
- // Change the registry key under which our settings are stored.
- // TODO: You should modify this string to be something appropriate
- // such as the name of your company or organization.
- SetRegistryKey(_T("Local AppWizard-Generated Applications"));
- LoadStdProfileSettings(); // Load standard INI file options (including MRU)
- // Register the application's document templates. Document templates
- // serve as the connection between documents, frame windows and views.
- CMultiDocTemplate* pDocTemplate;
- pDocTemplate = new CMultiDocTemplate(
- IDR_GPSSCTYPE,
- RUNTIME_CLASS(CGpsSCDoc),
- RUNTIME_CLASS(CChildFrame), // custom MDI child frame
- RUNTIME_CLASS(CGpsSCView));
- AddDocTemplate(pDocTemplate);
- // create main MDI Frame window
- CMainFrame* pMainFrame = new CMainFrame;
- if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
- return FALSE;
- m_pMainWnd = pMainFrame;
- // Parse command line for standard shell commands, DDE, file open
- CCommandLineInfo cmdInfo;
- ParseCommandLine(cmdInfo);
- // Dispatch commands specified on the command line
- if (!ProcessShellCommand(cmdInfo))
- return FALSE;
- m_bUserType = true;
- CPassWordDlg dlg;
- if(dlg.DoModal() != IDOK)
- return FALSE;
-
- // The main window has been initialized, so show and update it.
- pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
- pMainFrame->UpdateWindow();
- return TRUE;
- }
- //文件是否存在
- long CGpsSCApp::IsFileExist(CString FileName)
- {
- long Flength = 0;
- CFile f;
- if(f.Open(FileName,CFile::modeRead,NULL)) {
- Flength = f.GetLength();
- f.Close();
- }
- return Flength;
- }
- void CGpsSCApp::SetFileReadOnly(CString FileName,bool ReadOnly)
- {
- if(!IsFileExist(FileName)) return;
- CFileStatus fStatus;
- CFile f;
- if(!CFile::GetStatus(FileName,fStatus)) {
- AfxMessageBox("出错!");
- }
- if(ReadOnly) {
- fStatus.m_attribute = fStatus.m_attribute | 0x01;
- }
- else {
- fStatus.m_attribute = fStatus.m_attribute & 0xfe;
- }
- CFile::SetStatus(FileName,fStatus);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- // No message handlers
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // App command to run the dialog
- void CGpsSCApp::OnAppAbout()
- {
- CAboutDlg aboutDlg;
- aboutDlg.DoModal();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CGpsSCApp message handlers
- //////////////////////////////////////////////////////////////////////////////
- //名称:ExitInstance
- //功能:退出时清除工作
- /////////////////////////////////////////////////////////////////////////////
- int CGpsSCApp::ExitInstance()
- {
- SaveLog(); //保存日志
- CString FileName = m_strCurrPath+"log.ini";
- SetFileStatus(FileName,true);
- return CWinApp::ExitInstance();
- }
- void CGpsSCApp::OnSmscnumbersetting()
- {
- CSmscnumbSetDlg smnumdlg;
- smnumdlg.m_sSMSCNumber = m_sSMNum;
- if(smnumdlg.DoModal()==IDOK)
- {
- m_sSMNum = smnumdlg.m_sSMSCNumber;
- }
- m_sSMNum = smnumdlg.m_sSMSCNumber;
- }
- void CGpsSCApp::OnUpdateSmscnumbersetting(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- }
- //create the password dialog
- BOOL CGpsSCApp::CreatePassWordDlg()
- {
- CString strpass = GetProjectInDir();
- strpass += "sysinit.ini";
-
- CString str1 = GetPassWord(strpass);
- CPassWordDlg dlg;
- if(dlg.DoModal() == IDOK)
- {
- if(dlg.m_strPassWord == str1)
- {
- return TRUE;
- }
- else
- return FALSE;
- }
- else
- return FALSE;
- }
- //get the password
- CString CGpsSCApp::GetPassWord(const CString strpassderectory)
- {
-
- CFile f;
- int m_lFileBufLength;
- char * m_pFileBuf = NULL;
-
- if(!f.Open(strpassderectory,CFile::modeRead,NULL))
- {
- AfxMessageBox("open files error!");
- return "nihao";
- }
- f.SeekToBegin();
- m_lFileBufLength = f.GetLength();
-
- m_pFileBuf = new char[m_lFileBufLength];
- f.SeekToBegin();
- f.Read(m_pFileBuf,m_lFileBufLength);
- f.Close();
- m_pFileBuf[m_lFileBufLength] = ' ';
- int a = strlen(m_pFileBuf);
- strPassWord = m_pFileBuf;
- //delete m_pFileBuf;
- return strPassWord;
- }
- //get the current directory
- CString CGpsSCApp::GetProjectInDir()
- {
- CString Path;
- char Name[MAX_PATH+1];
- int Len,i;
- TCHAR chr = 0;
- GetModuleFileName(NULL,Name,MAX_PATH);
- Path = Name;
- Len = Path.GetLength();
- for(i = Len - 1; i >= 0; i--) {
- chr = Path.GetAt(i);
- if(chr == '\') break;
- }
- i++;
- Path = Path.Left(i);
- return Path;
- }
- BOOL CGpsSCApp::PreTranslateMessage(MSG* pMsg)
- {
- // CG: The following lines were added by the Splash Screen component.
if (CSplashWnd::PreTranslateAppMessage(pMsg))
return TRUE;
return CWinApp::PreTranslateMessage(pMsg);
- }
- bool CGpsSCApp::RegiesterNow(char szBuff[20])
- {
- CString strPassWord="",strShowRegID="";
- CString strWriteToReg="",strReadFromReg="";
- //生成写入注册表的
- //前四个
- char cArr1[10]="",cArr2[10]="";
- for(int i=0;i<4;i++)
- {
- cArr1[2*i]=(szBuff[i]-'0')/10+'0';
- cArr1[2*i+1]=(szBuff[i]-'0')%10+'0';
- }
- cArr1[8]=' ';
- //后四个
- for(i=0;i<4;i++)
- {
- cArr2[2*i]=(szBuff[i+4]-'0')/10+'0';
- cArr2[2*i+1]=(szBuff[i+4]-'0')%10+'0';
- }
- cArr2[8]=' ';
- strWriteToReg=cArr1;strWriteToReg+="_";strWriteToReg+=cArr2;
- // AfxMessageBox("写入注册表的"+strWriteToReg);
- //生成显示的注册号
- //前四个
- unsigned long ulPassWord;
- ulPassWord=atoi(cArr1);
- if(ulPassWord==0)
- ulPassWord=17598134;
- ulPassWord^=12345678;
- itoa(ulPassWord,cArr1,10);
- CString strArr1=cArr1;
- strArr1=DealcArrTo8(strArr1);
- //后四个
- ulPassWord=atoi(cArr2);
- if(ulPassWord==0)
- ulPassWord=17598134;
- ulPassWord^=12345678;
- itoa(ulPassWord,cArr2,10);
- CString strArr2=cArr2;
- strArr2=DealcArrTo8(strArr2);
- for(i=0;i<4;i++)
- strShowRegID+=strArr1.GetAt(i);
- strShowRegID+="_";
- for(i=4;i<8;i++)
- strShowRegID+=strArr1.GetAt(i);
- strShowRegID+="_";
- for(i=0;i<4;i++)
- strShowRegID+=strArr2.GetAt(i);
- strShowRegID+="_";
- for(i=4;i<8;i++)
- strShowRegID+=strArr2.GetAt(i);
- // AfxMessageBox("注册号"+strShowRegID);
- //从注册表中拿值
- /* CRegistry reg;
- CString s;
- reg.Open ("Software\Gps");
- reg.Read ("reg",&s);
- */
- CString s;
- s=ReadFile();
- //判断处理后的硬盘ID与注册表中的值是否相等
- if(strWriteToReg!=s)//第一次比较
- {
- AfxMessageBox("你没有注册!!rn请注册!!");
- int nTryNu=0;
- DoAgain:
- for(;nTryNu<4;)
- {
- nTryNu++;
- CMyDlg dlg;
- dlg.m_strDiskID=strShowRegID;//显示出来的
- int nResponse = dlg.DoModal();
- if (nResponse == IDOK)
- {
- strPassWord="";
- strPassWord=dlg.m_strDealID;
- // AfxMessageBox(strPassWord);
- }
- else if (nResponse == IDCANCEL)
- {
- return false;
- }
- if(strPassWord.GetLength()<19)
- {
- AfxMessageBox("密码长度不够!!rn请输入完整的密码!!");
- if(nTryNu>=3)
- return false;
- goto DoAgain;
- }
- //在内部得到的密码
- CString strPassWordInGps="";
- for(int i=0;i<4;i++)
- strPassWordInGps+=((strShowRegID.GetAt(i)-'0')^(strShowRegID.GetAt((i+1)%4)-'0'))%10+'0';
- strPassWordInGps+='_';
- for(i=0;i<4;i++)
- strPassWordInGps+=((strShowRegID.GetAt(i+5)-'0')^(strShowRegID.GetAt((i+1)%4+5)-'0'))%10+'0';
- strPassWordInGps+='_';
- for(i=0;i<4;i++)
- strPassWordInGps+=((strShowRegID.GetAt(i+5+5)-'0')^(strShowRegID.GetAt((i+1)%4+5+5)-'0'))%10+'0';
- strPassWordInGps+='_';
- for(i=0;i<3;i++)
- strPassWordInGps+=((strShowRegID.GetAt(i+5+5+5)-'0')^(strShowRegID.GetAt((i+1)%4+5+5+5)-'0'))%10+'0';
- strPassWordInGps+=((strShowRegID.GetAt(18)-'0')^5)%10+'0';
- if(strPassWord.Left(19)!=strPassWordInGps.Left(19))//第二次比较
- {
- if(nTryNu<3)
- AfxMessageBox("输入有错!请再输入!");
- if(nTryNu>=3)
- {
- AfxMessageBox("你己输入三次有误!");
- return false;
- }
- }
- else
- {
- /* reg.Open ("Software ");
- reg.CreateKey ("Gps ");
- reg.Write ("reg",strWriteToReg);
- reg.Read ("reg",&strReadFromReg);
- */
- WriteFile(strWriteToReg);
- strReadFromReg=ReadFile();
- if(strWriteToReg==strReadFromReg)
- AfxMessageBox("你己成功注册!!rn感谢你使用此软件!!");
- break;
- }
- }
- }
- return true;
-
- }
- CString CGpsSCApp::DealcArrTo8(CString strArr4)
- {
- int nLength=strArr4.GetLength();
- switch(nLength)
- {
- case 0:strArr4="00000000";break;
- case 1:strArr4="0000000"+strArr4;break;
- case 2:strArr4="000000"+strArr4;break;
- case 3:strArr4="00000"+strArr4;break;
- case 4:strArr4="0000"+strArr4;break;
- case 5:strArr4="000"+strArr4;break;
- case 6:strArr4="00"+strArr4;break;
- case 7:strArr4="0"+strArr4;break;
- }
- return strArr4;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- CString CGpsSCApp::GetCDiskID()
- {
- //获得驱动器序列号
- LPCTSTR lpRootPathName = "c:\";
- LPTSTR lpVolumeNameBuffer=new char[12];
- DWORD nVolumeNameSize=12;
- DWORD VolumeSerialNumber;
- DWORD MaximumComponentLength;
- DWORD FileSystemFlags;
- LPTSTR lpFileSystemNameBuffer=new char[10];
- DWORD nFileSystemNameSize=10;
- GetVolumeInformation(lpRootPathName,
- lpVolumeNameBuffer, nVolumeNameSize,
- &VolumeSerialNumber,
- &MaximumComponentLength,
- &FileSystemFlags,
- lpFileSystemNameBuffer, nFileSystemNameSize);
- //显示驱动器序列号
- CString str;
- str.Format("%x",VolumeSerialNumber);
- // AfxMessageBox(str);
- return str;
- }
- CString CGpsSCApp::GetMyPath()
- {
- CString strPath;
- GetModuleFileName(NULL,strPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
- strPath.ReleaseBuffer();
- return strPath;
- }
- void CGpsSCApp::WriteFile(CString Info)
- {
- CFile f;
- CString strSaveFilePath="";
- strSaveFilePath= GetMyPath();
- strSaveFilePath=strSaveFilePath.Left(strSaveFilePath.GetLength()-4);
- strSaveFilePath += ".txt";
- if(f.Open(strSaveFilePath,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite,NULL))
- {
- f.SeekToBegin( );
- f.Write(Info,Info.GetLength());
- }
- f.Close();
- }
- CString CGpsSCApp::ReadFile()
- {
- CFile f;
- char m_cpFileBuf[30];
- int nFileLength;
- CString strSaveFilePath="",strDataGet="";
- strSaveFilePath= GetMyPath();
- strSaveFilePath=strSaveFilePath.Left(strSaveFilePath.GetLength()-4);
- strSaveFilePath += ".txt";
- if(!f.Open(strSaveFilePath,CFile::modeRead,NULL)) {
- // AfxMessageBox("打开文件出错!");
- return "##";
- }
- f.SeekToBegin();
- nFileLength = f.GetLength();
- // nFileLength=9;
- f.SeekToBegin();
- f.Read(m_cpFileBuf,nFileLength);
- f.Close();
- m_cpFileBuf[nFileLength]=' ';
- strDataGet=m_cpFileBuf;
- return strDataGet;
- }
- void CGpsSCApp::SaveLog()
- {
- CIniFile IniFile;
- IniFile.SetName("Log");
- IniFile.SetPath(m_strCurrPath);
- BOOL bExist=IniFile.OpenIniFileForWrite();
- if(bExist)
- {
- IniFile.WriteSection("LoginUser");
- IniFile.WriteItemString("UserData",m_strLoginUser);
- IniFile.WriteString("n");
- IniFile.WriteSection("SMSCNumber");
- IniFile.WriteItemString("SMSCData",m_sSMNum);
- IniFile.WriteString("n");
- IniFile.WriteSection("Port");
- IniFile.WriteItemString("PortData",m_strPort);
- IniFile.WriteString("n");
- IniFile.WriteSection("DataBits");
- IniFile.WriteItemString("DataBistsData",m_strDataBits);
- IniFile.WriteString("n");
- IniFile.WriteSection("Parity");
- IniFile.WriteItemInt("ParityData",m_nParity);
- IniFile.WriteString("n");
- IniFile.WriteSection("StopBit");
- IniFile.WriteItemInt("StopBitData",m_nStopBit);
- IniFile.WriteString("n");
- IniFile.WriteSection("Baud");
- IniFile.WriteItemString("BaudData",m_strBaud);
- IniFile.CloseIniFile();
- }
- }
- void CGpsSCApp::ReadLog()
- {
- CIniFile IniFile;
- IniFile.SetName("Log");
- IniFile.SetPath(m_strCurrPath);
- BOOL bExist=IniFile.OpenIniFileForRead();
- if(bExist)
- {
- if(!IniFile.GetItemString("LoginUser","UserData",m_strLoginUser))
- m_strLoginUser = "管理员";
- if(!IniFile.GetItemString("SMSCNumber","SMSCData",m_sSMNum))
- m_sSMNum = "13800757500";
- if(!IniFile.GetItemString("Port","PortData",m_strPort))
- m_strPort = "COM1";
- if(!IniFile.GetItemString("DataBits","DataBistsData",m_strDataBits))
- m_strDataBits = "8";
- if(!IniFile.GetItemInt("Parity","ParityData",m_nParity))
- m_nParity = 0;
- if(!IniFile.GetItemInt("StopBit","StopBitData",m_nStopBit))
- m_nStopBit = 1;
- if(!IniFile.GetItemString("Baud","BaudData",m_strBaud))
- m_strBaud ="9600";
- IniFile.CloseIniFile();
- }
- else
- {
- m_strLoginUser = "管理员";
- m_sSMNum = "13800757500";
- m_strPort = "COM1";
- m_strDataBits = "8";
- m_nParity = 0;
- m_nStopBit = 0;
- m_strBaud = "9600";
- }
- }
- void CGpsSCApp::SetFileStatus(CString FileName,bool ReadOnly)
- {
- if(!IsFileExist(FileName)) return;
-
- CFileStatus fStatus;
- CFile f;
- if(!CFile::GetStatus(FileName,fStatus)) {
- AfxMessageBox("出错!");
- }
- if(ReadOnly) {
- fStatus.m_attribute = fStatus.m_attribute | 0x01;
- }
- else {
- fStatus.m_attribute = fStatus.m_attribute & 0xfe;
- }
- CFile::SetStatus(FileName,fStatus);
- }