xpcom/tests/windows/TestCOM.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/windows/TestCOM.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,147 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <windows.h>
    1.10 +#include <unknwn.h>
    1.11 +#include <stdio.h>
    1.12 +#include "nsISupports.h"
    1.13 +#include "nsIFactory.h"
    1.14 +
    1.15 +// unknwn.h is needed to build with WIN32_LEAN_AND_MEAN
    1.16 +#include <unknwn.h>
    1.17 +
    1.18 +// {5846BA30-B856-11d1-A98A-00805F8A7AC4}
    1.19 +#define NS_ITEST_COM_IID \
    1.20 +{ 0x5846ba30, 0xb856, 0x11d1, \
    1.21 +  { 0xa9, 0x8a, 0x0, 0x80, 0x5f, 0x8a, 0x7a, 0xc4 } }
    1.22 +
    1.23 +class nsITestCom: public nsISupports
    1.24 +{
    1.25 +public:
    1.26 +  NS_DECLARE_STATIC_IID_ACCESSOR(NS_ITEST_COM_IID)
    1.27 +  NS_IMETHOD Test() = 0;
    1.28 +};
    1.29 +
    1.30 +NS_DEFINE_STATIC_IID_ACCESSOR(nsITestCom, NS_ITEST_COM_IID)
    1.31 +
    1.32 +/*
    1.33 + * nsTestCom
    1.34 + */
    1.35 +
    1.36 +class nsTestCom MOZ_FINAL : public nsITestCom {
    1.37 +  NS_DECL_ISUPPORTS
    1.38 +
    1.39 +public:
    1.40 +  nsTestCom() {
    1.41 +  }
    1.42 +
    1.43 +  NS_IMETHOD Test() {
    1.44 +    printf("Accessed nsITestCom::Test() from COM\n");
    1.45 +    return NS_OK;
    1.46 +  }
    1.47 +
    1.48 +private:
    1.49 +  ~nsTestCom() {
    1.50 +    printf("nsTestCom instance successfully deleted\n");
    1.51 +  }
    1.52 +};
    1.53 +
    1.54 +NS_IMPL_QUERY_INTERFACE(nsTestCom, nsITestCom)
    1.55 +
    1.56 +MozExternalRefCountType nsTestCom::AddRef()
    1.57 +{
    1.58 +  nsrefcnt res = ++mRefCnt;
    1.59 +  NS_LOG_ADDREF(this, mRefCnt, "nsTestCom", sizeof(*this));
    1.60 +  printf("nsTestCom: Adding ref = %d\n", res);
    1.61 +  return res;
    1.62 +}
    1.63 +
    1.64 +MozExternalRefCountType nsTestCom::Release()
    1.65 +{
    1.66 +  nsrefcnt res = --mRefCnt;
    1.67 +  NS_LOG_RELEASE(this, mRefCnt, "nsTestCom");
    1.68 +  printf("nsTestCom: Releasing = %d\n", res);
    1.69 +  if (res == 0) {
    1.70 +    delete this;
    1.71 +  }
    1.72 +  return res;
    1.73 +}
    1.74 +
    1.75 +class nsTestComFactory MOZ_FINAL : public nsIFactory {
    1.76 +  NS_DECL_ISUPPORTS
    1.77 +public:
    1.78 +  nsTestComFactory() {
    1.79 +  }
    1.80 +  
    1.81 +  NS_IMETHOD CreateInstance(nsISupports *aOuter,
    1.82 +                            const nsIID &aIID,
    1.83 +                            void **aResult);
    1.84 +
    1.85 +  NS_IMETHOD LockFactory(bool aLock) {
    1.86 +    printf("nsTestComFactory: ");
    1.87 +    printf("%s", (aLock ? "Locking server" : "Unlocking server"));
    1.88 +    printf("\n");
    1.89 +    return NS_OK;
    1.90 +  }
    1.91 +};
    1.92 +
    1.93 +NS_IMPL_ISUPPORTS(nsTestComFactory, nsIFactory)
    1.94 +
    1.95 +nsresult nsTestComFactory::CreateInstance(nsISupports *aOuter,
    1.96 +					  const nsIID &aIID,
    1.97 +					  void **aResult)
    1.98 +{
    1.99 +  if (aOuter != nullptr) {
   1.100 +    return NS_ERROR_NO_AGGREGATION;
   1.101 +  }
   1.102 +
   1.103 +  nsTestCom *t = new nsTestCom();
   1.104 +  
   1.105 +  if (t == nullptr) {
   1.106 +    return NS_ERROR_OUT_OF_MEMORY;
   1.107 +  }
   1.108 +  
   1.109 +  NS_ADDREF(t);
   1.110 +  nsresult res = t->QueryInterface(aIID, aResult);
   1.111 +  NS_RELEASE(t);
   1.112 +
   1.113 +  if (NS_SUCCEEDED(res)) {
   1.114 +    printf("nsTestComFactory: successfully created nsTestCom instance\n");
   1.115 +  }
   1.116 +
   1.117 +  return res;
   1.118 +}
   1.119 +
   1.120 +/*
   1.121 + * main
   1.122 + */
   1.123 +
   1.124 +int main(int argc, char *argv[])
   1.125 +{
   1.126 +  nsTestComFactory *inst = new nsTestComFactory();
   1.127 +  IClassFactory *iFactory;
   1.128 +  inst->QueryInterface(NS_GET_IID(nsIFactory), (void **) &iFactory);
   1.129 +
   1.130 +  IUnknown *iUnknown;  
   1.131 +  nsITestCom *iTestCom;
   1.132 +
   1.133 +  iFactory->LockServer(TRUE);
   1.134 +  iFactory->CreateInstance(nullptr, IID_IUnknown, (void **) &iUnknown);
   1.135 +  iFactory->LockServer(FALSE);
   1.136 +
   1.137 +  GUID testGUID = NS_ITEST_COM_IID;
   1.138 +  HRESULT hres;
   1.139 +  hres= iUnknown->QueryInterface(testGUID, 
   1.140 +				 (void **) &iTestCom);
   1.141 +
   1.142 +  iTestCom->Test();
   1.143 +
   1.144 +  iUnknown->Release();
   1.145 +  iTestCom->Release();
   1.146 +  iFactory->Release();
   1.147 +
   1.148 +  return 0;
   1.149 +}
   1.150 +

mercurial