[DEVENUM]
[reactos.git] / reactos / dll / directx / wine / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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
25 #include "devenum_private.h"
26
27 static HRESULT WINAPI DEVENUM_IParseDisplayName_QueryInterface(IParseDisplayName *iface,
28 REFIID riid, void **ppv)
29 {
30 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
31
32 if (!ppv)
33 return E_POINTER;
34
35 if (IsEqualGUID(riid, &IID_IUnknown) ||
36 IsEqualGUID(riid, &IID_IParseDisplayName))
37 {
38 *ppv = iface;
39 IParseDisplayName_AddRef(iface);
40 return S_OK;
41 }
42
43 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
44 *ppv = NULL;
45 return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI DEVENUM_IParseDisplayName_AddRef(IParseDisplayName *iface)
49 {
50 TRACE("\n");
51
52 DEVENUM_LockModule();
53
54 return 2; /* non-heap based object */
55 }
56
57 static ULONG WINAPI DEVENUM_IParseDisplayName_Release(IParseDisplayName *iface)
58 {
59 TRACE("\n");
60
61 DEVENUM_UnlockModule();
62
63 return 1; /* non-heap based object */
64 }
65
66 /**********************************************************************
67 * DEVENUM_IParseDisplayName_ParseDisplayName
68 *
69 * Creates a moniker referenced to by the display string argument
70 *
71 * POSSIBLE BUGS:
72 * Might not handle more complicated strings properly (ie anything
73 * not in "@device:sw:{CLSID1}\<filter name or CLSID>" format
74 */
75 static HRESULT WINAPI DEVENUM_IParseDisplayName_ParseDisplayName(IParseDisplayName *iface,
76 IBindCtx *pbc, LPOLESTR pszDisplayName, ULONG *pchEaten, IMoniker **ppmkOut)
77 {
78 LPOLESTR pszBetween = NULL;
79 LPOLESTR pszClass = NULL;
80 MediaCatMoniker * pMoniker = NULL;
81 CLSID clsidDevice;
82 HRESULT res = S_OK;
83 WCHAR wszRegKeyName[MAX_PATH];
84 HKEY hbasekey;
85 int classlen;
86 static const WCHAR wszRegSeparator[] = {'\\', 0 };
87
88 TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
89
90 *ppmkOut = NULL;
91 if (pchEaten)
92 *pchEaten = strlenW(pszDisplayName);
93
94 pszDisplayName = strchrW(pszDisplayName, '{');
95 pszBetween = strchrW(pszDisplayName, '}') + 2;
96
97 /* size = pszBetween - pszDisplayName - 1 (for '\\' after CLSID)
98 * + 1 (for NULL character)
99 */
100 classlen = (int)(pszBetween - pszDisplayName - 1);
101 pszClass = CoTaskMemAlloc((classlen + 1) * sizeof(WCHAR));
102 if (!pszClass)
103 return E_OUTOFMEMORY;
104
105 memcpy(pszClass, pszDisplayName, classlen * sizeof(WCHAR));
106 pszClass[classlen] = 0;
107
108 TRACE("Device CLSID: %s\n", debugstr_w(pszClass));
109
110 res = CLSIDFromString(pszClass, &clsidDevice);
111
112 if (SUCCEEDED(res))
113 {
114 res = DEVENUM_GetCategoryKey(&clsidDevice, &hbasekey, wszRegKeyName, MAX_PATH);
115 }
116
117 if (SUCCEEDED(res))
118 {
119 pMoniker = DEVENUM_IMediaCatMoniker_Construct();
120 if (pMoniker)
121 {
122 strcatW(wszRegKeyName, wszRegSeparator);
123 strcatW(wszRegKeyName, pszBetween);
124
125 if (RegCreateKeyW(hbasekey, wszRegKeyName, &pMoniker->hkey) == ERROR_SUCCESS)
126 *ppmkOut = &pMoniker->IMoniker_iface;
127 else
128 {
129 IMoniker_Release(&pMoniker->IMoniker_iface);
130 res = MK_E_NOOBJECT;
131 }
132 }
133 }
134
135 CoTaskMemFree(pszClass);
136
137 TRACE("-- returning: %x\n", res);
138 return res;
139 }
140
141 /**********************************************************************
142 * IParseDisplayName_Vtbl
143 */
144 static const IParseDisplayNameVtbl IParseDisplayName_Vtbl =
145 {
146 DEVENUM_IParseDisplayName_QueryInterface,
147 DEVENUM_IParseDisplayName_AddRef,
148 DEVENUM_IParseDisplayName_Release,
149 DEVENUM_IParseDisplayName_ParseDisplayName
150 };
151
152 /* The one instance of this class */
153 IParseDisplayName DEVENUM_ParseDisplayName = { &IParseDisplayName_Vtbl };