home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / xpcom / tests / windows / TestCOM.cpp < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.6 KB  |  158 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 <iostream.h>
  20. #include "nsISupports.h"
  21. #include "nsIFactory.h"
  22.  
  23. // {5846BA30-B856-11d1-A98A-00805F8A7AC4}
  24. #define NS_ITEST_COM_IID \
  25. { 0x5846ba30, 0xb856, 0x11d1, \
  26.   { 0xa9, 0x8a, 0x0, 0x80, 0x5f, 0x8a, 0x7a, 0xc4 } }
  27.  
  28. class nsITestCom: public nsISupports
  29. {
  30. public:
  31.   NS_IMETHOD Test() = 0;
  32. };
  33.  
  34. static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
  35. static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
  36. static NS_DEFINE_IID(kITestComIID, NS_ITEST_COM_IID);
  37.  
  38. /*
  39.  * nsTestCom
  40.  */
  41.  
  42. class nsTestCom: public nsITestCom {
  43.   NS_DECL_ISUPPORTS
  44.  
  45. public:
  46.   nsTestCom() {
  47.     NS_INIT_REFCNT();
  48.   }
  49.   virtual ~nsTestCom() {
  50.     cout << "nsTestCom instance successfully deleted\n";
  51.   }
  52.  
  53.   NS_IMETHOD Test() {
  54.     cout << "Accessed nsITestCom::Test() from COM\n";
  55.     return NS_OK;
  56.   }
  57. };
  58.  
  59. NS_IMPL_QUERY_INTERFACE(nsTestCom, kITestComIID);
  60.  
  61. nsrefcnt nsTestCom::AddRef() 
  62. {
  63.   nsrefcnt res = ++mRefCnt;
  64.   cout << "nsTestCom: Adding ref = " << res << "\n";
  65.   return res;
  66. }
  67.  
  68. nsrefcnt nsTestCom::Release() 
  69. {
  70.   nsrefcnt res = --mRefCnt;
  71.   cout << "nsTestCom: Releasing = " << res << "\n";
  72.   if (res == 0) {
  73.     delete this;
  74.   }
  75.   return res;
  76. }
  77.  
  78. class nsTestComFactory: public nsIFactory {
  79.   NS_DECL_ISUPPORTS
  80. public:
  81.   nsTestComFactory() {
  82.     NS_INIT_REFCNT();
  83.   }
  84.   
  85.   NS_IMETHOD CreateInstance(nsISupports *aOuter,
  86.                             const nsIID &aIID,
  87.                             void **aResult);
  88.  
  89.   NS_IMETHOD_(void) LockFactory(PRBool aLock) {
  90.     cout << "nsTestComFactory: ";
  91.     cout << (aLock == PR_TRUE ? "Locking server" : "Unlocking server");
  92.     cout << "\n";
  93.   }
  94. };
  95.  
  96. NS_IMPL_ISUPPORTS(nsTestComFactory, kIFactoryIID);
  97.  
  98. nsresult nsTestComFactory::CreateInstance(nsISupports *aOuter,
  99.                       const nsIID &aIID,
  100.                       void **aResult)
  101. {
  102.   if (aOuter != NULL) {
  103.     return NS_ERROR_NO_AGGREGATION;
  104.   }
  105.  
  106.   nsTestCom *t = new nsTestCom();
  107.   
  108.   if (t == NULL) {
  109.     return NS_ERROR_OUT_OF_MEMORY;
  110.   }
  111.   
  112.   nsresult res = t->QueryInterface(aIID, aResult);
  113.  
  114.   if (NS_FAILED(res)) {
  115.     *aResult = NULL;
  116.     delete t;
  117.   }
  118.  
  119.   cout << "nsTestComFactory: successfully created nsTestCom instance\n";
  120.  
  121.   return res;
  122. }
  123.  
  124. /*
  125.  * main
  126.  */
  127.  
  128. int main(int argc, char *argv[])
  129. {
  130.   nsTestComFactory *inst = new nsTestComFactory();
  131.   IClassFactory *iFactory;
  132.   inst->QueryInterface(kIFactoryIID, (void **) &iFactory);
  133.  
  134.   IUnknown *iUnknown;  
  135.   nsITestCom *iTestCom;
  136.  
  137.   nsresult res;
  138.   iFactory->LockServer(TRUE);
  139.   res = iFactory->CreateInstance(NULL,
  140.                  IID_IUnknown, 
  141.                  (void **) &iUnknown);
  142.   iFactory->LockServer(FALSE);
  143.  
  144.   GUID testGUID = NS_ITEST_COM_IID;
  145.   HRESULT hres;
  146.   hres= iUnknown->QueryInterface(testGUID, 
  147.                  (void **) &iTestCom);
  148.  
  149.   iTestCom->Test();
  150.  
  151.   iUnknown->Release();
  152.   iTestCom->Release();
  153.   iFactory->Release();
  154.  
  155.   return 0;
  156. }
  157.  
  158.