home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Profiler / heapmon / baseview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  4.6 KB  |  199 lines

  1. // baseview.cpp
  2. //
  3. // Created 10/07/98
  4. //
  5. // (C)Copyright 1998-1999 Microsoft Corporation, All rights reserved.
  6. //
  7.  
  8. #include "pch.hpp"
  9. #pragma hdrstop
  10.  
  11. #include "baseview.hpp"
  12. #include "hpmonmgr.hpp"
  13.  
  14.  
  15. //static
  16. LRESULT CALLBACK BasicClient::BaseWndProc (HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
  17. {
  18.     switch (msg)
  19.     {
  20.         BasicClient *bc;
  21.         MDICREATESTRUCT *pmdics;
  22.  
  23.     case WM_CREATE:
  24.  
  25.         pmdics = (MDICREATESTRUCT*)((CREATESTRUCT*)lParam)->lpCreateParams;
  26.         bc = (BasicClient*)pmdics->lParam;
  27.  
  28.         // See WM_CLOSE for explanation of why this store can't be AddRef'd.
  29.         SetWindowLong(wnd, GWL_USERDATA, (LONG)bc);
  30.  
  31.         bc->m_hwnd = wnd;
  32.         if (bc->OnCreateWindow())
  33.         {
  34.             bc->AddRef();
  35.         }
  36.         else
  37.         {
  38.             bc->m_hwnd = NULL;
  39.  
  40.             DestroyWindow(wnd);
  41.         }
  42.  
  43.         return 0;
  44.  
  45.     case WM_CLOSE:
  46.  
  47.         bc = (BasicClient*)GetWindowLong(wnd, GWL_USERDATA);
  48.  
  49.         bc->m_mgr->UnregisterClient(bc);
  50.  
  51.         // Do NOT call DestroyWindow here.  See
  52.         // HeapMonitorManager::IterateClients.  Because it cannot be holding
  53.         // locks while sending messages to the UI thread, it may
  54.         // simultaneously be sending a message to this window, so our HWND
  55.         // needs to remain valid until the refcount reaches 0.
  56.  
  57.         // We must release the window's reference to the client now (was
  58.         // AddRef'd above for GWL_USERDATA).  If there are no other references
  59.         // to the window (99.99% case), this is effectively calling
  60.         // DestroyWindow.  If not, some other thread is keeping the client
  61.         // live, and we'll leave the GWL_USERDATA intact so that pending
  62.         // messages can actually be processed.
  63.         bc->Release();
  64.  
  65.         // TODO: switch to hourglass or something
  66.  
  67.         return 0;
  68.  
  69.     case WM_DESTROY:
  70.  
  71.         bc = (BasicClient*)GetWindowLong(wnd, GWL_USERDATA);
  72.         ASSERT(bc);
  73.  
  74.         // REMIND: this may occur inside the manager's critical section.
  75.  
  76.         bc->m_hwnd = NULL;
  77.         SetWindowLong(wnd, GWL_USERDATA, NULL);
  78.  
  79.         break;
  80.  
  81.     case WM_BC_DESTROY_NOW:
  82.  
  83.         bc = (BasicClient*)GetWindowLong(wnd, GWL_USERDATA);
  84.         ASSERT(bc);
  85.  
  86.         // REMIND: this may occur inside the manager's critical section.
  87.  
  88.         SendMessage(bc->m_mgr->GetMDIClientWindow(), WM_MDIDESTROY, (WPARAM)wnd, 0);
  89.         DestroyWindow(wnd);
  90.  
  91.         delete(bc);
  92.         bc = NULL;
  93.  
  94.         return 0;
  95.     }
  96.    
  97.     // REMIND: WM_CHILDACTIVATE, WM_GETMINMAXINFO, WM_MENUCHAR, WM_MOVE,
  98.     // WM_SETFOCUS, WM_SIZE, and WM_SYSCOMMAND must be passed to the mdi proc.
  99.     
  100.     return DefMDIChildProc(wnd, msg, wParam, lParam);
  101. }
  102.  
  103.  
  104. BasicClient::BasicClient ()
  105. {
  106.     m_RefCount = 1;
  107.     m_mgr = NULL;
  108. }
  109.  
  110. BasicClient::~BasicClient ()
  111. {
  112.     ASSERT(!m_RefCount);
  113.  
  114.     // REMIND: this may occur inside the manager's critical section.
  115.  
  116.     if (m_mgr != NULL)
  117.     {
  118.         m_mgr->Release();
  119.     }
  120. }
  121.  
  122. BOOL BasicClient::BasePreInitialize (HeapMonitorManager *mgr, PCSTR wndclsname, PCSTR wndtitle)
  123. {
  124.     BOOL result;
  125.  
  126.     (m_mgr = mgr)->AddRef();
  127.  
  128.     result = CreateMDIWindow( 
  129.         (PSTR)wndclsname,
  130.         (PSTR)wndtitle,
  131.         0,
  132.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  133.         mgr->GetMDIClientWindow(),
  134.         mgr->GetInstance(),
  135.         (LPARAM)this) != NULL;
  136.  
  137.     return result;
  138. }
  139.  
  140. BOOL BasicClient::BasePostInitialize (HeapMonitorClientRegistrationInfo *preginfo)
  141. {
  142.     BOOL result;
  143.  
  144.     result = m_mgr->RegisterClient(this, preginfo);
  145.  
  146.     if (!result)
  147.         SendMessage(m_hwnd, WM_CLOSE, 0, 0);
  148.  
  149.     return result;
  150. }
  151.  
  152.  
  153. ULONG STDMETHODCALLTYPE BasicClient::AddRef (VOID)
  154. {
  155.     ASSERT(m_RefCount && m_RefCount < ULONG_MAX);
  156.  
  157.     return InterlockedIncrement((LONG*)&m_RefCount);
  158. }
  159.  
  160.  
  161. ULONG STDMETHODCALLTYPE BasicClient::Release (VOID)
  162. {
  163.     ASSERT(m_RefCount);
  164.  
  165.     ULONG NewRefCount = (ULONG)InterlockedDecrement((LONG*)&m_RefCount);
  166.     if (NewRefCount)
  167.         return NewRefCount;
  168.  
  169.     // REMIND: this may occur inside the manager's critical section.
  170.  
  171.     if (m_hwnd)
  172.         SendNotifyMessage(m_hwnd, WM_BC_DESTROY_NOW, 0, 0);
  173.     else
  174.         delete(this);
  175.  
  176.     return 0;
  177.  
  178. }
  179.  
  180.  
  181. STDMETHODIMP BasicClient::QueryInterface (REFIID riid, PVOID *ppvObj)
  182. {
  183.     HRESULT hr = S_OK;
  184.  
  185.     if (riid == IID_IUnknown || riid == IID_IHeapMonitorClient)
  186.     {
  187.         *ppvObj = (IHeapMonitorClient*)this;
  188.         AddRef();
  189.     }
  190.     else
  191.     {
  192.         *ppvObj = NULL;
  193.         hr = E_NOINTERFACE;
  194.     }
  195.  
  196.     return hr;
  197. }
  198.  
  199.