winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / wbemdisp / main.c
blob7e73d5465314b3ed0a2ed58cb83493df57d4bc98
1 /*
2 * Copyright 2013 Hans Leidekker for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "objbase.h"
26 #include "wmiutils.h"
27 #include "wbemdisp.h"
28 #include "rpcproxy.h"
30 #include "wine/debug.h"
31 #include "wine/heap.h"
32 #include "wbemdisp_private.h"
33 #include "wbemdisp_classes.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wbemdisp);
37 static HINSTANCE instance;
39 static HRESULT WINAPI WinMGMTS_QueryInterface(IParseDisplayName *iface, REFIID riid, void **ppv)
41 if(IsEqualGUID(riid, &IID_IUnknown)) {
42 TRACE("(IID_IUnknown %p)\n", ppv);
43 *ppv = iface;
44 }else if(IsEqualGUID(riid, &IID_IParseDisplayName)) {
45 TRACE("(IID_IParseDisplayName %p)\n", ppv);
46 *ppv = iface;
47 }else {
48 WARN("Unsupported riid %s\n", debugstr_guid(riid));
49 *ppv = NULL;
50 return E_NOINTERFACE;
53 IUnknown_AddRef((IUnknown*)*ppv);
54 return S_OK;
57 static ULONG WINAPI WinMGMTS_AddRef(IParseDisplayName *iface)
59 return 2;
62 static ULONG WINAPI WinMGMTS_Release(IParseDisplayName *iface)
64 return 1;
67 static HRESULT parse_path( const WCHAR *str, BSTR *server, BSTR *namespace, BSTR *relative )
69 IWbemPath *path;
70 ULONG len;
71 HRESULT hr;
73 *server = *namespace = *relative = NULL;
75 hr = CoCreateInstance( &CLSID_WbemDefPath, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemPath, (void **)&path );
76 if (hr != S_OK) return hr;
78 hr = IWbemPath_SetText( path, WBEMPATH_CREATE_ACCEPT_ALL, str );
79 if (hr != S_OK) goto done;
81 len = 0;
82 hr = IWbemPath_GetServer( path, &len, NULL );
83 if (hr == S_OK)
85 if (!(*server = SysAllocStringLen( NULL, len )))
87 hr = E_OUTOFMEMORY;
88 goto done;
90 hr = IWbemPath_GetServer( path, &len, *server );
91 if (hr != S_OK) goto done;
94 len = 0;
95 hr = IWbemPath_GetText( path, WBEMPATH_GET_NAMESPACE_ONLY, &len, NULL );
96 if (hr == S_OK)
98 if (!(*namespace = SysAllocStringLen( NULL, len )))
100 hr = E_OUTOFMEMORY;
101 goto done;
103 hr = IWbemPath_GetText( path, WBEMPATH_GET_NAMESPACE_ONLY, &len, *namespace );
104 if (hr != S_OK) goto done;
106 len = 0;
107 hr = IWbemPath_GetText( path, WBEMPATH_GET_RELATIVE_ONLY, &len, NULL );
108 if (hr == S_OK)
110 if (!(*relative = SysAllocStringLen( NULL, len )))
112 hr = E_OUTOFMEMORY;
113 goto done;
115 hr = IWbemPath_GetText( path, WBEMPATH_GET_RELATIVE_ONLY, &len, *relative );
118 done:
119 IWbemPath_Release( path );
120 if (hr != S_OK)
122 SysFreeString( *server );
123 SysFreeString( *namespace );
124 SysFreeString( *relative );
126 return hr;
129 static HRESULT WINAPI WinMGMTS_ParseDisplayName(IParseDisplayName *iface, IBindCtx *pbc, LPOLESTR pszDisplayName,
130 ULONG *pchEaten, IMoniker **ppmkOut)
132 const DWORD prefix_len = ARRAY_SIZE(L"winmgmts:") - 1;
133 ISWbemLocator *locator = NULL;
134 ISWbemServices *services = NULL;
135 ISWbemObject *obj = NULL;
136 BSTR server, namespace, relative;
137 WCHAR *p;
138 HRESULT hr;
140 TRACE( "%p, %p, %s, %p, %p\n", iface, pbc, debugstr_w(pszDisplayName), pchEaten, ppmkOut );
142 if (wcsnicmp( pszDisplayName, L"winmgmts:", prefix_len )) return MK_E_SYNTAX;
144 p = pszDisplayName + prefix_len;
145 if (*p == '{')
147 FIXME( "ignoring security settings\n" );
148 while (*p && *p != '}') p++;
149 if (*p == '}') p++;
150 if (*p == '!') p++;
152 hr = parse_path( p, &server, &namespace, &relative );
153 if (hr != S_OK) return hr;
155 hr = SWbemLocator_create( (void **)&locator );
156 if (hr != S_OK) goto done;
158 hr = ISWbemLocator_ConnectServer( locator, server, namespace, NULL, NULL, NULL, NULL, 0, NULL, &services );
159 if (hr != S_OK) goto done;
161 if (!relative || !*relative) CreatePointerMoniker( (IUnknown *)services, ppmkOut );
162 else
164 hr = ISWbemServices_Get( services, relative, 0, NULL, &obj );
165 if (hr != S_OK) goto done;
166 hr = CreatePointerMoniker( (IUnknown *)obj, ppmkOut );
169 done:
170 if (obj) ISWbemObject_Release( obj );
171 if (services) ISWbemServices_Release( services );
172 if (locator) ISWbemLocator_Release( locator );
173 SysFreeString( server );
174 SysFreeString( namespace );
175 SysFreeString( relative );
176 if (hr == S_OK) *pchEaten = lstrlenW( pszDisplayName );
177 return hr;
180 static const IParseDisplayNameVtbl WinMGMTSVtbl = {
181 WinMGMTS_QueryInterface,
182 WinMGMTS_AddRef,
183 WinMGMTS_Release,
184 WinMGMTS_ParseDisplayName
187 static IParseDisplayName winmgmts = { &WinMGMTSVtbl };
189 static HRESULT WinMGMTS_create(void **ppv)
191 *ppv = &winmgmts;
192 return S_OK;
195 struct factory
197 IClassFactory IClassFactory_iface;
198 HRESULT (*fnCreateInstance)( LPVOID * );
201 static inline struct factory *impl_from_IClassFactory( IClassFactory *iface )
203 return CONTAINING_RECORD( iface, struct factory, IClassFactory_iface );
206 static HRESULT WINAPI factory_QueryInterface( IClassFactory *iface, REFIID riid, LPVOID *obj )
208 if (IsEqualGUID( riid, &IID_IUnknown ) || IsEqualGUID( riid, &IID_IClassFactory ))
210 IClassFactory_AddRef( iface );
211 *obj = iface;
212 return S_OK;
214 WARN( "interface %s not implemented\n", debugstr_guid(riid) );
215 return E_NOINTERFACE;
218 static ULONG WINAPI factory_AddRef( IClassFactory *iface )
220 return 2;
223 static ULONG WINAPI factory_Release( IClassFactory *iface )
225 return 1;
228 static HRESULT WINAPI factory_CreateInstance( IClassFactory *iface, LPUNKNOWN outer, REFIID riid,
229 LPVOID *obj )
231 struct factory *factory = impl_from_IClassFactory( iface );
232 IUnknown *unk;
233 HRESULT hr;
235 TRACE( "%p, %s, %p\n", outer, debugstr_guid(riid), obj );
237 *obj = NULL;
238 if (outer) return CLASS_E_NOAGGREGATION;
240 hr = factory->fnCreateInstance( (LPVOID *)&unk );
241 if (FAILED( hr ))
242 return hr;
244 hr = IUnknown_QueryInterface( unk, riid, obj );
245 IUnknown_Release( unk );
246 return hr;
249 static HRESULT WINAPI factory_LockServer( IClassFactory *iface, BOOL lock )
251 FIXME( "%p, %d\n", iface, lock );
252 return S_OK;
255 static const struct IClassFactoryVtbl factory_vtbl =
257 factory_QueryInterface,
258 factory_AddRef,
259 factory_Release,
260 factory_CreateInstance,
261 factory_LockServer
264 static struct factory swbem_locator_cf = { { &factory_vtbl }, SWbemLocator_create };
265 static struct factory swbem_namedvalueset_cf = { { &factory_vtbl }, SWbemNamedValueSet_create };
266 static struct factory winmgmts_cf = { { &factory_vtbl }, WinMGMTS_create };
268 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
271 switch (reason)
273 case DLL_PROCESS_ATTACH:
274 instance = hinst;
275 DisableThreadLibraryCalls( hinst );
276 break;
278 return TRUE;
281 HRESULT WINAPI DllGetClassObject( REFCLSID rclsid, REFIID iid, LPVOID *obj )
283 IClassFactory *cf = NULL;
285 TRACE( "%s, %s, %p\n", debugstr_guid(rclsid), debugstr_guid(iid), obj );
287 if (IsEqualGUID( rclsid, &CLSID_SWbemLocator ))
288 cf = &swbem_locator_cf.IClassFactory_iface;
289 else if (IsEqualGUID( rclsid, &CLSID_WinMGMTS ))
290 cf = &winmgmts_cf.IClassFactory_iface;
291 else if (IsEqualGUID( rclsid, &CLSID_SWbemNamedValueSet ))
292 cf = &swbem_namedvalueset_cf.IClassFactory_iface;
293 else
294 return CLASS_E_CLASSNOTAVAILABLE;
296 return IClassFactory_QueryInterface( cf, iid, obj );
299 /***********************************************************************
300 * DllCanUnloadNow (WBEMDISP.@)
302 HRESULT WINAPI DllCanUnloadNow(void)
304 return S_FALSE;
307 /***********************************************************************
308 * DllRegisterServer (WBEMDISP.@)
310 HRESULT WINAPI DllRegisterServer(void)
312 return __wine_register_resources( instance );
315 /***********************************************************************
316 * DllUnregisterServer (WBEMDISP.@)
318 HRESULT WINAPI DllUnregisterServer(void)
320 return __wine_unregister_resources( instance );