Back to index

lightning-sunbird  0.9+nobinonly
TestCOM.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <windows.h>
00040 #include <unknwn.h>
00041 #include <stdio.h>
00042 #include "nsISupports.h"
00043 #include "nsIFactory.h"
00044 
00045 // unknwn.h is needed to build with WIN32_LEAN_AND_MEAN
00046 #include <unknwn.h>
00047 
00048 // {5846BA30-B856-11d1-A98A-00805F8A7AC4}
00049 #define NS_ITEST_COM_IID \
00050 { 0x5846ba30, 0xb856, 0x11d1, \
00051   { 0xa9, 0x8a, 0x0, 0x80, 0x5f, 0x8a, 0x7a, 0xc4 } }
00052 
00053 class nsITestCom: public nsISupports
00054 {
00055 public:
00056   NS_DEFINE_STATIC_IID_ACCESSOR(NS_ITEST_COM_IID)
00057   NS_IMETHOD Test() = 0;
00058 };
00059 
00060 /*
00061  * nsTestCom
00062  */
00063 
00064 class nsTestCom: public nsITestCom {
00065   NS_DECL_ISUPPORTS
00066 
00067 public:
00068   nsTestCom() {
00069   }
00070 
00071   NS_IMETHOD Test() {
00072     printf("Accessed nsITestCom::Test() from COM\n");
00073     return NS_OK;
00074   }
00075 
00076 private:
00077   ~nsTestCom() {
00078     printf("nsTestCom instance successfully deleted\n");
00079   }
00080 };
00081 
00082 NS_IMPL_QUERY_INTERFACE1(nsTestCom, nsITestCom)
00083 
00084 nsrefcnt nsTestCom::AddRef() 
00085 {
00086   nsrefcnt res = ++mRefCnt;
00087   NS_LOG_ADDREF(this, mRefCnt, "nsTestCom", sizeof(*this));
00088   printf("nsTestCom: Adding ref = %d\n", res);
00089   return res;
00090 }
00091 
00092 nsrefcnt nsTestCom::Release() 
00093 {
00094   nsrefcnt res = --mRefCnt;
00095   NS_LOG_RELEASE(this, mRefCnt, "nsTestCom");
00096   printf("nsTestCom: Releasing = %d\n", res);
00097   if (res == 0) {
00098     delete this;
00099   }
00100   return res;
00101 }
00102 
00103 class nsTestComFactory: public nsIFactory {
00104   NS_DECL_ISUPPORTS
00105 public:
00106   nsTestComFactory() {
00107   }
00108   
00109   NS_IMETHOD CreateInstance(nsISupports *aOuter,
00110                             const nsIID &aIID,
00111                             void **aResult);
00112 
00113   NS_IMETHOD LockFactory(PRBool aLock) {
00114     printf("nsTestComFactory: ");
00115     printf("%s", (aLock == PR_TRUE ? "Locking server" : "Unlocking server"));
00116     printf("\n");
00117     return S_OK;
00118   }
00119 };
00120 
00121 NS_IMPL_ISUPPORTS1(nsTestComFactory, nsIFactory)
00122 
00123 nsresult nsTestComFactory::CreateInstance(nsISupports *aOuter,
00124                                      const nsIID &aIID,
00125                                      void **aResult)
00126 {
00127   if (aOuter != NULL) {
00128     return NS_ERROR_NO_AGGREGATION;
00129   }
00130 
00131   nsTestCom *t = new nsTestCom();
00132   
00133   if (t == NULL) {
00134     return NS_ERROR_OUT_OF_MEMORY;
00135   }
00136   
00137   NS_ADDREF(t);
00138   nsresult res = t->QueryInterface(aIID, aResult);
00139   NS_RELEASE(t);
00140 
00141   if (NS_SUCCEEDED(res)) {
00142     printf("nsTestComFactory: successfully created nsTestCom instance\n");
00143   }
00144 
00145   return res;
00146 }
00147 
00148 /*
00149  * main
00150  */
00151 
00152 int main(int argc, char *argv[])
00153 {
00154   nsTestComFactory *inst = new nsTestComFactory();
00155   IClassFactory *iFactory;
00156   inst->QueryInterface(NS_GET_IID(nsIFactory), (void **) &iFactory);
00157 
00158   IUnknown *iUnknown;  
00159   nsITestCom *iTestCom;
00160 
00161   nsresult res;
00162   iFactory->LockServer(TRUE);
00163   res = iFactory->CreateInstance(NULL,
00164                              IID_IUnknown, 
00165                              (void **) &iUnknown);
00166   iFactory->LockServer(FALSE);
00167 
00168   GUID testGUID = NS_ITEST_COM_IID;
00169   HRESULT hres;
00170   hres= iUnknown->QueryInterface(testGUID, 
00171                              (void **) &iTestCom);
00172 
00173   iTestCom->Test();
00174 
00175   iUnknown->Release();
00176   iTestCom->Release();
00177   iFactory->Release();
00178 
00179   return 0;
00180 }
00181