/reporting/crashsender/CrashSender.cpp

http://crashrpt.googlecode.com/ · C++ · 139 lines · 77 code · 24 blank · 38 comment · 9 complexity · b6441c1f12c56f4bd04addf9fb87dc90 MD5 · raw file

  1. /*************************************************************************************
  2. This file is a part of CrashRpt library.
  3. Copyright (c) 2003-2013 The CrashRpt project authors. All Rights Reserved.
  4. Use of this source code is governed by a BSD-style license
  5. that can be found in the License.txt file in the root of the source
  6. tree. All contributing project authors may
  7. be found in the Authors.txt file in the root of the source tree.
  8. ***************************************************************************************/
  9. // File: CrashSender.cpp
  10. // Description: Entry point to the application.
  11. // Authors: zexspectrum
  12. // Date: 2010
  13. #include "stdafx.h"
  14. #include "resource.h"
  15. #include "ErrorReportDlg.h"
  16. #include "ResendDlg.h"
  17. #include "CrashInfoReader.h"
  18. #include "strconv.h"
  19. #include "Utility.h"
  20. CAppModule _Module; // WTL's application module.
  21. int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int /*nCmdShow*/ = SW_SHOWDEFAULT)
  22. {
  23. int nRet = 0; // Return code
  24. CErrorReportDlg dlgErrorReport; // Error Report dialog
  25. CResendDlg dlgResend; // Resend dialog
  26. // Get command line parameters.
  27. LPCWSTR szCommandLine = GetCommandLineW();
  28. // Split command line.
  29. int argc = 0;
  30. LPWSTR* argv = CommandLineToArgvW(szCommandLine, &argc);
  31. // Check parameter count.
  32. if(argc!=2)
  33. return 1; // No arguments passed, exit.
  34. if(_tcscmp(argv[1], _T("/terminate"))==0)
  35. {
  36. // User wants us to find and terminate all instances of CrashSender.exe
  37. return CErrorReportSender::TerminateAllCrashSenderProcesses();
  38. }
  39. // Extract file mapping name from command line arg.
  40. CString sFileMappingName = CString(argv[1]);
  41. // Create the sender model that will collect crash report data
  42. // and send error report(s).
  43. CErrorReportSender* pSender = CErrorReportSender::GetInstance();
  44. // Init the sender object
  45. BOOL bInit = pSender->Init(sFileMappingName.GetBuffer(0));
  46. if(!bInit)
  47. {
  48. // Failed to init
  49. delete pSender;
  50. return 0;
  51. }
  52. // Determine what to do next
  53. // (either run in GUI more or run in silent mode).
  54. if(!pSender->GetCrashInfo()->m_bSilentMode)
  55. {
  56. // GUI mode.
  57. // Create message loop.
  58. CMessageLoop theLoop;
  59. _Module.AddMessageLoop(&theLoop);
  60. if(!pSender->GetCrashInfo()->m_bSendRecentReports)
  61. {
  62. // Create "Error Report" dialog
  63. if(dlgErrorReport.Create(NULL) == NULL)
  64. {
  65. ATLTRACE(_T("Error report dialog creation failed!\n"));
  66. delete pSender;
  67. return 1;
  68. }
  69. }
  70. else
  71. {
  72. // Create "Send Error Reports" dialog.
  73. if(dlgResend.Create(NULL) == NULL)
  74. {
  75. ATLTRACE(_T("Resend dialog creation failed!\n"));
  76. delete pSender;
  77. return 1;
  78. }
  79. }
  80. // Process window messages.
  81. nRet = theLoop.Run();
  82. _Module.RemoveMessageLoop();
  83. }
  84. else
  85. {
  86. // Silent (non-GUI mode).
  87. // Run the sender and wait until it exits.
  88. pSender->Run();
  89. pSender->WaitForCompletion();
  90. // Get return status
  91. nRet = pSender->GetStatus();
  92. }
  93. // Delete sender object.
  94. delete pSender;
  95. // Exit.
  96. return nRet;
  97. }
  98. int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
  99. {
  100. HRESULT hRes = ::CoInitialize(NULL);
  101. // If you are running on NT 4.0 or higher you can use the following call instead to
  102. // make the EXE free threaded. This means that calls come in on a random RPC thread.
  103. // HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
  104. ATLASSERT(SUCCEEDED(hRes));
  105. // this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
  106. ::DefWindowProc(NULL, 0, 0, 0L);
  107. AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES); // add flags to support other controls
  108. hRes = _Module.Init(NULL, hInstance);
  109. ATLASSERT(SUCCEEDED(hRes));
  110. int nRet = Run(lpstrCmdLine, nCmdShow);
  111. _Module.Term();
  112. ::CoUninitialize();
  113. return nRet;
  114. }