Fix rpcndr.h so it takes CONST_VTABLE into account.
[wine] / dlls / ole32 / tests / compobj.c
1 /*
2  * Component Object Tests
3  *
4  * Copyright 2005 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #define CONST_VTABLE
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "objbase.h"
29 #include "shlguid.h"
30
31 #include "wine/test.h"
32
33 /* functions that are not present on all versions of Windows */
34 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
35
36 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08lx\n", hr)
37
38 static const CLSID CLSID_non_existent =   { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
39 static const CLSID CLSID_CDeviceMoniker = { 0x4315d437, 0x5b8c, 0x11d0, { 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86 } };
40 static const WCHAR devicedotone[] = {'d','e','v','i','c','e','.','1',0};
41 static const WCHAR wszNonExistent[] = {'N','o','n','E','x','i','s','t','e','n','t',0};
42 static const WCHAR wszCLSID_CDeviceMoniker[] =
43 {
44     '{',
45     '4','3','1','5','d','4','3','7','-',
46     '5','b','8','c','-',
47     '1','1','d','0','-',
48     'b','d','3','b','-',
49     '0','0','a','0','c','9','1','1','c','e','8','6',
50     '}',0
51 };
52
53 static const IID IID_IWineTest =
54 {
55     0x5201163f,
56     0x8164,
57     0x4fd0,
58     {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
59 }; /* 5201163f-8164-4fd0-a1a2-5d5a3654d3bd */
60
61
62 static void test_ProgIDFromCLSID(void)
63 {
64     LPWSTR progid;
65     HRESULT hr = ProgIDFromCLSID(&CLSID_CDeviceMoniker, &progid);
66     ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08lx\n", hr);
67     if (hr == S_OK)
68     {
69         ok(!lstrcmpiW(progid, devicedotone), "Didn't get expected prog ID\n");
70         CoTaskMemFree(progid);
71     }
72
73     progid = (LPWSTR)0xdeadbeef;
74     hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
75     ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08lx\n", hr);
76     ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
77
78     hr = ProgIDFromCLSID(&CLSID_CDeviceMoniker, NULL);
79     ok(hr == E_INVALIDARG, "ProgIDFromCLSID should return E_INVALIDARG instead of 0x%08lx\n", hr);
80 }
81
82 static void test_CLSIDFromProgID(void)
83 {
84     CLSID clsid;
85     HRESULT hr = CLSIDFromProgID(devicedotone, &clsid);
86     ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08lx\n", hr);
87     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
88
89     hr = CLSIDFromString((LPOLESTR)devicedotone, &clsid);
90     ok_ole_success(hr, "CLSIDFromString");
91     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
92
93     /* test some failure cases */
94
95     hr = CLSIDFromProgID(wszNonExistent, NULL);
96     ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08lx\n", hr);
97
98     hr = CLSIDFromProgID(NULL, &clsid);
99     ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08lx\n", hr);
100
101     memset(&clsid, 0xcc, sizeof(clsid));
102     hr = CLSIDFromProgID(wszNonExistent, &clsid);
103     ok(hr == CO_E_CLASSSTRING, "CLSIDFromProgID on nonexistent ProgID should have returned CO_E_CLASSSTRING instead of 0x%08lx\n", hr);
104     ok(IsEqualCLSID(&clsid, &CLSID_NULL), "CLSIDFromProgID should have set clsid to all-zeros on failure\n");
105 }
106
107 static void test_CLSIDFromString(void)
108 {
109     CLSID clsid;
110     HRESULT hr = CLSIDFromString((LPOLESTR)wszCLSID_CDeviceMoniker, &clsid);
111     ok_ole_success(hr, "CLSIDFromString");
112     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
113
114     hr = CLSIDFromString(NULL, &clsid);
115     ok_ole_success(hr, "CLSIDFromString");
116     ok(IsEqualCLSID(&clsid, &CLSID_NULL), "clsid wasn't equal to CLSID_NULL\n");
117 }
118
119 static void test_CoCreateInstance(void)
120 {
121     REFCLSID rclsid = &CLSID_MyComputer;
122     IUnknown *pUnk = (IUnknown *)0xdeadbeef;
123     HRESULT hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
124     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08lx\n", hr);
125     ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
126
127     OleInitialize(NULL);
128     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
129     ok_ole_success(hr, "CoCreateInstance");
130     IUnknown_Release(pUnk);
131     OleUninitialize();
132
133     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
134     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08lx\n", hr);
135 }
136
137 static void test_CoGetClassObject(void)
138 {
139     IUnknown *pUnk = (IUnknown *)0xdeadbeef;
140     HRESULT hr = CoGetClassObject(&CLSID_MyComputer, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
141     ok(hr == CO_E_NOTINITIALIZED, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08lx\n", hr);
142     ok(pUnk == NULL, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
143
144     hr = CoGetClassObject(&CLSID_MyComputer, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
145     ok(hr == E_INVALIDARG, "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08lx\n", hr);
146 }
147
148 static ATOM register_dummy_class(void)
149 {
150     WNDCLASS wc =
151     {
152         0,
153         DefWindowProc,
154         0,
155         0,
156         GetModuleHandle(NULL),
157         NULL,
158         LoadCursor(NULL, IDC_ARROW),
159         (HBRUSH)(COLOR_BTNFACE+1),
160         NULL,
161         TEXT("WineOleTestClass"),
162     };
163     
164     return RegisterClass(&wc);
165 }
166
167 static void test_ole_menu(void)
168 {
169         HWND hwndFrame;
170         HRESULT hr;
171
172         hwndFrame = CreateWindow(MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
173         hr = OleSetMenuDescriptor(NULL, hwndFrame, NULL, NULL, NULL);
174         todo_wine ok_ole_success(hr, "OleSetMenuDescriptor");
175
176         DestroyWindow(hwndFrame);
177 }
178
179
180 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
181 {
182     if (ppvObj == NULL) return E_POINTER;
183
184     if (IsEqualGUID(riid, &IID_IUnknown) ||
185         IsEqualGUID(riid, &IID_IClassFactory))
186     {
187         *ppvObj = (LPVOID)iface;
188         IMessageFilter_AddRef(iface);
189         return S_OK;
190     }
191
192     return E_NOINTERFACE;
193 }
194
195 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
196 {
197     return 2; /* non-heap object */
198 }
199
200 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
201 {
202     return 1; /* non-heap object */
203 }
204
205 static DWORD WINAPI MessageFilter_HandleInComingCall(
206   IMessageFilter *iface,
207   DWORD dwCallType,
208   HTASK threadIDCaller,
209   DWORD dwTickCount,
210   LPINTERFACEINFO lpInterfaceInfo)
211 {
212     trace("HandleInComingCall\n");
213     return SERVERCALL_ISHANDLED;
214 }
215
216 static DWORD WINAPI MessageFilter_RetryRejectedCall(
217   IMessageFilter *iface,
218   HTASK threadIDCallee,
219   DWORD dwTickCount,
220   DWORD dwRejectType)
221 {
222     trace("RetryRejectedCall\n");
223     return 0;
224 }
225
226 static DWORD WINAPI MessageFilter_MessagePending(
227   IMessageFilter *iface,
228   HTASK threadIDCallee,
229   DWORD dwTickCount,
230   DWORD dwPendingType)
231 {
232     trace("MessagePending\n");
233     return PENDINGMSG_WAITNOPROCESS;
234 }
235
236 static const IMessageFilterVtbl MessageFilter_Vtbl =
237 {
238     MessageFilter_QueryInterface,
239     MessageFilter_AddRef,
240     MessageFilter_Release,
241     MessageFilter_HandleInComingCall,
242     MessageFilter_RetryRejectedCall,
243     MessageFilter_MessagePending
244 };
245
246 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
247
248 static void test_CoRegisterMessageFilter(void)
249 {
250     HRESULT hr;
251     IMessageFilter *prev_filter;
252
253 #if 0 /* crashes without an apartment! */
254     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
255 #endif
256
257     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
258     prev_filter = (IMessageFilter *)0xdeadbeef;
259     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
260     ok(hr == CO_E_NOT_SUPPORTED,
261         "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08lx\n",
262         hr);
263     ok(prev_filter == (IMessageFilter *)0xdeadbeef,
264         "prev_filter should have been set to %p\n", prev_filter);
265     CoUninitialize();
266
267     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
268
269     hr = CoRegisterMessageFilter(NULL, NULL);
270     ok_ole_success(hr, "CoRegisterMessageFilter");
271
272     prev_filter = (IMessageFilter *)0xdeadbeef;
273     hr = CoRegisterMessageFilter(NULL, &prev_filter);
274     ok_ole_success(hr, "CoRegisterMessageFilter");
275     ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
276
277     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
278     ok_ole_success(hr, "CoRegisterMessageFilter");
279     ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
280
281     hr = CoRegisterMessageFilter(NULL, NULL);
282     ok_ole_success(hr, "CoRegisterMessageFilter");
283
284     CoUninitialize();
285 }
286
287 static HRESULT WINAPI Test_IUnknown_QueryInterface(
288     LPUNKNOWN iface,
289     REFIID riid,
290     LPVOID *ppvObj)
291 {
292     if (ppvObj == NULL) return E_POINTER;
293
294     if (IsEqualIID(riid, &IID_IUnknown) ||
295         IsEqualIID(riid, &IID_IWineTest))
296     {
297         *ppvObj = (LPVOID)iface;
298         IUnknown_AddRef(iface);
299         return S_OK;
300     }
301
302     *ppvObj = NULL;
303     return E_NOINTERFACE;
304 }
305
306 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
307 {
308     return 2; /* non-heap-based object */
309 }
310
311 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
312 {
313     return 1; /* non-heap-based object */
314 }
315
316 static const IUnknownVtbl TestUnknown_Vtbl =
317 {
318     Test_IUnknown_QueryInterface,
319     Test_IUnknown_AddRef,
320     Test_IUnknown_Release,
321 };
322
323 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
324
325 static HRESULT WINAPI PSFactoryBuffer_QueryInterface(
326     IPSFactoryBuffer * This,
327     /* [in] */ REFIID riid,
328     /* [iid_is][out] */ void **ppvObject)
329 {
330     if (IsEqualIID(riid, &IID_IUnknown) ||
331         IsEqualIID(riid, &IID_IPSFactoryBuffer))
332     {
333         *ppvObject = This;
334         IPSFactoryBuffer_AddRef(This);
335         return S_OK;
336     }
337     return E_NOINTERFACE;
338 }
339         
340 static ULONG WINAPI PSFactoryBuffer_AddRef(
341     IPSFactoryBuffer * This)
342 {
343     return 2;
344 }
345
346 static ULONG WINAPI PSFactoryBuffer_Release(
347     IPSFactoryBuffer * This)
348 {
349     return 1;
350 }
351
352 static HRESULT WINAPI PSFactoryBuffer_CreateProxy(
353     IPSFactoryBuffer * This,
354     /* [in] */ IUnknown *pUnkOuter,
355     /* [in] */ REFIID riid,
356     /* [out] */ IRpcProxyBuffer **ppProxy,
357     /* [out] */ void **ppv)
358 {
359     return E_NOTIMPL;
360 }
361         
362 static HRESULT WINAPI PSFactoryBuffer_CreateStub(
363     IPSFactoryBuffer * This,
364     /* [in] */ REFIID riid,
365     /* [unique][in] */ IUnknown *pUnkServer,
366     /* [out] */ IRpcStubBuffer **ppStub)
367 {
368     return E_NOTIMPL;
369 }
370
371 static IPSFactoryBufferVtbl PSFactoryBufferVtbl =
372 {
373     PSFactoryBuffer_QueryInterface,
374     PSFactoryBuffer_AddRef,
375     PSFactoryBuffer_Release,
376     PSFactoryBuffer_CreateProxy,
377     PSFactoryBuffer_CreateStub
378 };
379
380 static IPSFactoryBuffer PSFactoryBuffer = { &PSFactoryBufferVtbl };
381
382 static const CLSID CLSID_WineTestPSFactoryBuffer =
383 {
384     0x52011640,
385     0x8164,
386     0x4fd0,
387     {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
388 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
389
390 static void test_CoRegisterPSClsid(void)
391 {
392     HRESULT hr;
393     DWORD dwRegistrationKey;
394     IStream *stream;
395     CLSID clsid;
396
397     hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
398     ok(hr == CO_E_NOTINITIALIZED, "CoRegisterPSClsid should have returened CO_E_NOTINITIALIZED instead of 0x%08lx\n", hr);
399
400     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
401
402     hr = CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer, (IUnknown *)&PSFactoryBuffer,
403         CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwRegistrationKey);
404     ok_ole_success(hr, "CoRegisterClassObject");
405
406     hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
407     ok_ole_success(hr, "CoRegisterPSClsid");
408
409     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
410     ok_ole_success(hr, "CreateStreamOnHGlobal");
411
412     hr = CoMarshalInterface(stream, &IID_IWineTest, (IUnknown *)&Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
413     ok(hr == E_NOTIMPL, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08lx\n", hr);
414     IStream_Release(stream);
415
416     hr = CoRevokeClassObject(dwRegistrationKey);
417     ok_ole_success(hr, "CoRevokeClassObject");
418
419     CoUninitialize();
420
421     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
422
423     hr = CoGetPSClsid(&IID_IWineTest, &clsid);
424     ok(hr == REGDB_E_IIDNOTREG, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08lx\n", hr);
425
426     CoUninitialize();
427 }
428
429 static void test_CoGetPSClsid(void)
430 {
431     HRESULT hr;
432     CLSID clsid;
433
434     hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
435     ok(hr == CO_E_NOTINITIALIZED,
436        "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08lx\n",
437        hr);
438
439     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
440
441     hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
442     ok_ole_success(hr, "CoGetPSClsid");
443
444     hr = CoGetPSClsid(&IID_IWineTest, &clsid);
445     ok(hr == REGDB_E_IIDNOTREG,
446        "CoGetPSClsid for random IID returned 0x%08lx instead of REGDB_E_IIDNOTREG\n",
447        hr);
448
449     hr = CoGetPSClsid(&IID_IClassFactory, NULL);
450     ok(hr == E_INVALIDARG,
451        "CoGetPSClsid for null clsid returned 0x%08lx instead of E_INVALIDARG\n",
452        hr);
453
454     CoUninitialize();
455 }
456
457
458 START_TEST(compobj)
459 {
460     HMODULE hOle32 = GetModuleHandle("ole32");
461     if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx")))
462     {
463         trace("You need DCOM95 installed to run this test\n");
464         return;
465     }
466
467     test_ProgIDFromCLSID();
468     test_CLSIDFromProgID();
469     test_CLSIDFromString();
470     test_CoCreateInstance();
471     test_ole_menu();
472     test_CoGetClassObject();
473     test_CoRegisterMessageFilter();
474     test_CoRegisterPSClsid();
475     test_CoGetPSClsid();
476 }