opengl32: Avoid generating a wrapper for internal functions when we can call the...
[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%08x\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%08x\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 %08x\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%08x\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%08x\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%08x\n", hr);
97
98     hr = CLSIDFromProgID(NULL, &clsid);
99     ok(hr == E_INVALIDARG, "CLSIDFromProgID should have returned E_INVALIDARG instead of 0x%08x\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%08x\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%08x\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%08x\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%08x\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%08x\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     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
254     ok(hr == CO_E_NOT_SUPPORTED,
255         "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
256         hr);
257
258     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
259     prev_filter = (IMessageFilter *)0xdeadbeef;
260     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
261     ok(hr == CO_E_NOT_SUPPORTED,
262         "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08x\n",
263         hr);
264     ok(prev_filter == (IMessageFilter *)0xdeadbeef,
265         "prev_filter should have been set to %p\n", prev_filter);
266     CoUninitialize();
267
268     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
269
270     hr = CoRegisterMessageFilter(NULL, NULL);
271     ok_ole_success(hr, "CoRegisterMessageFilter");
272
273     prev_filter = (IMessageFilter *)0xdeadbeef;
274     hr = CoRegisterMessageFilter(NULL, &prev_filter);
275     ok_ole_success(hr, "CoRegisterMessageFilter");
276     ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
277
278     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
279     ok_ole_success(hr, "CoRegisterMessageFilter");
280     ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
281
282     hr = CoRegisterMessageFilter(NULL, NULL);
283     ok_ole_success(hr, "CoRegisterMessageFilter");
284
285     CoUninitialize();
286 }
287
288 static HRESULT WINAPI Test_IUnknown_QueryInterface(
289     LPUNKNOWN iface,
290     REFIID riid,
291     LPVOID *ppvObj)
292 {
293     if (ppvObj == NULL) return E_POINTER;
294
295     if (IsEqualIID(riid, &IID_IUnknown) ||
296         IsEqualIID(riid, &IID_IWineTest))
297     {
298         *ppvObj = (LPVOID)iface;
299         IUnknown_AddRef(iface);
300         return S_OK;
301     }
302
303     *ppvObj = NULL;
304     return E_NOINTERFACE;
305 }
306
307 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
308 {
309     return 2; /* non-heap-based object */
310 }
311
312 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
313 {
314     return 1; /* non-heap-based object */
315 }
316
317 static const IUnknownVtbl TestUnknown_Vtbl =
318 {
319     Test_IUnknown_QueryInterface,
320     Test_IUnknown_AddRef,
321     Test_IUnknown_Release,
322 };
323
324 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
325
326 static HRESULT WINAPI PSFactoryBuffer_QueryInterface(
327     IPSFactoryBuffer * This,
328     /* [in] */ REFIID riid,
329     /* [iid_is][out] */ void **ppvObject)
330 {
331     if (IsEqualIID(riid, &IID_IUnknown) ||
332         IsEqualIID(riid, &IID_IPSFactoryBuffer))
333     {
334         *ppvObject = This;
335         IPSFactoryBuffer_AddRef(This);
336         return S_OK;
337     }
338     return E_NOINTERFACE;
339 }
340         
341 static ULONG WINAPI PSFactoryBuffer_AddRef(
342     IPSFactoryBuffer * This)
343 {
344     return 2;
345 }
346
347 static ULONG WINAPI PSFactoryBuffer_Release(
348     IPSFactoryBuffer * This)
349 {
350     return 1;
351 }
352
353 static HRESULT WINAPI PSFactoryBuffer_CreateProxy(
354     IPSFactoryBuffer * This,
355     /* [in] */ IUnknown *pUnkOuter,
356     /* [in] */ REFIID riid,
357     /* [out] */ IRpcProxyBuffer **ppProxy,
358     /* [out] */ void **ppv)
359 {
360     return E_NOTIMPL;
361 }
362         
363 static HRESULT WINAPI PSFactoryBuffer_CreateStub(
364     IPSFactoryBuffer * This,
365     /* [in] */ REFIID riid,
366     /* [unique][in] */ IUnknown *pUnkServer,
367     /* [out] */ IRpcStubBuffer **ppStub)
368 {
369     return E_NOTIMPL;
370 }
371
372 static IPSFactoryBufferVtbl PSFactoryBufferVtbl =
373 {
374     PSFactoryBuffer_QueryInterface,
375     PSFactoryBuffer_AddRef,
376     PSFactoryBuffer_Release,
377     PSFactoryBuffer_CreateProxy,
378     PSFactoryBuffer_CreateStub
379 };
380
381 static IPSFactoryBuffer PSFactoryBuffer = { &PSFactoryBufferVtbl };
382
383 static const CLSID CLSID_WineTestPSFactoryBuffer =
384 {
385     0x52011640,
386     0x8164,
387     0x4fd0,
388     {0xa1, 0xa2, 0x5d, 0x5a, 0x36, 0x54, 0xd3, 0xbd}
389 }; /* 52011640-8164-4fd0-a1a2-5d5a3654d3bd */
390
391 static void test_CoRegisterPSClsid(void)
392 {
393     HRESULT hr;
394     DWORD dwRegistrationKey;
395     IStream *stream;
396     CLSID clsid;
397
398     hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
399     ok(hr == CO_E_NOTINITIALIZED, "CoRegisterPSClsid should have returened CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
400
401     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
402
403     hr = CoRegisterClassObject(&CLSID_WineTestPSFactoryBuffer, (IUnknown *)&PSFactoryBuffer,
404         CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwRegistrationKey);
405     ok_ole_success(hr, "CoRegisterClassObject");
406
407     hr = CoRegisterPSClsid(&IID_IWineTest, &CLSID_WineTestPSFactoryBuffer);
408     ok_ole_success(hr, "CoRegisterPSClsid");
409
410     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
411     ok_ole_success(hr, "CreateStreamOnHGlobal");
412
413     hr = CoMarshalInterface(stream, &IID_IWineTest, (IUnknown *)&Test_Unknown, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
414     ok(hr == E_NOTIMPL, "CoMarshalInterface should have returned E_NOTIMPL instead of 0x%08x\n", hr);
415     IStream_Release(stream);
416
417     hr = CoRevokeClassObject(dwRegistrationKey);
418     ok_ole_success(hr, "CoRevokeClassObject");
419
420     CoUninitialize();
421
422     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
423
424     hr = CoGetPSClsid(&IID_IWineTest, &clsid);
425     ok(hr == REGDB_E_IIDNOTREG, "CoGetPSClsid should have returned REGDB_E_IIDNOTREG instead of 0x%08x\n", hr);
426
427     CoUninitialize();
428 }
429
430 static void test_CoGetPSClsid(void)
431 {
432     HRESULT hr;
433     CLSID clsid;
434
435     hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
436     ok(hr == CO_E_NOTINITIALIZED,
437        "CoGetPSClsid should have returned CO_E_NOTINITIALIZED instead of 0x%08x\n",
438        hr);
439
440     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
441
442     hr = CoGetPSClsid(&IID_IClassFactory, &clsid);
443     ok_ole_success(hr, "CoGetPSClsid");
444
445     hr = CoGetPSClsid(&IID_IWineTest, &clsid);
446     ok(hr == REGDB_E_IIDNOTREG,
447        "CoGetPSClsid for random IID returned 0x%08x instead of REGDB_E_IIDNOTREG\n",
448        hr);
449
450     hr = CoGetPSClsid(&IID_IClassFactory, NULL);
451     ok(hr == E_INVALIDARG,
452        "CoGetPSClsid for null clsid returned 0x%08x instead of E_INVALIDARG\n",
453        hr);
454
455     CoUninitialize();
456 }
457
458
459 START_TEST(compobj)
460 {
461     HMODULE hOle32 = GetModuleHandle("ole32");
462     if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx")))
463     {
464         trace("You need DCOM95 installed to run this test\n");
465         return;
466     }
467
468     test_ProgIDFromCLSID();
469     test_CLSIDFromProgID();
470     test_CLSIDFromString();
471     test_CoCreateInstance();
472     test_ole_menu();
473     test_CoGetClassObject();
474     test_CoRegisterMessageFilter();
475     test_CoRegisterPSClsid();
476     test_CoGetPSClsid();
477 }