home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / wincom / dllcom / src / dlltask.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.8 KB  |  103 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "dllcom.h"
  20.  
  21. CProcessEntry *CProcess::m_pProcessList = NULL;
  22.  
  23. CProcessEntry::CProcessEntry(CComDll *pDll)
  24. {
  25.     DLL_ASSERT(pDll);
  26.  
  27.     m_dwProcessID = CProcess::GetProcessID();
  28.     m_pDllInstance = pDll;
  29.  
  30.     m_pNext = CProcess::m_pProcessList;
  31.     CProcess::m_pProcessList = this;
  32. }
  33.  
  34. CProcessEntry::~CProcessEntry()
  35. {
  36.     //  find ourselves in the list and remove us.
  37.     CProcessEntry **ppChange = &CProcess::m_pProcessList;
  38.  
  39.     while(*ppChange != this)    {
  40.         ppChange = &((*ppChange)->m_pNext);
  41.     }
  42.  
  43.     *ppChange = m_pNext;
  44.  
  45.     m_dwProcessID = 0;
  46.     m_pDllInstance = NULL;
  47.     m_pNext = NULL;
  48. }
  49.  
  50. DWORD CProcess::GetProcessID()
  51. {
  52.     DWORD dwRetval = 0;
  53.  
  54.     //  This is only needed to multiplex instance data on win16.
  55. #ifdef DLL_WIN16
  56.     dwRetval = CoGetCurrentProcess();
  57. #else
  58.     //  Just use the HINSTANCE unless we need to do this on a per
  59.     //      thread bases (then as above like WIN16).
  60.     dwRetval = (DWORD)CComDll::GetInstanceHandle();
  61. #endif
  62.  
  63.     return(dwRetval);
  64. }
  65.  
  66. CComDll *CProcess::GetProcessDll()
  67. {
  68.     CComDll *pRetval = NULL;
  69.  
  70.     //  Before we can find the Dll instance data, we need to
  71.     //      get the ID of the current process calling us.
  72.     DWORD dwCurrent = GetProcessID();
  73.  
  74.     //  See if the object already exists.
  75.     CProcessEntry *pTraverse = m_pProcessList;
  76.     while(pTraverse && pTraverse->m_dwProcessID != dwCurrent)   {
  77.         pTraverse = pTraverse->m_pNext;
  78.     }
  79.  
  80.     if(pTraverse)   {
  81.         //  Have it already.
  82.         //  Return it.
  83.         pRetval = pTraverse->m_pDllInstance;
  84.  
  85.         //  We must be sure to properly reference count the returned
  86.         //      object.
  87.         pRetval->AddRef();
  88.     }
  89.     else    {
  90.         //  Do not have it.
  91.         //  Ask the end consumer to create it.
  92.         pRetval = DLL_ConsumerCreateInstance();
  93.     }
  94.  
  95.     return(pRetval);
  96. }
  97.  
  98. BOOL CProcess::CanUnloadNow()
  99. {
  100.     //  We can unload if we have no more process entries;
  101.     //      all objects are freed off.
  102.     return(m_pProcessList == NULL);
  103. }