main.c 8.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2013 Hans Leidekker for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
20 21 22

#define COBJMACROS

23 24
#include "windef.h"
#include "winbase.h"
25
#include "objbase.h"
26
#include "wmiutils.h"
27
#include "wbemdisp.h"
28 29
#include "rpcproxy.h"

30
#include "wine/debug.h"
31
#include "wine/heap.h"
32
#include "wbemdisp_private.h"
33
#include "wbemdisp_classes.h"
34 35 36

WINE_DEFAULT_DEBUG_CHANNEL(wbemdisp);

37
static HINSTANCE instance;
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
static HRESULT WINAPI WinMGMTS_QueryInterface(IParseDisplayName *iface, REFIID riid, void **ppv)
{
    if(IsEqualGUID(riid, &IID_IUnknown)) {
        TRACE("(IID_IUnknown %p)\n", ppv);
        *ppv = iface;
    }else if(IsEqualGUID(riid, &IID_IParseDisplayName)) {
        TRACE("(IID_IParseDisplayName %p)\n", ppv);
        *ppv = iface;
    }else {
        WARN("Unsupported riid %s\n", debugstr_guid(riid));
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI WinMGMTS_AddRef(IParseDisplayName *iface)
{
    return 2;
}

static ULONG WINAPI WinMGMTS_Release(IParseDisplayName *iface)
{
    return 1;
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static HRESULT parse_path( const WCHAR *str, BSTR *server, BSTR *namespace, BSTR *relative )
{
    IWbemPath *path;
    ULONG len;
    HRESULT hr;

    *server = *namespace = *relative = NULL;

    hr = CoCreateInstance( &CLSID_WbemDefPath, NULL, CLSCTX_INPROC_SERVER, &IID_IWbemPath, (void **)&path );
    if (hr != S_OK) return hr;

    hr = IWbemPath_SetText( path, WBEMPATH_CREATE_ACCEPT_ALL, str );
    if (hr != S_OK) goto done;

    len = 0;
    hr = IWbemPath_GetServer( path, &len, NULL );
    if (hr == S_OK)
    {
        if (!(*server = SysAllocStringLen( NULL, len )))
        {
            hr = E_OUTOFMEMORY;
            goto done;
        }
        hr = IWbemPath_GetServer( path, &len, *server );
        if (hr != S_OK) goto done;
    }

    len = 0;
    hr = IWbemPath_GetText( path, WBEMPATH_GET_NAMESPACE_ONLY, &len, NULL );
    if (hr == S_OK)
    {
        if (!(*namespace = SysAllocStringLen( NULL, len )))
        {
            hr = E_OUTOFMEMORY;
            goto done;
        }
        hr = IWbemPath_GetText( path, WBEMPATH_GET_NAMESPACE_ONLY, &len, *namespace );
        if (hr != S_OK) goto done;
    }
    len = 0;
    hr = IWbemPath_GetText( path, WBEMPATH_GET_RELATIVE_ONLY, &len, NULL );
    if (hr == S_OK)
    {
        if (!(*relative = SysAllocStringLen( NULL, len )))
        {
            hr = E_OUTOFMEMORY;
            goto done;
        }
        hr = IWbemPath_GetText( path, WBEMPATH_GET_RELATIVE_ONLY, &len, *relative );
    }

done:
    IWbemPath_Release( path );
    if (hr != S_OK)
    {
        SysFreeString( *server );
        SysFreeString( *namespace );
        SysFreeString( *relative );
    }
    return hr;
}

129 130 131
static HRESULT WINAPI WinMGMTS_ParseDisplayName(IParseDisplayName *iface, IBindCtx *pbc, LPOLESTR pszDisplayName,
        ULONG *pchEaten, IMoniker **ppmkOut)
{
132
    static const WCHAR prefixW[] = {'w','i','n','m','g','m','t','s',':',0};
133
    const DWORD prefix_len = ARRAY_SIZE(prefixW) - 1;
134 135 136 137 138 139 140 141 142
    ISWbemLocator *locator = NULL;
    ISWbemServices *services = NULL;
    ISWbemObject *obj = NULL;
    BSTR server, namespace, relative;
    WCHAR *p;
    HRESULT hr;

    TRACE( "%p, %p, %s, %p, %p\n", iface, pbc, debugstr_w(pszDisplayName), pchEaten, ppmkOut );

143
    if (wcsnicmp( pszDisplayName, prefixW, prefix_len )) return MK_E_SYNTAX;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

    p = pszDisplayName + prefix_len;
    if (*p == '{')
    {
        FIXME( "ignoring security settings\n" );
        while (*p && *p != '}') p++;
        if (*p == '}') p++;
        if (*p == '!') p++;
    }
    hr = parse_path( p, &server, &namespace, &relative );
    if (hr != S_OK) return hr;

    hr = SWbemLocator_create( (void **)&locator );
    if (hr != S_OK) goto done;

    hr = ISWbemLocator_ConnectServer( locator, server, namespace, NULL, NULL, NULL, NULL, 0, NULL, &services );
    if (hr != S_OK) goto done;

162
    if (!relative || !*relative) CreatePointerMoniker( (IUnknown *)services, ppmkOut );
163 164 165 166
    else
    {
        hr = ISWbemServices_Get( services, relative, 0, NULL, &obj );
        if (hr != S_OK) goto done;
167
        hr = CreatePointerMoniker( (IUnknown *)obj, ppmkOut );
168 169 170 171 172 173 174 175 176
    }

done:
    if (obj) ISWbemObject_Release( obj );
    if (services) ISWbemServices_Release( services );
    if (locator) ISWbemLocator_Release( locator );
    SysFreeString( server );
    SysFreeString( namespace );
    SysFreeString( relative );
177
    if (hr == S_OK) *pchEaten = lstrlenW( pszDisplayName );
178
    return hr;
179 180 181 182 183 184 185 186 187 188 189
}

static const IParseDisplayNameVtbl WinMGMTSVtbl = {
    WinMGMTS_QueryInterface,
    WinMGMTS_AddRef,
    WinMGMTS_Release,
    WinMGMTS_ParseDisplayName
};

static IParseDisplayName winmgmts = { &WinMGMTSVtbl };

190
static HRESULT WinMGMTS_create(void **ppv)
191 192 193 194 195
{
    *ppv = &winmgmts;
    return S_OK;
}

196 197 198
struct factory
{
    IClassFactory IClassFactory_iface;
199
    HRESULT (*fnCreateInstance)( LPVOID * );
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
};

static inline struct factory *impl_from_IClassFactory( IClassFactory *iface )
{
    return CONTAINING_RECORD( iface, struct factory, IClassFactory_iface );
}

static HRESULT WINAPI factory_QueryInterface( IClassFactory *iface, REFIID riid, LPVOID *obj )
{
    if (IsEqualGUID( riid, &IID_IUnknown ) || IsEqualGUID( riid, &IID_IClassFactory ))
    {
        IClassFactory_AddRef( iface );
        *obj = iface;
        return S_OK;
    }
    FIXME( "interface %s not implemented\n", debugstr_guid(riid) );
    return E_NOINTERFACE;
}

static ULONG WINAPI factory_AddRef( IClassFactory *iface )
{
    return 2;
}

static ULONG WINAPI factory_Release( IClassFactory *iface )
{
    return 1;
}

static HRESULT WINAPI factory_CreateInstance( IClassFactory *iface, LPUNKNOWN outer, REFIID riid,
                                              LPVOID *obj )
{
    struct factory *factory = impl_from_IClassFactory( iface );
    IUnknown *unk;
    HRESULT hr;

    TRACE( "%p, %s, %p\n", outer, debugstr_guid(riid), obj );

    *obj = NULL;
    if (outer) return CLASS_E_NOAGGREGATION;

241
    hr = factory->fnCreateInstance( (LPVOID *)&unk );
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    if (FAILED( hr ))
        return hr;

    hr = IUnknown_QueryInterface( unk, riid, obj );
    IUnknown_Release( unk );
    return hr;
}

static HRESULT WINAPI factory_LockServer( IClassFactory *iface, BOOL lock )
{
    FIXME( "%p, %d\n", iface, lock );
    return S_OK;
}

static const struct IClassFactoryVtbl factory_vtbl =
{
    factory_QueryInterface,
    factory_AddRef,
    factory_Release,
    factory_CreateInstance,
    factory_LockServer
};

static struct factory swbem_locator_cf = { { &factory_vtbl }, SWbemLocator_create };
266
static struct factory winmgmts_cf = { { &factory_vtbl }, WinMGMTS_create };
267

268 269 270 271 272 273 274 275
BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
{

    switch (reason)
    {
        case DLL_WINE_PREATTACH:
            return FALSE;    /* prefer native version */
        case DLL_PROCESS_ATTACH:
276
            instance = hinst;
277 278 279 280 281
            DisableThreadLibraryCalls( hinst );
            break;
    }
    return TRUE;
}
282

283 284 285 286 287 288 289
HRESULT WINAPI DllGetClassObject( REFCLSID rclsid, REFIID iid, LPVOID *obj )
{
    IClassFactory *cf = NULL;

    TRACE( "%s, %s, %p\n", debugstr_guid(rclsid), debugstr_guid(iid), obj );

    if (IsEqualGUID( rclsid, &CLSID_SWbemLocator ))
290 291 292 293 294 295
        cf = &swbem_locator_cf.IClassFactory_iface;
    else if (IsEqualGUID( rclsid, &CLSID_WinMGMTS ))
        cf = &winmgmts_cf.IClassFactory_iface;
    else
        return CLASS_E_CLASSNOTAVAILABLE;

296 297 298 299 300 301 302 303 304 305 306
    return IClassFactory_QueryInterface( cf, iid, obj );
}

/***********************************************************************
 *      DllCanUnloadNow (WBEMDISP.@)
 */
HRESULT WINAPI DllCanUnloadNow(void)
{
    return S_FALSE;
}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/***********************************************************************
 *      DllRegisterServer (WBEMDISP.@)
 */
HRESULT WINAPI DllRegisterServer(void)
{
    return __wine_register_resources( instance );
}

/***********************************************************************
 *      DllUnregisterServer (WBEMDISP.@)
 */
HRESULT WINAPI DllUnregisterServer(void)
{
    return __wine_unregister_resources( instance );
}