trfAgent.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:6k
- #include "stdafx.h"
- #include "trfAgent.h"
- #include "MainFrm.h"
- #include "trfAgentDoc.h"
- #include "trfAgentView.h"
- extern CString _LoadString(UINT uResID)
- {
- TCHAR lpBuf[1024];
- memset(lpBuf, 0x0, sizeofarray(lpBuf));
- ::LoadString(::AfxGetInstanceHandle(), uResID, lpBuf, sizeofarray(lpBuf));
- return CString((TCHAR*)(&lpBuf[0]));
- }
- BEGIN_MESSAGE_MAP(CTrfAgentApp, CWinApp)
- //{{AFX_MSG_MAP(CTrfAgentApp)
- ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
- //}}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()
- CTrfAgentApp::CTrfAgentApp(){}
- CTrfAgentApp theApp;
- BOOL CTrfAgentApp::InitInstance()
- {
- ///
- AfxEnableControlContainer();
- // Initialize OLE 2.0 libraries
- if (!::AfxOleInit())
- {
- MSGBOX_ERROR(_LoadString(IDS_INITOLELIBFAILED).GetBuffer(0));
- return FALSE;
- }
- // support shell "send-to" right menu command.
- PrepareSendFilesFromCommand(this->m_lpCmdLine);
- //@1
- if(this->m_oAppInstance.Exists())
- {
- NotifySysWndToLoadShellFiles(NULL);
- TRACE0("One instance has already exist...n");
- return FALSE;
- }
- if(!this->m_oAppInstance.InitApp())
- {
- TRACE0("Failed to initialize the system environment...n");
- return FALSE;
- }
- //@2
- if(!m_oCSHelper.Start())
- {
- TRACE0("Failed to start the Client-Server Helper instance...n");
- return FALSE;
- }
- #ifdef _AFXDLL
- Enable3dControls();
- #else
- Enable3dControlsStatic();
- #endif
- SetRegistryKey(_T("trfAgent v.10"));
- LoadStdProfileSettings();
- CSingleDocTemplate* pDocTemplate;
- pDocTemplate = new CSingleDocTemplate(
- IDR_MAINFRAME,
- RUNTIME_CLASS(CTrfAgentDoc),
- RUNTIME_CLASS(CMainFrame),
- RUNTIME_CLASS(CTrfAgentView));
- AddDocTemplate(pDocTemplate);
- CCommandLineInfo cmdInfo;
- ParseCommandLine(cmdInfo);
- if (!ProcessShellCommand(cmdInfo))
- {
- TRACE0("Failed to process shell command...n");
- return FALSE;
- }
- //record system initializing log.
- CString strlog;
- strlog.Format( _LoadString(IDS_LOG_AGINFO).GetBuffer(0),
- m_oCSHelper.GetClientID(), m_oCSHelper.GetClientName().c_str());
- m_pMainWnd->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
- strlog.Empty();
- strlog.Format(_LoadString(IDS_LOG_LOCALADDR).GetBuffer(0),
- CXNetAddress::GetLocalHostName().c_str(),
- CXNetAddress::GetLocalHostIp().c_str());
- m_pMainWnd->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
- if(!this->m_oCCHelper.Start())
- {
- TRACE0("Failed to start Agent - Agent helper...n");
- return FALSE;
- }
- strlog.Empty();
- strlog.Format(_LoadString(IDS_PEERSVROK).GetBuffer(0));
- m_pMainWnd->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
- //////////////////////////////////////////////////////////////////////////
- if(NULL != this->m_lpCmdLine && NULL != this->m_lpCmdLine[0])
- {
- NotifySysWndToLoadShellFiles(m_pMainWnd->GetSafeHwnd());
- }
- //////////////////////////////////////////////////////////////////////////
-
- m_pMainWnd->ShowWindow(SW_SHOW);
- m_pMainWnd->UpdateWindow();
- //////////////////////////////////////////////////////////////////////////
- return TRUE;
- }
- int CTrfAgentApp::ExitInstance()
- {
- this->m_oCCHelper.Stop();
-
- this->m_oCSHelper.Stop();
-
- this->m_oAppInstance.UnInitApp();
- return CWinApp::ExitInstance();
- }
- void CTrfAgentApp::PrepareSendFilesFromCommand(LPCSTR lpszCommandLine)
- {
- CString commandline = lpszCommandLine;
- commandline += " "; //add end token.
- if(NULL == lpszCommandLine || commandline.IsEmpty()) return ;
- CRegister regkey;
-
- if(!regkey.Open(HKEY_CURRENT_USER, REG_SSHLSNDTOFLEDIR)) return ;
-
- CString filename;
- int count = 0, start = 0, quotpare = 0, len = 0;
-
- len = commandline.GetLength();
- for(int i = 0; i < len; ++i)
- {
- filename.Empty();
-
- //parse the filename.
-
- TCHAR ch = commandline.GetAt(i);
-
- if(ch == 0x0022)
- {
- //is quotations pare ok ?
- if(quotpare == 1)
- {
- filename = commandline.Mid(start + 1, i - start - 1);
- quotpare = 0;
- start = i + 1;
- if(filename.IsEmpty()) continue;
- }
- else
- {
- start = i;
- quotpare++;
- continue;
- }
- }
- else if(ch == 0x0020)
- {
- //is in middle of two quotations ?
- if(quotpare == 1)
- {
- continue;
- }
- else
- {
- filename = commandline.Mid(start + 1, i - start - 1);
- start = i + 1;
- if(filename.IsEmpty()) continue;
- }
- }
-
- //write into register.
-
- if(!filename.IsEmpty() && FileExists(filename.GetBuffer(0)))
- {
- CString keyname;
- keyname.Format("%d", count);
- regkey.Write_String_Value(keyname.GetBuffer(0), filename.GetBuffer(0));
-
- ++count;
- }
- }
- regkey.Write_DWord_Value(REG_SSHLSNDTOFLECNT, count);
- }
- void CTrfAgentApp::NotifySysWndToLoadShellFiles(HWND hwnd)
- {
- if(NULL == hwnd)
- {
- hwnd = ::FindWindowEx(NULL, NULL, NULL, _LoadString(IDR_MAINFRAME));
- }
- if(hwnd != NULL)
- {
- ::SendMessage(hwnd, UWM_SHELLSNDTONOTIFY, 0, 0);
- ::PostMessage(hwnd, UWM_SYSTRAY, 0, WM_LBUTTONDBLCLK);
- }
- }
- /*
- * CAboutDlg : System About Dialog.
- */
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- CButton m_btnOK;
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX);
- //}}AFX_VIRTUAL
- protected:
- //{{AFX_MSG(CAboutDlg)
- virtual BOOL OnInitDialog();
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- DDX_Control(pDX, IDOK, m_btnOK);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- void CTrfAgentApp::OnAppAbout()
- {
- CAboutDlg aboutDlg;
- aboutDlg.DoModal();
- }
- BOOL CAboutDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- this->CenterWindow();
- this->m_btnOK.SetWindowText(_LoadString(IDS_BTNOK).GetBuffer(0));
- return TRUE;
- }