parsedisplayname.c 4.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *	IParseDisplayName implementation for DEVENUM.dll
 *
 * Copyright (C) 2002 Robert Shearman
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26 27 28 29
 *
 * NOTES ON THIS FILE:
 * - Implements IParseDisplayName interface which creates a moniker
 *   from a string in a special format
 */
#include "devenum_private.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(devenum);

30 31
static HRESULT WINAPI DEVENUM_IParseDisplayName_QueryInterface(IParseDisplayName *iface,
        REFIID riid, void **ppv)
32 33 34
{
    TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));

35 36
    if (!ppv)
        return E_POINTER;
37 38 39 40

    if (IsEqualGUID(riid, &IID_IUnknown) ||
        IsEqualGUID(riid, &IID_IParseDisplayName))
    {
41 42 43
        *ppv = iface;
        IParseDisplayName_AddRef(iface);
        return S_OK;
44 45
    }

46
    FIXME("- no interface IID: %s\n", debugstr_guid(riid));
47
    *ppv = NULL;
48 49 50
    return E_NOINTERFACE;
}

51
static ULONG WINAPI DEVENUM_IParseDisplayName_AddRef(IParseDisplayName *iface)
52 53 54
{
    TRACE("\n");

55
    DEVENUM_LockModule();
56

57
    return 2; /* non-heap based object */
58 59
}

60
static ULONG WINAPI DEVENUM_IParseDisplayName_Release(IParseDisplayName *iface)
61 62 63
{
    TRACE("\n");

64
    DEVENUM_UnlockModule();
65

66
    return 1; /* non-heap based object */
67 68 69 70 71 72 73 74
}

/**********************************************************************
 * DEVENUM_IParseDisplayName_ParseDisplayName
 *
 *  Creates a moniker referenced to by the display string argument
 *
 * POSSIBLE BUGS:
75 76
 *  Might not handle more complicated strings properly (ie anything
 *  not in "@device:sw:{CLSID1}\<filter name or CLSID>" format
77
 */
78 79
static HRESULT WINAPI DEVENUM_IParseDisplayName_ParseDisplayName(IParseDisplayName *iface,
        IBindCtx *pbc, LPOLESTR pszDisplayName, ULONG *pchEaten, IMoniker **ppmkOut)
80 81 82 83 84 85
{
    LPOLESTR pszBetween = NULL;
    LPOLESTR pszClass = NULL;
    MediaCatMoniker * pMoniker = NULL;
    CLSID clsidDevice;
    HRESULT res = S_OK;
86 87
    WCHAR wszRegKeyName[MAX_PATH];
    HKEY hbasekey;
88
    int classlen;
89
    static const WCHAR wszRegSeparator[] =   {'\\', 0 };
90

91 92 93 94 95 96 97 98 99 100 101 102
    TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(pszDisplayName), pchEaten, ppmkOut);

    *ppmkOut = NULL;
    if (pchEaten)
        *pchEaten = strlenW(pszDisplayName);

    pszDisplayName = strchrW(pszDisplayName, '{');
    pszBetween = strchrW(pszDisplayName, '}') + 2;

    /* size = pszBetween - pszDisplayName - 1 (for '\\' after CLSID)
     * + 1 (for NULL character)
     */
103 104
    classlen = (int)(pszBetween - pszDisplayName - 1);
    pszClass = CoTaskMemAlloc((classlen + 1) * sizeof(WCHAR));
105 106 107
    if (!pszClass)
        return E_OUTOFMEMORY;

108 109
    memcpy(pszClass, pszDisplayName, classlen * sizeof(WCHAR));
    pszClass[classlen] = 0;
110 111 112 113 114 115 116

    TRACE("Device CLSID: %s\n", debugstr_w(pszClass));

    res = CLSIDFromString(pszClass, &clsidDevice);

    if (SUCCEEDED(res))
    {
117
        res = DEVENUM_GetCategoryKey(&clsidDevice, &hbasekey, wszRegKeyName, MAX_PATH);
118 119 120 121 122 123 124
    }

    if (SUCCEEDED(res))
    {
        pMoniker = DEVENUM_IMediaCatMoniker_Construct();
        if (pMoniker)
        {
125 126 127 128
            strcatW(wszRegKeyName, wszRegSeparator);
            strcatW(wszRegKeyName, pszBetween);

            if (RegCreateKeyW(hbasekey, wszRegKeyName, &pMoniker->hkey) == ERROR_SUCCESS)
129
                *ppmkOut = &pMoniker->IMoniker_iface;
130 131
            else
            {
132
                IMoniker_Release(&pMoniker->IMoniker_iface);
133 134 135 136 137
                res = MK_E_NOOBJECT;
            }
        }
    }

138
    CoTaskMemFree(pszClass);
139

140
    TRACE("-- returning: %x\n", res);
141 142 143 144 145 146
    return res;
}

/**********************************************************************
 * IParseDisplayName_Vtbl
 */
147
static const IParseDisplayNameVtbl IParseDisplayName_Vtbl =
148 149 150 151 152 153 154 155
{
    DEVENUM_IParseDisplayName_QueryInterface,
    DEVENUM_IParseDisplayName_AddRef,
    DEVENUM_IParseDisplayName_Release,
    DEVENUM_IParseDisplayName_ParseDisplayName
};

/* The one instance of this class */
156
IParseDisplayName DEVENUM_ParseDisplayName = { &IParseDisplayName_Vtbl };