home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / numega / sc501.exe / data1.cab / Examples / OLEERR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  19.5 KB  |  421 lines

  1. /*
  2.  * OLEErr.cpp
  3.  * $Header: /BoundsChecker/Examples/BUGBNCHX/MAINERR/OLEERR.CPP 2     10/30/96 4:08p Dave $
  4.  *
  5.  * Description:
  6.  *  The functions that cause the OLE Check errors.
  7.  *
  8.  * Notes:
  9.  *  <implementation notes go here>
  10.  *
  11.  ***********************************************************************
  12.  *
  13.  * Nu-Mega Technologies, Inc.
  14.  * P.O. Box 7780
  15.  * Nashua, NH 03060
  16.  *
  17.  * (c) Copyright 1994, 1995 NuMega Technologies, Inc.
  18.  * ALL RIGHTS RESERVED.
  19.  *
  20.  ***********************************************************************
  21.  *
  22.  **********************************************************************/
  23. #include "stdafx.h"
  24. #include "OLEErr.h"
  25.  
  26. #ifdef _UNICODE
  27. #define OLE_ERR_PREP()                                                                                         \
  28.       CLSID clsidIFace ;                                                                                       \
  29.                                                                                                                \
  30.       LPTSTR    lpszIfaceTestProgID  = _T ( "InterfaceTest" ) ;                                                \
  31.       WCHAR     lpszwIfaceTestProgID [ 128 ] ;                                                                 \
  32.                                                                                                                \
  33.       memset ( lpszwIfaceTestProgID , 0 , ( sizeof ( WCHAR ) * 128 ) ) ;                                       \
  34.                                                                                                                \
  35.       lstrcpy ( lpszwIfaceTestProgID , lpszIfaceTestProgID ) ;                                                 \
  36.                                                                                                                \
  37.       HRESULT hrCLSID = CLSIDFromProgID( lpszwIfaceTestProgID, &clsidIFace ) ;                                 \
  38.       if ( S_OK != hrCLSID )                                                                                   \
  39.       {                                                                                                        \
  40.          MessageBox ( NULL                                                                     ,               \
  41.                       "Please register the IFACEDLL.DLL or re-install BoundsChecker examples." ,               \
  42.                       "BugBench Registration Error"                                            ,               \
  43.                       MB_APPLMODAL | MB_OK                                                      ) ;            \
  44.          return ;                                                                                              \
  45.       }                                                                                                        \
  46.                                                                                                                \
  47.       HRESULT hr = CoCreateInstance ( clsidIFace , NULL , CLSCTX_INPROC_SERVER ,                               \
  48.                                       IID_IUnknown , (LPVOID*)&lpUnk ) ;                                       \
  49.                                                                                                                \
  50.       if ( S_OK != hr )                                                                                        \
  51.       {                                                                                                        \
  52.          MessageBox ( NULL                                         ,                                           \
  53.                       "Could not create the InterfaceTest Object." ,                                           \
  54.                       "BugBench Registration Error"                ,                                           \
  55.                       MB_APPLMODAL | MB_OK                          ) ;                                        \
  56.          return ;                                                                                              \
  57.       }                                                                                                        
  58. #else
  59.  
  60. #define OLE_ERR_PREP()                                                                                         \
  61.       CLSID clsidIFace ;                                                                                       \
  62.                                                                                                                \
  63.       LPTSTR    lpszIfaceTestProgID  = _T ( "InterfaceTest" ) ;                                                \
  64.       WCHAR     lpszwIfaceTestProgID [ 128 ] ;                                                                 \
  65.                                                                                                                \
  66.       memset ( lpszwIfaceTestProgID , 0 , ( sizeof ( WCHAR ) * 128 ) ) ;                                       \
  67.                                                                                                                \
  68.       mbstowcs ( lpszwIfaceTestProgID  , lpszIfaceTestProgID , lstrlen ( lpszIfaceTestProgID ) ) ;             \
  69.                                                                                                                \
  70.       HRESULT hrCLSID = CLSIDFromProgID( lpszwIfaceTestProgID, &clsidIFace ) ;                                 \
  71.       if ( S_OK != hrCLSID )                                                                                   \
  72.       {                                                                                                        \
  73.          MessageBox ( NULL                                                                     ,               \
  74.                       "Please register the IFACEDLL.DLL or re-install BoundsChecker examples." ,               \
  75.                       "BugBench Registration Error"                                            ,               \
  76.                       MB_APPLMODAL | MB_OK                                                      ) ;            \
  77.          return ;                                                                                              \
  78.       }                                                                                                        \
  79.                                                                                                                \
  80.       HRESULT hr = CoCreateInstance ( clsidIFace , NULL , CLSCTX_INPROC_SERVER ,                               \
  81.                                       IID_IUnknown , (LPVOID*)&lpUnk ) ;                                       \
  82.                                                                                                                \
  83.       if ( S_OK != hr )                                                                                        \
  84.       {                                                                                                        \
  85.          MessageBox ( NULL                                         ,                                           \
  86.                       "Could not create the InterfaceTest Object." ,                                           \
  87.                       "BugBench Registration Error"                ,                                           \
  88.                       MB_APPLMODAL | MB_OK                          ) ;                                        \
  89.          return ;                                                                                              \
  90.       }                                                                                                        
  91.  
  92. #endif // if-else-_UNICODE
  93.  
  94. // OLECheck - Interface leak
  95. void OLE_Interface_Leak ( )
  96. {
  97.    _TRY
  98.    {
  99.       CLSID clsidIFace ;
  100.  
  101.       LPTSTR    lpszIfaceTestProgID  = _T ( "InterfaceTest" ) ;
  102.       WCHAR     lpszwIfaceTestProgID [ 128 ] ;
  103.       
  104.       memset ( lpszwIfaceTestProgID , 0 , ( sizeof ( WCHAR ) * 128 ) ) ;
  105.  
  106. #ifdef _UNICODE
  107.       lstrcpy ( lpszwIfaceTestProgID , lpszIfaceTestProgID ) ;
  108. #else                               
  109.       mbstowcs ( lpszwIfaceTestProgID  , lpszIfaceTestProgID , lstrlen ( lpszIfaceTestProgID ) ) ;
  110. #endif
  111.  
  112.       HRESULT hrCLSID = CLSIDFromProgID( lpszwIfaceTestProgID , &clsidIFace ) ; 
  113.       if ( S_OK != hrCLSID )
  114.       {
  115.          MessageBox ( NULL                                                                     , 
  116.                       "Please register the IFACEDLL.DLL or re-install BoundsChecker examples." , 
  117.                       "BugBench Registration Error"                                            ,
  118.                       MB_APPLMODAL | MB_OK                                                                 ) ;
  119.          return ;
  120.       }
  121.  
  122.       LPUNKNOWN lpUnk ;
  123.       HRESULT hr = CoCreateInstance ( clsidIFace , NULL , CLSCTX_INPROC_SERVER , IID_IUnknown , (LPVOID*)&lpUnk ) ;
  124.  
  125.       // Thre lpUnk->Release should be called to prevent a leak, but since we want to CAUSE a leak,
  126.       // it won't be called.
  127.  
  128.    }
  129.    _CATCH
  130.    {
  131.    }
  132. }
  133.  
  134. // OLECheck - Interface method failure
  135. void OLE_IntfFail_QueryInterface ( )
  136. {
  137.    LPUNKNOWN lpUnk ;
  138.    _TRY
  139.    {
  140.       // OLE_ERR_PREP allocates a local variable lpUnk, a valid LPUNKNOWN.
  141.       OLE_ERR_PREP()
  142.  
  143.       // Now generate the real error.
  144.       LPMALLOC lpMalloc ;
  145.       HRESULT hrExpectFail = lpUnk->QueryInterface ( IID_IMalloc, (LPVOID*)&lpMalloc) ;
  146.  
  147.       if ( S_OK == hrExpectFail )
  148.       { 
  149.          // This shouldn't happen, but if it works, let's release the pointer anyway.
  150.          lpMalloc->Release ( ) ;
  151.       }
  152.  
  153.       lpUnk->Release ( ) ;
  154.    }
  155.    _CATCH
  156.    {
  157.    }
  158. }
  159.  
  160.  
  161. // OLECheck - Interface method argument: Bad handle
  162. void OLE_IntfArg_BadHandle_Draw ( ) 
  163. {
  164.    LPUNKNOWN lpUnk ;
  165.    _TRY
  166.    {
  167.       // OLE_ERR_PREP allocates a local variable lpUnk, a valid LPUNKNOWN.
  168.       OLE_ERR_PREP()
  169.  
  170.       LPVIEWOBJECT lpVwObj ;
  171.       HRESULT hrExpectOk = lpUnk->QueryInterface ( IID_IViewObject , (LPVOID*)&lpVwObj ) ;
  172.  
  173.       if ( S_OK != hrExpectOk )
  174.       {
  175.          MessageBox ( NULL                                                                                             , 
  176.                       "The Interface Test Object should support IViewObject. Please re-install the BugBench examples." , 
  177.                       "BugBench Registration Error"                                                                    ,
  178.                       MB_APPLMODAL | MB_OK                                                                              ) ;
  179.          return ;
  180.       }
  181.  
  182.  
  183.       // Now generate the error 
  184.       HDC dcRender = NULL ;
  185.       RECTL rectBounds;
  186.       rectBounds.left   = 100 ;
  187.       rectBounds.right  = 100 ;
  188.       rectBounds.top    = 100 ;
  189.       rectBounds.bottom = 100 ;
  190.       lpVwObj->Draw ( DVASPECT_CONTENT ,  /* Device aspect              */ 
  191.                       0                ,  /* View index                 */
  192.                       NULL             ,  /* Must be NULL               */
  193.                       NULL             ,  /* Target Device description  */
  194.                       NULL             ,  /* Target Device info context */
  195.                       dcRender         ,  /* Device context to draw on  */ /* This is the bug */
  196.                       &rectBounds      ,  /* Bounding rectangle         */
  197.                       NULL             ,  /* Window bounding rect       */
  198.                       NULL             ,  /* Call-back routine          */
  199.                       0                   /* Continue call-back param   */ ) ;
  200.  
  201.       lpVwObj->Release ( ) ;
  202.    }
  203.    _CATCH
  204.    {
  205.    }
  206.  
  207.    lpUnk->Release () ;
  208. }
  209.  
  210. // OLECheck - Interface method argument: General
  211. void OLE_IntfArg_General_QueryGetData ( )
  212. {
  213.    LPUNKNOWN lpUnk ;
  214.    _TRY
  215.    {
  216.       // OLE_ERR_PREP allocates a local variable lpUnk, a valid LPUNKNOWN.
  217.       OLE_ERR_PREP()
  218.  
  219.       LPDATAOBJECT lpDataObj ;
  220.       HRESULT hrExpectOk = lpUnk->QueryInterface ( IID_IDataObject , (LPVOID*)&lpDataObj ) ;
  221.  
  222.       if ( S_OK != hrExpectOk )
  223.       {
  224.          MessageBox ( NULL                                                                                             , 
  225.                       "The Interface Test Object should support IViewObject. Please re-install the BugBench examples." , 
  226.                       "BugBench Registration Error"                                                                    ,
  227.                       MB_APPLMODAL | MB_OK                                                                              ) ;
  228.  
  229.          return ;
  230.       }
  231.  
  232.       // Now generate the error
  233.       FORMATETC* pFormatEtc = (FORMATETC*)0x01;
  234.       lpDataObj->QueryGetData( pFormatEtc ) ;
  235.       
  236.       lpDataObj->Release();
  237.    }
  238.    _CATCH
  239.    {
  240.    }
  241.  
  242.    lpUnk->Release () ;
  243. }
  244.  
  245.  
  246. // OLECheck - Interface method argument: Out of range
  247. void OLE_IntfArg_BadRange_Draw ( )
  248. {
  249.    LPUNKNOWN lpUnk ;
  250.    _TRY
  251.    {
  252.       // OLE_ERR_PREP allocates a local variable lpUnk, a valid LPUNKNOWN.
  253.       OLE_ERR_PREP()
  254.  
  255.       LPVIEWOBJECT lpVwObj ;
  256.       HRESULT hrExpectOk = lpUnk->QueryInterface ( IID_IViewObject , (LPVOID*)&lpVwObj ) ;
  257.  
  258.       if ( S_OK != hrExpectOk )
  259.       {
  260.          MessageBox ( NULL                                                                                             , 
  261.                       "The Interface Test Object should support IViewObject. Please re-install the BugBench examples." , 
  262.                       "BugBench Registration Error"                                                                    ,
  263.                       MB_APPLMODAL | MB_OK                                                                              ) ;
  264.          return ;
  265.       }
  266.  
  267.  
  268.       // Now generate the error 
  269.       HDC dcRender = CreateDC ( _T ( "DISPLAY" ), NULL , NULL , NULL ) ;
  270.       RECTL rectBounds;
  271.       rectBounds.left   = 100 ;
  272.       rectBounds.right  = 100 ;
  273.       rectBounds.top    = 100 ;
  274.       rectBounds.bottom = 100 ;
  275.       lpVwObj->Draw ( DVASPECT_CONTENT ,  /* Device aspect              */ 
  276.                       0                ,  /* View index                 */
  277.                       (LPVOID) 1       ,  /* Must be NULL               */ /* This is the bug */
  278.                       NULL             ,  /* Target Device description  */
  279.                       NULL             ,  /* Target Device info context */
  280.                       dcRender         ,  /* Device context to draw on  */ 
  281.                       &rectBounds      ,  /* Bounding rectangle         */
  282.                       NULL             ,  /* Window bounding rect       */
  283.                       NULL             ,  /* Call-back routine          */
  284.                       0                   /* Continue call-back param   */ ) ;
  285.  
  286.       lpVwObj->Release ( ) ;
  287.       DeleteDC ( dcRender ) ;
  288.    }
  289.    _CATCH
  290.    {
  291.    }
  292.  
  293.    lpUnk->Release () ;
  294. }
  295.  
  296.  
  297. // OLECheck - Interface method argument: Structure size field is not initialized
  298. void OLE_IntfArg_StructSize_Draw ( )
  299. {
  300.    LPUNKNOWN lpUnk ;
  301.    _TRY
  302.    {
  303.       // OLE_ERR_PREP allocates a local variable lpUnk, a valid LPUNKNOWN.
  304.       OLE_ERR_PREP()
  305.  
  306.       LPVIEWOBJECT lpVwObj ;
  307.       HRESULT hrExpectOk = lpUnk->QueryInterface ( IID_IViewObject , (LPVOID*)&lpVwObj ) ;
  308.  
  309.       if ( S_OK != hrExpectOk )
  310.       {
  311.          MessageBox ( NULL                                                                                             , 
  312.                       "The Interface Test Object should support IViewObject. Please re-install the BugBench examples." , 
  313.                       "BugBench Registration Error"                                                                    ,
  314.                       MB_APPLMODAL | MB_OK                                                                              ) ;
  315.          return ;
  316.       }
  317.  
  318.  
  319.       // Now generate the error 
  320.       HDC dcRender = CreateDC ( _T ( "DISPLAY" ), NULL , NULL , NULL ) ;
  321.       RECTL rectBounds;
  322.       rectBounds.left   = 100 ;
  323.       rectBounds.right  = 100 ;
  324.       rectBounds.top    = 100 ;
  325.       rectBounds.bottom = 100 ;
  326.       DVTARGETDEVICE dvTarget ;
  327.  
  328.       lpVwObj->Draw ( DVASPECT_CONTENT ,  /* Device aspect              */ 
  329.                       0                ,  /* View index                 */
  330.                       NULL             ,  /* Must be NULL               */ 
  331.                       &dvTarget        ,  /* Target Device description  */ /* This is the bug */
  332.                       NULL             ,  /* Target Device info context */
  333.                       dcRender         ,  /* Device context to draw on  */ 
  334.                       &rectBounds      ,  /* Bounding rectangle         */
  335.                       NULL             ,  /* Window bounding rect       */
  336.                       NULL             ,  /* Call-back routine          */
  337.                       0                   /* Continue call-back param   */ ) ;
  338.  
  339.       lpVwObj->Release ( ) ;
  340.    }
  341.    _CATCH
  342.    {
  343.    }
  344.  
  345.    lpUnk->Release () ;
  346. }
  347.  
  348.  
  349. // OLECheck - Interface method argument: Undefined or illegal flags
  350. void OLE_IntfArg_InvalidFlag_SetAdvise ( )
  351. {
  352.    LPUNKNOWN lpUnk ;
  353.    _TRY
  354.    {
  355.       // OLE_ERR_PREP allocates a local variable lpUnk, a valid LPUNKNOWN.
  356.       OLE_ERR_PREP()
  357.  
  358.       LPVIEWOBJECT lpVwObj ;
  359.       HRESULT hrExpectOk = lpUnk->QueryInterface ( IID_IViewObject , (LPVOID*)&lpVwObj ) ;
  360.  
  361.       if ( S_OK != hrExpectOk )
  362.       {
  363.          MessageBox ( NULL                                                                                             , 
  364.                       "The Interface Test Object should support IViewObject. Please re-install the BugBench examples." , 
  365.                       "BugBench Registration Error"                                                                    ,
  366.                       MB_APPLMODAL | MB_OK                                                                              ) ;
  367.          return ;
  368.       }
  369.  
  370.  
  371.       // Now generate the error 
  372.       lpVwObj->SetAdvise ( DVASPECT_CONTENT ,    /* Device aspect              */ 
  373.                            ADVF_NODATA      ,    /* Advisory flag              */ /* This is the bug */
  374.                            NULL              ) ; /* the advisory sink          */
  375.  
  376.       lpVwObj->Release ( ) ;
  377.    }
  378.    _CATCH
  379.    {
  380.    }
  381.  
  382.    lpUnk->Release () ;
  383. }
  384.  
  385.  
  386. // OLECheck - Interface method argument: Conflicting combination of flags
  387. void OLE_IntfArg_BadComboFlag_SetAdvise ( )
  388. {
  389.    LPUNKNOWN lpUnk ;
  390.    _TRY
  391.    {
  392.       // OLE_ERR_PREP allocates a local variable lpUnk, a valid LPUNKNOWN.
  393.       OLE_ERR_PREP()
  394.  
  395.       LPVIEWOBJECT lpVwObj ;
  396.       HRESULT hrExpectOk = lpUnk->QueryInterface ( IID_IViewObject , (LPVOID*)&lpVwObj ) ;
  397.  
  398.       if ( S_OK != hrExpectOk )
  399.       {
  400.          MessageBox ( NULL                                                                                             , 
  401.                       "The Interface Test Object should support IViewObject. Please re-install the BugBench examples." , 
  402.                       "BugBench Registration Error"                                                                    ,
  403.                       MB_APPLMODAL | MB_OK                                                                              ) ;
  404.          return ;
  405.       }
  406.  
  407.  
  408.       // Now generate the error 
  409.       lpVwObj->SetAdvise ( DVASPECT_CONTENT | DVASPECT_THUMBNAIL ,    /* Device aspect              */ /* This is the bug */
  410.                            ADVF_ONLYONCE                         ,    /* Advisory flag              */ 
  411.                            NULL                                   ) ; /* the advisory sink          */
  412.  
  413.       lpVwObj->Release ( ) ;
  414.    }
  415.    _CATCH
  416.    {
  417.    }
  418.  
  419.    lpUnk->Release () ;
  420. }
  421.