Only link against libdxguid where necessary.
[wine] / dlls / devenum / parsedisplayname.c
1 /*
2  *      IParseDisplayName implementation for DEVENUM.dll
3  *
4  * Copyright (C) 2002 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  * NOTES ON THIS FILE:
21  * - Implements IParseDisplayName interface which creates a moniker
22  *   from a string in a special format
23  */
24 #include "devenum_private.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
29
30 static HRESULT WINAPI DEVENUM_IParseDisplayName_QueryInterface(
31     LPPARSEDISPLAYNAME iface,
32     REFIID riid,
33     LPVOID *ppvObj)
34 {
35     ICOM_THIS(ParseDisplayNameImpl, iface);
36     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
37
38     if (This == NULL || ppvObj == NULL) return E_POINTER;
39
40     if (IsEqualGUID(riid, &IID_IUnknown) ||
41         IsEqualGUID(riid, &IID_IParseDisplayName))
42     {
43         *ppvObj = (LPVOID)iface;
44         IParseDisplayName_AddRef(iface);
45         return S_OK;
46     }
47
48     FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
49     return E_NOINTERFACE;
50 }
51
52 /**********************************************************************
53  * DEVENUM_IParseDisplayName_AddRef (also IUnknown)
54  */
55 static ULONG WINAPI DEVENUM_IParseDisplayName_AddRef(LPPARSEDISPLAYNAME iface)
56 {
57     ICOM_THIS(ParseDisplayNameImpl, iface);
58     TRACE("\n");
59
60     if (This == NULL) return E_POINTER;
61
62     if (InterlockedIncrement(&This->ref) == 1) {
63         InterlockedIncrement(&dll_ref);
64     }
65
66     return ++This->ref;
67 }
68
69 /**********************************************************************
70  * DEVENUM_IParseDisplayName_Release (also IUnknown)
71  */
72 static ULONG WINAPI DEVENUM_IParseDisplayName_Release(LPPARSEDISPLAYNAME iface)
73 {
74     ICOM_THIS(ParseDisplayNameImpl, iface);
75     ULONG ref;
76     TRACE("\n");
77
78     if (This == NULL) return E_POINTER;
79
80     ref = --This->ref;
81     if (InterlockedDecrement(&This->ref) == 0) {
82         InterlockedDecrement(&dll_ref);
83     }
84     return ref;
85 }
86
87 /**********************************************************************
88  * DEVENUM_IParseDisplayName_ParseDisplayName
89  *
90  *  Creates a moniker referenced to by the display string argument
91  *
92  * POSSIBLE BUGS:
93  *  Might not handle more complicated strings properly (ie anything
94  *  not in "@device:sw:{CLSID1}\<filter name or CLSID>" format
95  */
96 static HRESULT WINAPI DEVENUM_IParseDisplayName_ParseDisplayName(
97     LPPARSEDISPLAYNAME iface,
98     IBindCtx *pbc,
99     LPOLESTR pszDisplayName,
100     ULONG *pchEaten,
101     IMoniker **ppmkOut)
102 {
103     LPOLESTR pszBetween = NULL;
104     LPOLESTR pszClass = NULL;
105     IEnumMoniker * pEm = NULL;
106     MediaCatMoniker * pMoniker = NULL;
107     CLSID clsidDevice;
108     HRESULT res = S_OK;
109
110     TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
111
112     *ppmkOut = NULL;
113     if (pchEaten)
114         *pchEaten = strlenW(pszDisplayName);
115
116     pszDisplayName = strchrW(pszDisplayName, '{');
117     pszBetween = strchrW(pszDisplayName, '}') + 2;
118
119     /* size = pszBetween - pszDisplayName - 1 (for '\\' after CLSID)
120      * + 1 (for NULL character)
121      */
122     pszClass = CoTaskMemAlloc((int)(pszBetween - pszDisplayName) * sizeof(WCHAR));
123     if (!pszClass)
124         return E_OUTOFMEMORY;
125
126     strncpyW(pszClass, pszDisplayName, (int)(pszBetween - pszDisplayName) - 1);
127     pszClass[(int)(pszBetween - pszDisplayName) - 1] = 0;
128
129     TRACE("Device CLSID: %s\n", debugstr_w(pszClass));
130
131     res = CLSIDFromString(pszClass, &clsidDevice);
132
133     if (SUCCEEDED(res))
134     {
135         res = DEVENUM_ICreateDevEnum_CreateClassEnumerator((ICreateDevEnum *)(char*)&DEVENUM_CreateDevEnum, &clsidDevice, &pEm, 0);
136         if (res == S_FALSE) /* S_FALSE means no category */
137             res = MK_E_NOOBJECT;
138     }
139
140     if (SUCCEEDED(res))
141     {
142         pMoniker = DEVENUM_IMediaCatMoniker_Construct();
143         if (pMoniker)
144         {
145             if (RegCreateKeyW(((EnumMonikerImpl *)pEm)->hkey,
146                                pszBetween,
147                                &pMoniker->hkey) == ERROR_SUCCESS)
148                 *ppmkOut = (LPMONIKER)pMoniker;
149             else
150             {
151                 IMoniker_Release((LPMONIKER)pMoniker);
152                 res = MK_E_NOOBJECT;
153             }
154         }
155     }
156
157     if (pEm)
158         IEnumMoniker_Release(pEm);
159
160     if (pszClass)
161         CoTaskMemFree(pszClass);
162
163     TRACE("-- returning: %lx\n", res);
164     return res;
165 }
166
167 /**********************************************************************
168  * IParseDisplayName_Vtbl
169  */
170 static ICOM_VTABLE(IParseDisplayName) IParseDisplayName_Vtbl =
171 {
172     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
173     DEVENUM_IParseDisplayName_QueryInterface,
174     DEVENUM_IParseDisplayName_AddRef,
175     DEVENUM_IParseDisplayName_Release,
176     DEVENUM_IParseDisplayName_ParseDisplayName
177 };
178
179 /* The one instance of this class */
180 ParseDisplayNameImpl DEVENUM_ParseDisplayName = { &IParseDisplayName_Vtbl, 0 };