trfAgent.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:6k
源码类别:

网格计算

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "trfAgent.h"
  3. #include "MainFrm.h"
  4. #include "trfAgentDoc.h"
  5. #include "trfAgentView.h"
  6. extern CString _LoadString(UINT uResID)
  7. {
  8. TCHAR lpBuf[1024];
  9. memset(lpBuf, 0x0, sizeofarray(lpBuf));
  10. ::LoadString(::AfxGetInstanceHandle(), uResID, lpBuf, sizeofarray(lpBuf));
  11. return CString((TCHAR*)(&lpBuf[0]));
  12. }
  13. BEGIN_MESSAGE_MAP(CTrfAgentApp, CWinApp)
  14. //{{AFX_MSG_MAP(CTrfAgentApp)
  15. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  16. //}}AFX_MSG_MAP
  17. // Standard file based document commands
  18. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  19. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  20. END_MESSAGE_MAP()
  21. CTrfAgentApp::CTrfAgentApp(){}
  22. CTrfAgentApp theApp;
  23. BOOL CTrfAgentApp::InitInstance()
  24. {
  25. ///
  26. AfxEnableControlContainer();
  27. // Initialize OLE 2.0 libraries
  28. if (!::AfxOleInit())
  29. {
  30. MSGBOX_ERROR(_LoadString(IDS_INITOLELIBFAILED).GetBuffer(0));
  31. return FALSE;
  32. }
  33. // support shell "send-to" right menu command.
  34. PrepareSendFilesFromCommand(this->m_lpCmdLine);
  35. //@1
  36. if(this->m_oAppInstance.Exists())
  37. {
  38. NotifySysWndToLoadShellFiles(NULL);
  39. TRACE0("One instance has already exist...n");
  40. return FALSE;
  41. }
  42. if(!this->m_oAppInstance.InitApp())
  43. {
  44. TRACE0("Failed to initialize the system environment...n");
  45. return FALSE;
  46. }
  47. //@2
  48. if(!m_oCSHelper.Start())
  49. {
  50. TRACE0("Failed to start the Client-Server Helper instance...n");
  51. return FALSE;
  52. }
  53. #ifdef _AFXDLL
  54. Enable3dControls();
  55. #else
  56. Enable3dControlsStatic();
  57. #endif
  58. SetRegistryKey(_T("trfAgent v.10"));
  59. LoadStdProfileSettings();
  60. CSingleDocTemplate* pDocTemplate;
  61. pDocTemplate = new CSingleDocTemplate(
  62. IDR_MAINFRAME,
  63. RUNTIME_CLASS(CTrfAgentDoc),
  64. RUNTIME_CLASS(CMainFrame),
  65. RUNTIME_CLASS(CTrfAgentView));
  66. AddDocTemplate(pDocTemplate);
  67. CCommandLineInfo cmdInfo;
  68. ParseCommandLine(cmdInfo);
  69. if (!ProcessShellCommand(cmdInfo))
  70. {
  71. TRACE0("Failed to process shell command...n");
  72. return FALSE;
  73. }
  74. //record system initializing log.
  75. CString strlog;
  76. strlog.Format( _LoadString(IDS_LOG_AGINFO).GetBuffer(0), 
  77. m_oCSHelper.GetClientID(), m_oCSHelper.GetClientName().c_str());
  78. m_pMainWnd->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
  79. strlog.Empty();
  80. strlog.Format(_LoadString(IDS_LOG_LOCALADDR).GetBuffer(0), 
  81. CXNetAddress::GetLocalHostName().c_str(), 
  82. CXNetAddress::GetLocalHostIp().c_str());
  83. m_pMainWnd->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
  84. if(!this->m_oCCHelper.Start())
  85. {
  86. TRACE0("Failed to start Agent - Agent helper...n");
  87. return FALSE;
  88. }
  89. strlog.Empty();
  90. strlog.Format(_LoadString(IDS_PEERSVROK).GetBuffer(0));
  91. m_pMainWnd->SendMessage(UWM_UPDATESYSLOGINF, 0, (LPARAM)strlog.GetBuffer(0));
  92. //////////////////////////////////////////////////////////////////////////
  93. if(NULL != this->m_lpCmdLine && NULL != this->m_lpCmdLine[0])
  94. {
  95. NotifySysWndToLoadShellFiles(m_pMainWnd->GetSafeHwnd());
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. m_pMainWnd->ShowWindow(SW_SHOW);
  99. m_pMainWnd->UpdateWindow();
  100. //////////////////////////////////////////////////////////////////////////
  101. return TRUE;
  102. }
  103. int CTrfAgentApp::ExitInstance() 
  104. {
  105. this->m_oCCHelper.Stop();
  106. this->m_oCSHelper.Stop();
  107. this->m_oAppInstance.UnInitApp();
  108. return CWinApp::ExitInstance();
  109. }
  110. void CTrfAgentApp::PrepareSendFilesFromCommand(LPCSTR lpszCommandLine)
  111. {
  112. CString commandline = lpszCommandLine;
  113. commandline += " "; //add end token.
  114. if(NULL == lpszCommandLine || commandline.IsEmpty())  return ;
  115. CRegister regkey;
  116. if(!regkey.Open(HKEY_CURRENT_USER, REG_SSHLSNDTOFLEDIR)) return ;
  117. CString filename;
  118. int count = 0, start = 0, quotpare = 0, len = 0;
  119. len = commandline.GetLength();
  120. for(int i = 0; i < len; ++i)
  121. {
  122. filename.Empty();
  123. //parse the filename.
  124. TCHAR ch = commandline.GetAt(i);
  125. if(ch == 0x0022)
  126. {
  127. //is quotations pare ok ?
  128. if(quotpare == 1)
  129. {
  130. filename = commandline.Mid(start + 1, i - start - 1);
  131. quotpare = 0;
  132. start = i + 1;
  133. if(filename.IsEmpty()) continue;
  134. }
  135. else
  136. {
  137. start = i;
  138. quotpare++;
  139. continue;
  140. }
  141. }
  142. else if(ch == 0x0020)
  143. {
  144. //is in middle of two quotations ?
  145. if(quotpare == 1)
  146. {
  147. continue;
  148. }
  149. else
  150. {
  151. filename = commandline.Mid(start + 1, i - start - 1);
  152. start = i + 1;
  153. if(filename.IsEmpty()) continue;
  154. }
  155. }
  156. //write into register.
  157. if(!filename.IsEmpty() && FileExists(filename.GetBuffer(0)))
  158. {
  159. CString keyname;
  160. keyname.Format("%d", count);
  161. regkey.Write_String_Value(keyname.GetBuffer(0), filename.GetBuffer(0));
  162. ++count;
  163. }
  164. }
  165. regkey.Write_DWord_Value(REG_SSHLSNDTOFLECNT, count);
  166. }
  167. void CTrfAgentApp::NotifySysWndToLoadShellFiles(HWND hwnd)
  168. {
  169. if(NULL == hwnd)
  170. {
  171. hwnd = ::FindWindowEx(NULL, NULL, NULL, _LoadString(IDR_MAINFRAME));
  172. }
  173. if(hwnd != NULL)
  174. {
  175. ::SendMessage(hwnd, UWM_SHELLSNDTONOTIFY, 0, 0);
  176. ::PostMessage(hwnd, UWM_SYSTRAY, 0, WM_LBUTTONDBLCLK);
  177. }
  178. }
  179. /*
  180.  * CAboutDlg : System About Dialog.
  181.  */
  182. class CAboutDlg : public CDialog
  183. {
  184. public:
  185. CAboutDlg();
  186. CButton m_btnOK;
  187. //{{AFX_DATA(CAboutDlg)
  188. enum { IDD = IDD_ABOUTBOX };
  189. //}}AFX_DATA
  190. //{{AFX_VIRTUAL(CAboutDlg)
  191. protected:
  192. virtual void DoDataExchange(CDataExchange* pDX);
  193. //}}AFX_VIRTUAL
  194. protected:
  195. //{{AFX_MSG(CAboutDlg)
  196. virtual BOOL OnInitDialog();
  197. //}}AFX_MSG
  198. DECLARE_MESSAGE_MAP()
  199. };
  200. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  201. {
  202. }
  203. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  204. {
  205. CDialog::DoDataExchange(pDX);
  206. //{{AFX_DATA_MAP(CAboutDlg)
  207. DDX_Control(pDX, IDOK, m_btnOK);
  208. //}}AFX_DATA_MAP
  209. }
  210. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  211. //{{AFX_MSG_MAP(CAboutDlg)
  212. //}}AFX_MSG_MAP
  213. END_MESSAGE_MAP()
  214. void CTrfAgentApp::OnAppAbout()
  215. {
  216. CAboutDlg aboutDlg;
  217. aboutDlg.DoModal();
  218. }
  219. BOOL CAboutDlg::OnInitDialog() 
  220. {
  221. CDialog::OnInitDialog();
  222. this->CenterWindow();
  223. this->m_btnOK.SetWindowText(_LoadString(IDS_BTNOK).GetBuffer(0));
  224. return TRUE;
  225. }