1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Pierre Phaneuf <pp@ludusdesign.com>
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
27 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #include <windows.h>
|
---|
40 | #include <unknwn.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include "nsISupports.h"
|
---|
43 | #include "nsIFactory.h"
|
---|
44 |
|
---|
45 | // unknwn.h is needed to build with WIN32_LEAN_AND_MEAN
|
---|
46 | #include <unknwn.h>
|
---|
47 |
|
---|
48 | // {5846BA30-B856-11d1-A98A-00805F8A7AC4}
|
---|
49 | #define NS_ITEST_COM_IID \
|
---|
50 | { 0x5846ba30, 0xb856, 0x11d1, \
|
---|
51 | { 0xa9, 0x8a, 0x0, 0x80, 0x5f, 0x8a, 0x7a, 0xc4 } }
|
---|
52 |
|
---|
53 | class nsITestCom: public nsISupports
|
---|
54 | {
|
---|
55 | public:
|
---|
56 | NS_DEFINE_STATIC_IID_ACCESSOR(NS_ITEST_COM_IID)
|
---|
57 | NS_IMETHOD Test() = 0;
|
---|
58 | };
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * nsTestCom
|
---|
62 | */
|
---|
63 |
|
---|
64 | class nsTestCom: public nsITestCom {
|
---|
65 | NS_DECL_ISUPPORTS
|
---|
66 |
|
---|
67 | public:
|
---|
68 | nsTestCom() {
|
---|
69 | }
|
---|
70 |
|
---|
71 | NS_IMETHOD Test() {
|
---|
72 | printf("Accessed nsITestCom::Test() from COM\n");
|
---|
73 | return NS_OK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | private:
|
---|
77 | ~nsTestCom() {
|
---|
78 | printf("nsTestCom instance successfully deleted\n");
|
---|
79 | }
|
---|
80 | };
|
---|
81 |
|
---|
82 | NS_IMPL_QUERY_INTERFACE1(nsTestCom, nsITestCom)
|
---|
83 |
|
---|
84 | nsrefcnt nsTestCom::AddRef()
|
---|
85 | {
|
---|
86 | nsrefcnt res = ++mRefCnt;
|
---|
87 | NS_LOG_ADDREF(this, mRefCnt, "nsTestCom", sizeof(*this));
|
---|
88 | printf("nsTestCom: Adding ref = %d\n", res);
|
---|
89 | return res;
|
---|
90 | }
|
---|
91 |
|
---|
92 | nsrefcnt nsTestCom::Release()
|
---|
93 | {
|
---|
94 | nsrefcnt res = --mRefCnt;
|
---|
95 | NS_LOG_RELEASE(this, mRefCnt, "nsTestCom");
|
---|
96 | printf("nsTestCom: Releasing = %d\n", res);
|
---|
97 | if (res == 0) {
|
---|
98 | delete this;
|
---|
99 | }
|
---|
100 | return res;
|
---|
101 | }
|
---|
102 |
|
---|
103 | class nsTestComFactory: public nsIFactory {
|
---|
104 | NS_DECL_ISUPPORTS
|
---|
105 | public:
|
---|
106 | nsTestComFactory() {
|
---|
107 | }
|
---|
108 |
|
---|
109 | NS_IMETHOD CreateInstance(nsISupports *aOuter,
|
---|
110 | const nsIID &aIID,
|
---|
111 | void **aResult);
|
---|
112 |
|
---|
113 | NS_IMETHOD LockFactory(PRBool aLock) {
|
---|
114 | printf("nsTestComFactory: ");
|
---|
115 | printf("%s", (aLock == PR_TRUE ? "Locking server" : "Unlocking server"));
|
---|
116 | printf("\n");
|
---|
117 | return S_OK;
|
---|
118 | }
|
---|
119 | };
|
---|
120 |
|
---|
121 | NS_IMPL_ISUPPORTS1(nsTestComFactory, nsIFactory)
|
---|
122 |
|
---|
123 | nsresult nsTestComFactory::CreateInstance(nsISupports *aOuter,
|
---|
124 | const nsIID &aIID,
|
---|
125 | void **aResult)
|
---|
126 | {
|
---|
127 | if (aOuter != NULL) {
|
---|
128 | return NS_ERROR_NO_AGGREGATION;
|
---|
129 | }
|
---|
130 |
|
---|
131 | nsTestCom *t = new nsTestCom();
|
---|
132 |
|
---|
133 | if (t == NULL) {
|
---|
134 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
135 | }
|
---|
136 |
|
---|
137 | NS_ADDREF(t);
|
---|
138 | nsresult res = t->QueryInterface(aIID, aResult);
|
---|
139 | NS_RELEASE(t);
|
---|
140 |
|
---|
141 | if (NS_SUCCEEDED(res)) {
|
---|
142 | printf("nsTestComFactory: successfully created nsTestCom instance\n");
|
---|
143 | }
|
---|
144 |
|
---|
145 | return res;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * main
|
---|
150 | */
|
---|
151 |
|
---|
152 | int main(int argc, char *argv[])
|
---|
153 | {
|
---|
154 | nsTestComFactory *inst = new nsTestComFactory();
|
---|
155 | IClassFactory *iFactory;
|
---|
156 | inst->QueryInterface(NS_GET_IID(nsIFactory), (void **) &iFactory);
|
---|
157 |
|
---|
158 | IUnknown *iUnknown;
|
---|
159 | nsITestCom *iTestCom;
|
---|
160 |
|
---|
161 | nsresult res;
|
---|
162 | iFactory->LockServer(TRUE);
|
---|
163 | res = iFactory->CreateInstance(NULL,
|
---|
164 | IID_IUnknown,
|
---|
165 | (void **) &iUnknown);
|
---|
166 | iFactory->LockServer(FALSE);
|
---|
167 |
|
---|
168 | GUID testGUID = NS_ITEST_COM_IID;
|
---|
169 | HRESULT hres;
|
---|
170 | hres= iUnknown->QueryInterface(testGUID,
|
---|
171 | (void **) &iTestCom);
|
---|
172 |
|
---|
173 | iTestCom->Test();
|
---|
174 |
|
---|
175 | iUnknown->Release();
|
---|
176 | iTestCom->Release();
|
---|
177 | iFactory->Release();
|
---|
178 |
|
---|
179 | return 0;
|
---|
180 | }
|
---|
181 |
|
---|