Warn the user if he is running the test in interactive mode but the
[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     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
36
37     if (ppvObj == NULL) return E_POINTER;
38
39     if (IsEqualGUID(riid, &IID_IUnknown) ||
40         IsEqualGUID(riid, &IID_IParseDisplayName))
41     {
42         *ppvObj = (LPVOID)iface;
43         IParseDisplayName_AddRef(iface);
44         return S_OK;
45     }
46
47     FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
48     return E_NOINTERFACE;
49 }
50
51 /**********************************************************************
52  * DEVENUM_IParseDisplayName_AddRef (also IUnknown)
53  */
54 static ULONG WINAPI DEVENUM_IParseDisplayName_AddRef(LPPARSEDISPLAYNAME iface)
55 {
56     TRACE("\n");
57
58     DEVENUM_LockModule();
59
60     return 2; /* non-heap based object */
61 }
62
63 /**********************************************************************
64  * DEVENUM_IParseDisplayName_Release (also IUnknown)
65  */
66 static ULONG WINAPI DEVENUM_IParseDisplayName_Release(LPPARSEDISPLAYNAME iface)
67 {
68     TRACE("\n");
69
70     DEVENUM_UnlockModule();
71
72     return 1; /* non-heap based object */
73 }
74
75 /**********************************************************************
76  * DEVENUM_IParseDisplayName_ParseDisplayName
77  *
78  *  Creates a moniker referenced to by the display string argument
79  *
80  * POSSIBLE BUGS:
81  *  Might not handle more complicated strings properly (ie anything
82  *  not in "@device:sw:{CLSID1}\<filter name or CLSID>" format
83  */
84 static HRESULT WINAPI DEVENUM_IParseDisplayName_ParseDisplayName(
85     LPPARSEDISPLAYNAME iface,
86     IBindCtx *pbc,
87     LPOLESTR pszDisplayName,
88     ULONG *pchEaten,
89     IMoniker **ppmkOut)
90 {
91     LPOLESTR pszBetween = NULL;
92     LPOLESTR pszClass = NULL;
93     IEnumMoniker * pEm = NULL;
94     MediaCatMoniker * pMoniker = NULL;
95     CLSID clsidDevice;
96     HRESULT res = S_OK;
97
98     TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
99
100     *ppmkOut = NULL;
101     if (pchEaten)
102         *pchEaten = strlenW(pszDisplayName);
103
104     pszDisplayName = strchrW(pszDisplayName, '{');
105     pszBetween = strchrW(pszDisplayName, '}') + 2;
106
107     /* size = pszBetween - pszDisplayName - 1 (for '\\' after CLSID)
108      * + 1 (for NULL character)
109      */
110     pszClass = CoTaskMemAlloc((int)(pszBetween - pszDisplayName) * sizeof(WCHAR));
111     if (!pszClass)
112         return E_OUTOFMEMORY;
113
114     strncpyW(pszClass, pszDisplayName, (int)(pszBetween - pszDisplayName) - 1);
115     pszClass[(int)(pszBetween - pszDisplayName) - 1] = 0;
116
117     TRACE("Device CLSID: %s\n", debugstr_w(pszClass));
118
119     res = CLSIDFromString(pszClass, &clsidDevice);
120
121     if (SUCCEEDED(res))
122     {
123         res = DEVENUM_ICreateDevEnum_CreateClassEnumerator((ICreateDevEnum *)(char*)&DEVENUM_CreateDevEnum, &clsidDevice, &pEm, 0);
124         if (res == S_FALSE) /* S_FALSE means no category */
125             res = MK_E_NOOBJECT;
126     }
127
128     if (SUCCEEDED(res))
129     {
130         pMoniker = DEVENUM_IMediaCatMoniker_Construct();
131         if (pMoniker)
132         {
133             if (RegCreateKeyW(((EnumMonikerImpl *)pEm)->hkey,
134                                pszBetween,
135                                &pMoniker->hkey) == ERROR_SUCCESS)
136                 *ppmkOut = (LPMONIKER)pMoniker;
137             else
138             {
139                 IMoniker_Release((LPMONIKER)pMoniker);
140                 res = MK_E_NOOBJECT;
141             }
142         }
143     }
144
145     if (pEm)
146         IEnumMoniker_Release(pEm);
147
148     if (pszClass)
149         CoTaskMemFree(pszClass);
150
151     TRACE("-- returning: %lx\n", res);
152     return res;
153 }
154
155 /**********************************************************************
156  * IParseDisplayName_Vtbl
157  */
158 static IParseDisplayNameVtbl IParseDisplayName_Vtbl =
159 {
160     DEVENUM_IParseDisplayName_QueryInterface,
161     DEVENUM_IParseDisplayName_AddRef,
162     DEVENUM_IParseDisplayName_Release,
163     DEVENUM_IParseDisplayName_ParseDisplayName
164 };
165
166 /* The one instance of this class */
167 ParseDisplayNameImpl DEVENUM_ParseDisplayName = { &IParseDisplayName_Vtbl };