home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / activedocument / dfv / winmain.cxx < prev   
C/C++ Source or Header  |  1995-02-13  |  5KB  |  174 lines

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. //  File:       winmain.cxx
  7. //
  8. //  Contents:   main entry point for Stereo Shop
  9. //
  10. //  Classes:
  11. //
  12. //  Functions:  WinMain
  13. //
  14. //  History:    6-08-94   stevebl   Created
  15. //
  16. //----------------------------------------------------------------------------
  17.  
  18. #include "dfv.h"
  19. #include "mwclass.h"
  20. #include <ole2ver.h>
  21.  
  22. //+---------------------------------------------------------------------------
  23. //
  24. //  Function:   InitApplication
  25. //
  26. //  Synopsis:   initializes the application and registers its window class
  27. //              (called once for all instances)
  28. //
  29. //  Arguments:  [hInstance] - handle to the first instance
  30. //
  31. //  Returns:    TRUE on success
  32. //
  33. //  History:    4-11-94   stevebl   Created for MFract
  34. //              6-08-94   stevebl   Stolen from MFract
  35. //
  36. //----------------------------------------------------------------------------
  37.  
  38. BOOL InitApplication(HINSTANCE hInstance)
  39. {
  40.     WNDCLASS wc;
  41.  
  42.     wc.style = 0;
  43.     wc.lpfnWndProc = &WindowProc;
  44.     wc.cbClsExtra = 0;
  45.     wc.cbWndExtra = 0;
  46.     wc.hInstance = hInstance;
  47.     wc.hIcon = LoadIcon(hInstance, TEXT("StreamIcon"));
  48.     wc.hCursor = (HCURSOR) LoadCursor(NULL, IDC_ARROW);
  49.     wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  50.     wc.lpszMenuName = NULL;
  51.     wc.lpszClassName = TEXT(STREAM_VIEW_CLASS_NAME);
  52.     ATOM aReturn = RegisterClass(&wc);
  53.     if (!aReturn)
  54.     {
  55.         return(aReturn);
  56.     }
  57.     wc.style = CS_DBLCLKS;
  58.     wc.hIcon = LoadIcon(hInstance, TEXT("AppIcon"));
  59.     wc.lpszMenuName = TEXT(MAIN_WINDOW_CLASS_MENU_STR);
  60.     wc.lpszClassName = TEXT(MAIN_WINDOW_CLASS_NAME);
  61.     return(RegisterClass(&wc));
  62. }
  63.  
  64. //+---------------------------------------------------------------------------
  65. //
  66. //  Function:   WinMain
  67. //
  68. //  Synopsis:   main window proceedure
  69. //
  70. //  Arguments:  [hInstance]     - instance handle
  71. //              [hPrevInstance] - handle of the previous instance (if any)
  72. //              [lpCmdLine]     - pointer to the command line
  73. //              [nCmdShow]      - show state
  74. //
  75. //  History:    4-11-94   stevebl   Created for MFract
  76. //              6-08-94   stevebl   Stolen from MFract
  77. //
  78. //  Notes:      initializes application and starts message loop
  79. //
  80. //----------------------------------------------------------------------------
  81.  
  82. extern "C" int PASCAL WinMain(HINSTANCE hInstance,
  83.             HINSTANCE hPrevInstance,
  84.             LPSTR lpCmdLine,
  85.             int nCmdShow)
  86. {
  87.     DWORD dwBuildVersion = CoBuildVersion();
  88.     if (HIWORD(dwBuildVersion) != rmm || LOWORD(dwBuildVersion) < rup)
  89.     {
  90.         // alert the caller that the OLE version is incompatible
  91.         // with this build.
  92.         MessageBoxFromStringIdsAndArgs(
  93.             NULL,
  94.             hInstance,
  95.             IDS_OLEINCOMPATIBLE,
  96.             IDS_ERROR,
  97.             MB_OK | MB_ICONEXCLAMATION,
  98.             rmm, rup);
  99.         return(FALSE);
  100.     }
  101.     if (FAILED(CoInitialize(NULL)))
  102.     {
  103.         // alert the caller that OLE couldn't be initialized
  104.         MessageBoxFromStringIds(
  105.             NULL,
  106.             hInstance,
  107.             IDS_OLEINITFAILED,
  108.             IDS_ERROR,
  109.             MB_OK | MB_ICONEXCLAMATION);
  110.         return(FALSE);
  111.     }
  112.     if (!hPrevInstance)
  113.     {
  114.         if (!InitApplication(hInstance))
  115.         {
  116.             CoUninitialize();
  117.             return(FALSE);
  118.         }
  119.     }
  120.     CMainWindow * pw = new CMainWindow;
  121.     if (pw == NULL)
  122.     {
  123.         CoUninitialize();
  124.         return(FALSE);
  125.     }
  126.     if (!pw->InitInstance(hInstance, nCmdShow))
  127.     {
  128.         // Note, if InitInstance has failed then it would have
  129.         // already deleted pw for me so I don't delete it here.
  130.         // This is because when WM_CREATE returns -1 (failure)
  131.         // Windows sends the WM_DESTROY message to the window
  132.         // and the the CHlprWindow class destroys itself whenever
  133.         // it receives this message.
  134.         CoUninitialize();
  135.         return(FALSE);
  136.     }
  137.  
  138.     // check command line and try and load as a file
  139.  
  140.     if (strlen(lpCmdLine) > 0)
  141.     {
  142. #ifdef UNICODE
  143.         mbstowcs(pw->_szFileName, lpCmdLine, MAX_PATH - 1);
  144. #else
  145.         strncpy(pw->_szFileName, lpCmdLine, MAX_PATH - 1);
  146. #endif
  147.         pw->OpenFile();
  148.     }
  149.  
  150.     MSG msg;
  151.     HACCEL haccel = LoadAccelerators(hInstance, TEXT("AppAccel"));
  152.     if (haccel == NULL)
  153.     {
  154.         CoUninitialize();
  155.         return(FALSE);
  156.     }
  157.  
  158.     while (GetMessage(&msg, NULL, 0, 0))
  159.     {
  160.         if (!TranslateAccelerator(
  161.             pw->GetHwnd(),
  162.             haccel,
  163.             &msg))
  164.         {
  165.             TranslateMessage(&msg);
  166.             DispatchMessage(&msg);
  167.         }
  168.     }
  169.     CoUninitialize();
  170.     return(msg.wParam);
  171. }
  172.  
  173.  
  174.