Sync with trunk r63743.
[reactos.git] / dll / win32 / wbemdisp / locator.c
1 /*
2 * Copyright 2013 Hans Leidekker for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "wbemdisp_private.h"
20
21 #include <winnls.h>
22 #include <oleauto.h>
23
24 struct locator
25 {
26 ISWbemLocator ISWbemLocator_iface;
27 LONG refs;
28 };
29
30 static inline struct locator *impl_from_ISWbemLocator( ISWbemLocator *iface )
31 {
32 return CONTAINING_RECORD( iface, struct locator, ISWbemLocator_iface );
33 }
34
35 static ULONG WINAPI locator_AddRef(
36 ISWbemLocator *iface )
37 {
38 struct locator *locator = impl_from_ISWbemLocator( iface );
39 return InterlockedIncrement( &locator->refs );
40 }
41
42 static ULONG WINAPI locator_Release(
43 ISWbemLocator *iface )
44 {
45 struct locator *locator = impl_from_ISWbemLocator( iface );
46 LONG refs = InterlockedDecrement( &locator->refs );
47 if (!refs)
48 {
49 TRACE( "destroying %p\n", locator );
50 heap_free( locator );
51 }
52 return refs;
53 }
54
55 static HRESULT WINAPI locator_QueryInterface(
56 ISWbemLocator *iface,
57 REFIID riid,
58 void **ppvObject )
59 {
60 struct locator *locator = impl_from_ISWbemLocator( iface );
61
62 TRACE( "%p, %s, %p\n", locator, debugstr_guid( riid ), ppvObject );
63
64 if (IsEqualGUID( riid, &IID_ISWbemLocator ) ||
65 IsEqualGUID( riid, &IID_IDispatch ) ||
66 IsEqualGUID( riid, &IID_IUnknown ))
67 {
68 *ppvObject = iface;
69 }
70 else
71 {
72 FIXME( "interface %s not implemented\n", debugstr_guid(riid) );
73 return E_NOINTERFACE;
74 }
75 ISWbemLocator_AddRef( iface );
76 return S_OK;
77 }
78
79 static HRESULT WINAPI locator_GetTypeInfoCount(
80 ISWbemLocator *iface,
81 UINT *count )
82 {
83 struct locator *locator = impl_from_ISWbemLocator( iface );
84
85 TRACE( "%p, %p\n", locator, count );
86 *count = 1;
87 return S_OK;
88 }
89
90 enum type_id
91 {
92 ISWbemLocator_tid,
93 last_tid
94 };
95
96 static ITypeLib *wbemdisp_typelib;
97 static ITypeInfo *wbemdisp_typeinfo[last_tid];
98
99 static REFIID wbemdisp_tid_id[] =
100 {
101 &IID_ISWbemLocator
102 };
103
104 static HRESULT get_typeinfo( enum type_id tid, ITypeInfo **ret )
105 {
106 HRESULT hr;
107
108 if (!wbemdisp_typelib)
109 {
110 ITypeLib *typelib;
111
112 hr = LoadRegTypeLib( &LIBID_WbemScripting, 1, 2, LOCALE_SYSTEM_DEFAULT, &typelib );
113 if (FAILED( hr ))
114 {
115 ERR( "LoadRegTypeLib failed: %08x\n", hr );
116 return hr;
117 }
118 if (InterlockedCompareExchangePointer( (void **)&wbemdisp_typelib, typelib, NULL ))
119 ITypeLib_Release( typelib );
120 }
121 if (!wbemdisp_typeinfo[tid])
122 {
123 ITypeInfo *typeinfo;
124
125 hr = ITypeLib_GetTypeInfoOfGuid( wbemdisp_typelib, wbemdisp_tid_id[tid], &typeinfo );
126 if (FAILED( hr ))
127 {
128 ERR( "GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(wbemdisp_tid_id[tid]), hr );
129 return hr;
130 }
131 if (InterlockedCompareExchangePointer( (void **)(wbemdisp_typeinfo + tid), typeinfo, NULL ))
132 ITypeInfo_Release( typeinfo );
133 }
134 *ret = wbemdisp_typeinfo[tid];
135 return S_OK;
136 }
137
138 static HRESULT WINAPI locator_GetTypeInfo(
139 ISWbemLocator *iface,
140 UINT index,
141 LCID lcid,
142 ITypeInfo **info )
143 {
144 struct locator *locator = impl_from_ISWbemLocator( iface );
145 TRACE( "%p, %u, %u, %p\n", locator, index, lcid, info );
146
147 return get_typeinfo( ISWbemLocator_tid, info );
148 }
149
150 static HRESULT WINAPI locator_GetIDsOfNames(
151 ISWbemLocator *iface,
152 REFIID riid,
153 LPOLESTR *names,
154 UINT count,
155 LCID lcid,
156 DISPID *dispid )
157 {
158 struct locator *locator = impl_from_ISWbemLocator( iface );
159 ITypeInfo *typeinfo;
160 HRESULT hr;
161
162 TRACE( "%p, %s, %p, %u, %u, %p\n", locator, debugstr_guid(riid), names, count, lcid, dispid );
163
164 if (!names || !count || !dispid) return E_INVALIDARG;
165
166 hr = get_typeinfo( ISWbemLocator_tid, &typeinfo );
167 if (SUCCEEDED(hr))
168 {
169 hr = ITypeInfo_GetIDsOfNames( typeinfo, names, count, dispid );
170 ITypeInfo_Release( typeinfo );
171 }
172 return hr;
173 }
174
175 static HRESULT WINAPI locator_Invoke(
176 ISWbemLocator *iface,
177 DISPID member,
178 REFIID riid,
179 LCID lcid,
180 WORD flags,
181 DISPPARAMS *params,
182 VARIANT *result,
183 EXCEPINFO *excep_info,
184 UINT *arg_err )
185 {
186 struct locator *locator = impl_from_ISWbemLocator( iface );
187 ITypeInfo *typeinfo;
188 HRESULT hr;
189
190 TRACE( "%p, %d, %s, %d, %d, %p, %p, %p, %p\n", locator, member, debugstr_guid(riid),
191 lcid, flags, params, result, excep_info, arg_err );
192
193 hr = get_typeinfo( ISWbemLocator_tid, &typeinfo );
194 if (SUCCEEDED(hr))
195 {
196 hr = ITypeInfo_Invoke( typeinfo, &locator->ISWbemLocator_iface, member, flags,
197 params, result, excep_info, arg_err );
198 ITypeInfo_Release( typeinfo );
199 }
200 return hr;
201 }
202
203 static HRESULT WINAPI locator_ConnectServer(
204 ISWbemLocator *iface,
205 BSTR strServer,
206 BSTR strNamespace,
207 BSTR strUser,
208 BSTR strPassword,
209 BSTR strLocale,
210 BSTR strAuthority,
211 LONG iSecurityFlags,
212 IDispatch *objWbemNamedValueSet,
213 ISWbemServices **objWbemServices )
214 {
215 FIXME( "%p, %s, %s, %s, %p, %s, %s, 0x%08x, %p, %p\n", iface, debugstr_w(strServer),
216 debugstr_w(strNamespace), debugstr_w(strUser), strPassword, debugstr_w(strLocale),
217 debugstr_w(strAuthority), iSecurityFlags, objWbemNamedValueSet, objWbemServices );
218 return E_NOTIMPL;
219 }
220
221 static HRESULT WINAPI locator_get_Security_(
222 ISWbemLocator *iface,
223 ISWbemSecurity **objWbemSecurity )
224 {
225 FIXME( "%p, %p\n", iface, objWbemSecurity );
226 return E_NOTIMPL;
227 }
228
229 static const ISWbemLocatorVtbl locator_vtbl =
230 {
231 locator_QueryInterface,
232 locator_AddRef,
233 locator_Release,
234 locator_GetTypeInfoCount,
235 locator_GetTypeInfo,
236 locator_GetIDsOfNames,
237 locator_Invoke,
238 locator_ConnectServer,
239 locator_get_Security_
240 };
241
242 HRESULT SWbemLocator_create( void **obj )
243 {
244 struct locator *locator;
245
246 TRACE( "%p\n", obj );
247
248 if (!(locator = heap_alloc( sizeof(*locator) ))) return E_OUTOFMEMORY;
249 locator->ISWbemLocator_iface.lpVtbl = &locator_vtbl;
250 locator->refs = 1;
251
252 *obj = &locator->ISWbemLocator_iface;
253 TRACE( "returning iface %p\n", *obj );
254 return S_OK;
255 }