|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include <windows.h> |
|
7 #include <unknwn.h> |
|
8 #include <stdio.h> |
|
9 #include "nsISupports.h" |
|
10 #include "nsIFactory.h" |
|
11 |
|
12 // unknwn.h is needed to build with WIN32_LEAN_AND_MEAN |
|
13 #include <unknwn.h> |
|
14 |
|
15 // {5846BA30-B856-11d1-A98A-00805F8A7AC4} |
|
16 #define NS_ITEST_COM_IID \ |
|
17 { 0x5846ba30, 0xb856, 0x11d1, \ |
|
18 { 0xa9, 0x8a, 0x0, 0x80, 0x5f, 0x8a, 0x7a, 0xc4 } } |
|
19 |
|
20 class nsITestCom: public nsISupports |
|
21 { |
|
22 public: |
|
23 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ITEST_COM_IID) |
|
24 NS_IMETHOD Test() = 0; |
|
25 }; |
|
26 |
|
27 NS_DEFINE_STATIC_IID_ACCESSOR(nsITestCom, NS_ITEST_COM_IID) |
|
28 |
|
29 /* |
|
30 * nsTestCom |
|
31 */ |
|
32 |
|
33 class nsTestCom MOZ_FINAL : public nsITestCom { |
|
34 NS_DECL_ISUPPORTS |
|
35 |
|
36 public: |
|
37 nsTestCom() { |
|
38 } |
|
39 |
|
40 NS_IMETHOD Test() { |
|
41 printf("Accessed nsITestCom::Test() from COM\n"); |
|
42 return NS_OK; |
|
43 } |
|
44 |
|
45 private: |
|
46 ~nsTestCom() { |
|
47 printf("nsTestCom instance successfully deleted\n"); |
|
48 } |
|
49 }; |
|
50 |
|
51 NS_IMPL_QUERY_INTERFACE(nsTestCom, nsITestCom) |
|
52 |
|
53 MozExternalRefCountType nsTestCom::AddRef() |
|
54 { |
|
55 nsrefcnt res = ++mRefCnt; |
|
56 NS_LOG_ADDREF(this, mRefCnt, "nsTestCom", sizeof(*this)); |
|
57 printf("nsTestCom: Adding ref = %d\n", res); |
|
58 return res; |
|
59 } |
|
60 |
|
61 MozExternalRefCountType nsTestCom::Release() |
|
62 { |
|
63 nsrefcnt res = --mRefCnt; |
|
64 NS_LOG_RELEASE(this, mRefCnt, "nsTestCom"); |
|
65 printf("nsTestCom: Releasing = %d\n", res); |
|
66 if (res == 0) { |
|
67 delete this; |
|
68 } |
|
69 return res; |
|
70 } |
|
71 |
|
72 class nsTestComFactory MOZ_FINAL : public nsIFactory { |
|
73 NS_DECL_ISUPPORTS |
|
74 public: |
|
75 nsTestComFactory() { |
|
76 } |
|
77 |
|
78 NS_IMETHOD CreateInstance(nsISupports *aOuter, |
|
79 const nsIID &aIID, |
|
80 void **aResult); |
|
81 |
|
82 NS_IMETHOD LockFactory(bool aLock) { |
|
83 printf("nsTestComFactory: "); |
|
84 printf("%s", (aLock ? "Locking server" : "Unlocking server")); |
|
85 printf("\n"); |
|
86 return NS_OK; |
|
87 } |
|
88 }; |
|
89 |
|
90 NS_IMPL_ISUPPORTS(nsTestComFactory, nsIFactory) |
|
91 |
|
92 nsresult nsTestComFactory::CreateInstance(nsISupports *aOuter, |
|
93 const nsIID &aIID, |
|
94 void **aResult) |
|
95 { |
|
96 if (aOuter != nullptr) { |
|
97 return NS_ERROR_NO_AGGREGATION; |
|
98 } |
|
99 |
|
100 nsTestCom *t = new nsTestCom(); |
|
101 |
|
102 if (t == nullptr) { |
|
103 return NS_ERROR_OUT_OF_MEMORY; |
|
104 } |
|
105 |
|
106 NS_ADDREF(t); |
|
107 nsresult res = t->QueryInterface(aIID, aResult); |
|
108 NS_RELEASE(t); |
|
109 |
|
110 if (NS_SUCCEEDED(res)) { |
|
111 printf("nsTestComFactory: successfully created nsTestCom instance\n"); |
|
112 } |
|
113 |
|
114 return res; |
|
115 } |
|
116 |
|
117 /* |
|
118 * main |
|
119 */ |
|
120 |
|
121 int main(int argc, char *argv[]) |
|
122 { |
|
123 nsTestComFactory *inst = new nsTestComFactory(); |
|
124 IClassFactory *iFactory; |
|
125 inst->QueryInterface(NS_GET_IID(nsIFactory), (void **) &iFactory); |
|
126 |
|
127 IUnknown *iUnknown; |
|
128 nsITestCom *iTestCom; |
|
129 |
|
130 iFactory->LockServer(TRUE); |
|
131 iFactory->CreateInstance(nullptr, IID_IUnknown, (void **) &iUnknown); |
|
132 iFactory->LockServer(FALSE); |
|
133 |
|
134 GUID testGUID = NS_ITEST_COM_IID; |
|
135 HRESULT hres; |
|
136 hres= iUnknown->QueryInterface(testGUID, |
|
137 (void **) &iTestCom); |
|
138 |
|
139 iTestCom->Test(); |
|
140 |
|
141 iUnknown->Release(); |
|
142 iTestCom->Release(); |
|
143 iFactory->Release(); |
|
144 |
|
145 return 0; |
|
146 } |
|
147 |