michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "nsISupports.h" michael@0: #include "nsIFactory.h" michael@0: michael@0: // unknwn.h is needed to build with WIN32_LEAN_AND_MEAN michael@0: #include michael@0: michael@0: // {5846BA30-B856-11d1-A98A-00805F8A7AC4} michael@0: #define NS_ITEST_COM_IID \ michael@0: { 0x5846ba30, 0xb856, 0x11d1, \ michael@0: { 0xa9, 0x8a, 0x0, 0x80, 0x5f, 0x8a, 0x7a, 0xc4 } } michael@0: michael@0: class nsITestCom: public nsISupports michael@0: { michael@0: public: michael@0: NS_DECLARE_STATIC_IID_ACCESSOR(NS_ITEST_COM_IID) michael@0: NS_IMETHOD Test() = 0; michael@0: }; michael@0: michael@0: NS_DEFINE_STATIC_IID_ACCESSOR(nsITestCom, NS_ITEST_COM_IID) michael@0: michael@0: /* michael@0: * nsTestCom michael@0: */ michael@0: michael@0: class nsTestCom MOZ_FINAL : public nsITestCom { michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: public: michael@0: nsTestCom() { michael@0: } michael@0: michael@0: NS_IMETHOD Test() { michael@0: printf("Accessed nsITestCom::Test() from COM\n"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: ~nsTestCom() { michael@0: printf("nsTestCom instance successfully deleted\n"); michael@0: } michael@0: }; michael@0: michael@0: NS_IMPL_QUERY_INTERFACE(nsTestCom, nsITestCom) michael@0: michael@0: MozExternalRefCountType nsTestCom::AddRef() michael@0: { michael@0: nsrefcnt res = ++mRefCnt; michael@0: NS_LOG_ADDREF(this, mRefCnt, "nsTestCom", sizeof(*this)); michael@0: printf("nsTestCom: Adding ref = %d\n", res); michael@0: return res; michael@0: } michael@0: michael@0: MozExternalRefCountType nsTestCom::Release() michael@0: { michael@0: nsrefcnt res = --mRefCnt; michael@0: NS_LOG_RELEASE(this, mRefCnt, "nsTestCom"); michael@0: printf("nsTestCom: Releasing = %d\n", res); michael@0: if (res == 0) { michael@0: delete this; michael@0: } michael@0: return res; michael@0: } michael@0: michael@0: class nsTestComFactory MOZ_FINAL : public nsIFactory { michael@0: NS_DECL_ISUPPORTS michael@0: public: michael@0: nsTestComFactory() { michael@0: } michael@0: michael@0: NS_IMETHOD CreateInstance(nsISupports *aOuter, michael@0: const nsIID &aIID, michael@0: void **aResult); michael@0: michael@0: NS_IMETHOD LockFactory(bool aLock) { michael@0: printf("nsTestComFactory: "); michael@0: printf("%s", (aLock ? "Locking server" : "Unlocking server")); michael@0: printf("\n"); michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsTestComFactory, nsIFactory) michael@0: michael@0: nsresult nsTestComFactory::CreateInstance(nsISupports *aOuter, michael@0: const nsIID &aIID, michael@0: void **aResult) michael@0: { michael@0: if (aOuter != nullptr) { michael@0: return NS_ERROR_NO_AGGREGATION; michael@0: } michael@0: michael@0: nsTestCom *t = new nsTestCom(); michael@0: michael@0: if (t == nullptr) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: NS_ADDREF(t); michael@0: nsresult res = t->QueryInterface(aIID, aResult); michael@0: NS_RELEASE(t); michael@0: michael@0: if (NS_SUCCEEDED(res)) { michael@0: printf("nsTestComFactory: successfully created nsTestCom instance\n"); michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: /* michael@0: * main michael@0: */ michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: nsTestComFactory *inst = new nsTestComFactory(); michael@0: IClassFactory *iFactory; michael@0: inst->QueryInterface(NS_GET_IID(nsIFactory), (void **) &iFactory); michael@0: michael@0: IUnknown *iUnknown; michael@0: nsITestCom *iTestCom; michael@0: michael@0: iFactory->LockServer(TRUE); michael@0: iFactory->CreateInstance(nullptr, IID_IUnknown, (void **) &iUnknown); michael@0: iFactory->LockServer(FALSE); michael@0: michael@0: GUID testGUID = NS_ITEST_COM_IID; michael@0: HRESULT hres; michael@0: hres= iUnknown->QueryInterface(testGUID, michael@0: (void **) &iTestCom); michael@0: michael@0: iTestCom->Test(); michael@0: michael@0: iUnknown->Release(); michael@0: iTestCom->Release(); michael@0: iFactory->Release(); michael@0: michael@0: return 0; michael@0: } michael@0: