ole: Test the behaviour of CoCreateInstance with an uninitialized apartment.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define COBJMACROS
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "shlguid.h"
29
30 #include "wine/test.h"
31
32 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08lx\n", hr)
33
34 static const CLSID CLSID_non_existent =   { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
35 static const CLSID CLSID_CDeviceMoniker = { 0x4315d437, 0x5b8c, 0x11d0, { 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86 } };
36 static const WCHAR devicedotone[] = {'d','e','v','i','c','e','.','1',0};
37 static const WCHAR wszCLSID_CDeviceMoniker[] =
38 {
39     '{',
40     '4','3','1','5','d','4','3','7','-',
41     '5','b','8','c','-',
42     '1','1','d','0','-',
43     'b','d','3','b','-',
44     '0','0','a','0','c','9','1','1','c','e','8','6',
45     '}',0
46 };
47
48 static void test_ProgIDFromCLSID(void)
49 {
50     LPWSTR progid;
51     HRESULT hr = ProgIDFromCLSID(&CLSID_CDeviceMoniker, &progid);
52     ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08lx\n", hr);
53     if (hr == S_OK)
54     {
55         ok(!lstrcmpiW(progid, devicedotone), "Didn't get expected prog ID\n");
56         CoTaskMemFree(progid);
57     }
58
59     progid = (LPWSTR)0xdeadbeef;
60     hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
61     ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08lx\n", hr);
62     ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
63 }
64
65 static void test_CLSIDFromProgID(void)
66 {
67     CLSID clsid;
68     HRESULT hr = CLSIDFromProgID(devicedotone, &clsid);
69     ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08lx\n", hr);
70     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
71
72     hr = CLSIDFromString((LPOLESTR)devicedotone, &clsid);
73     ok_ole_success(hr, "CLSIDFromString");
74     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
75 }
76
77 static void test_CLSIDFromString(void)
78 {
79     CLSID clsid;
80     HRESULT hr = CLSIDFromString((LPOLESTR)wszCLSID_CDeviceMoniker, &clsid);
81     ok_ole_success(hr, "CLSIDFromString");
82     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
83 }
84
85 static void test_CoCreateInstance(void)
86 {
87     REFCLSID rclsid = &CLSID_MyComputer;
88     IUnknown *pUnk = (IUnknown *)0xdeadbeef;
89     HRESULT hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
90     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have return CO_E_NOTINITIALIZED instead of 0x%08lx", hr);
91     todo_wine {
92     ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
93     }
94
95     OleInitialize(NULL);
96     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
97     ok_ole_success(hr, "CoCreateInstance");
98     IUnknown_Release(pUnk);
99     OleUninitialize();
100
101     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
102     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have return CO_E_NOTINITIALIZED instead of 0x%08lx", hr);
103 }
104
105 START_TEST(compobj)
106 {
107     test_ProgIDFromCLSID();
108     test_CLSIDFromProgID();
109     test_CLSIDFromString();
110     test_CoCreateInstance();
111 }