dxdiagn: Add operating system string properties to the DxDiag_SystemInfo container.
[wine] / dlls / dxdiagn / container.c
1 /* 
2  * IDxDiagContainer Implementation
3  * 
4  * Copyright 2004 Raphael Junqueira
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  */
21
22 #include "config.h"
23
24 #define COBJMACROS
25 #include "dxdiag_private.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);
30
31 /* IDxDiagContainer IUnknown parts follow: */
32 static HRESULT WINAPI IDxDiagContainerImpl_QueryInterface(PDXDIAGCONTAINER iface, REFIID riid, LPVOID *ppobj)
33 {
34     IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
35
36     if (!ppobj) return E_INVALIDARG;
37
38     if (IsEqualGUID(riid, &IID_IUnknown)
39         || IsEqualGUID(riid, &IID_IDxDiagContainer)) {
40         IUnknown_AddRef(iface);
41         *ppobj = This;
42         return S_OK;
43     }
44
45     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
46     *ppobj = NULL;
47     return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI IDxDiagContainerImpl_AddRef(PDXDIAGCONTAINER iface) {
51     IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
52     ULONG refCount = InterlockedIncrement(&This->ref);
53
54     TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
55
56     DXDIAGN_LockModule();
57
58     return refCount;
59 }
60
61 static ULONG WINAPI IDxDiagContainerImpl_Release(PDXDIAGCONTAINER iface) {
62     IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
63     ULONG refCount = InterlockedDecrement(&This->ref);
64
65     TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
66
67     if (!refCount) {
68         IDxDiagProvider_Release(This->pProv);
69         HeapFree(GetProcessHeap(), 0, This);
70     }
71
72     DXDIAGN_UnlockModule();
73     
74     return refCount;
75 }
76
77 /* IDxDiagContainer Interface follow: */
78 static HRESULT WINAPI IDxDiagContainerImpl_GetNumberOfChildContainers(PDXDIAGCONTAINER iface, DWORD* pdwCount) {
79   IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
80   TRACE("(%p)\n", iface);
81   if (NULL == pdwCount) {
82     return E_INVALIDARG;
83   }
84   *pdwCount = This->cont->nSubContainers;
85   return S_OK;
86 }
87
88 static HRESULT WINAPI IDxDiagContainerImpl_EnumChildContainerNames(PDXDIAGCONTAINER iface, DWORD dwIndex, LPWSTR pwszContainer, DWORD cchContainer) {
89   IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
90   IDxDiagContainerImpl_Container *p;
91   DWORD i = 0;
92
93   TRACE("(%p, %u, %p, %u)\n", iface, dwIndex, pwszContainer, cchContainer);
94
95   if (NULL == pwszContainer || 0 == cchContainer) {
96     return E_INVALIDARG;
97   }
98
99   LIST_FOR_EACH_ENTRY(p, &This->cont->subContainers, IDxDiagContainerImpl_Container, entry)
100   {
101     if (dwIndex == i) {
102       TRACE("Found container name %s, copying string\n", debugstr_w(p->contName));
103       lstrcpynW(pwszContainer, p->contName, cchContainer);
104       return (cchContainer <= strlenW(p->contName)) ?
105               DXDIAG_E_INSUFFICIENT_BUFFER : S_OK;
106     }
107     ++i;
108   }
109
110   TRACE("Failed to find container name at specified index\n");
111   *pwszContainer = '\0';
112   return E_INVALIDARG;
113 }
114
115 static HRESULT IDxDiagContainerImpl_GetChildContainerInternal(IDxDiagContainerImpl_Container *cont, LPCWSTR pwszContainer, IDxDiagContainerImpl_Container **subcont) {
116   IDxDiagContainerImpl_Container *p;
117
118   LIST_FOR_EACH_ENTRY(p, &cont->subContainers, IDxDiagContainerImpl_Container, entry)
119   {
120     if (0 == lstrcmpW(p->contName, pwszContainer)) {
121       *subcont = p;
122       return S_OK;
123     }
124   }
125
126   return E_INVALIDARG;
127 }
128
129 static HRESULT WINAPI IDxDiagContainerImpl_GetChildContainer(PDXDIAGCONTAINER iface, LPCWSTR pwszContainer, IDxDiagContainer **ppInstance) {
130   IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
131   IDxDiagContainerImpl_Container *pContainer = This->cont;
132   LPWSTR tmp, orig_tmp;
133   INT tmp_len;
134   WCHAR* cur;
135   HRESULT hr = E_INVALIDARG;
136
137   TRACE("(%p, %s, %p)\n", iface, debugstr_w(pwszContainer), ppInstance);
138
139   if (NULL == ppInstance || NULL == pwszContainer) {
140     return E_INVALIDARG;
141   }
142
143   *ppInstance = NULL;
144
145   tmp_len = strlenW(pwszContainer) + 1;
146   orig_tmp = tmp = HeapAlloc(GetProcessHeap(), 0, tmp_len * sizeof(WCHAR));
147   if (NULL == tmp) return E_FAIL;
148   lstrcpynW(tmp, pwszContainer, tmp_len);
149
150   cur = strchrW(tmp, '.');
151   while (NULL != cur) {
152     *cur = '\0'; /* cut tmp string to '.' */
153     if (!*(cur + 1)) break; /* Account for a lone terminating period, as in "cont1.cont2.". */
154     TRACE("Trying to get parent container %s\n", debugstr_w(tmp));
155     hr = IDxDiagContainerImpl_GetChildContainerInternal(pContainer, tmp, &pContainer);
156     if (FAILED(hr))
157       goto on_error;
158     cur++; /* go after '.' (just replaced by \0) */
159     tmp = cur;
160     cur = strchrW(tmp, '.');
161   }
162
163   TRACE("Trying to get container %s\n", debugstr_w(tmp));
164   hr = IDxDiagContainerImpl_GetChildContainerInternal(pContainer, tmp, &pContainer);
165   if (SUCCEEDED(hr)) {
166     hr = DXDiag_CreateDXDiagContainer(&IID_IDxDiagContainer, pContainer, This->pProv, (void **)ppInstance);
167     if (SUCCEEDED(hr))
168         TRACE("Succeeded in getting the container instance\n");
169   }
170
171 on_error:
172   HeapFree(GetProcessHeap(), 0, orig_tmp);
173   return hr;
174 }
175
176 static HRESULT WINAPI IDxDiagContainerImpl_GetNumberOfProps(PDXDIAGCONTAINER iface, DWORD* pdwCount) {
177   IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
178   TRACE("(%p)\n", iface);
179   if (NULL == pdwCount) {
180     return E_INVALIDARG;
181   }
182   *pdwCount = This->cont->nProperties;
183   return S_OK;
184 }
185
186 static HRESULT WINAPI IDxDiagContainerImpl_EnumPropNames(PDXDIAGCONTAINER iface, DWORD dwIndex, LPWSTR pwszPropName, DWORD cchPropName) {
187   IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
188   IDxDiagContainerImpl_Property *p;
189   DWORD i = 0;
190
191   TRACE("(%p, %u, %p, %u)\n", iface, dwIndex, pwszPropName, cchPropName);
192
193   if (NULL == pwszPropName || 0 == cchPropName) {
194     return E_INVALIDARG;
195   }
196
197   LIST_FOR_EACH_ENTRY(p, &This->cont->properties, IDxDiagContainerImpl_Property, entry)
198   {
199     if (dwIndex == i) {
200       TRACE("Found property name %s, copying string\n", debugstr_w(p->propName));
201       lstrcpynW(pwszPropName, p->propName, cchPropName);
202       return (cchPropName <= strlenW(p->propName)) ?
203               DXDIAG_E_INSUFFICIENT_BUFFER : S_OK;
204     }
205     ++i;
206   }
207
208   TRACE("Failed to find property name at specified index\n");
209   return E_INVALIDARG;
210 }
211
212 static HRESULT WINAPI IDxDiagContainerImpl_GetProp(PDXDIAGCONTAINER iface, LPCWSTR pwszPropName, VARIANT* pvarProp) {
213   IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
214   IDxDiagContainerImpl_Property *p;
215
216   TRACE("(%p, %s, %p)\n", iface, debugstr_w(pwszPropName), pvarProp);
217
218   if (NULL == pvarProp || NULL == pwszPropName) {
219     return E_INVALIDARG;
220   }
221
222   LIST_FOR_EACH_ENTRY(p, &This->cont->properties, IDxDiagContainerImpl_Property, entry)
223   {
224     if (0 == lstrcmpW(p->propName, pwszPropName)) {
225       HRESULT hr = VariantClear(pvarProp);
226       if (hr == DISP_E_ARRAYISLOCKED || hr == DISP_E_BADVARTYPE)
227         VariantInit(pvarProp);
228
229       return VariantCopy(pvarProp, &p->vProp);
230     }
231   }
232
233   return E_INVALIDARG;
234 }
235
236 static const IDxDiagContainerVtbl DxDiagContainer_Vtbl =
237 {
238     IDxDiagContainerImpl_QueryInterface,
239     IDxDiagContainerImpl_AddRef,
240     IDxDiagContainerImpl_Release,
241     IDxDiagContainerImpl_GetNumberOfChildContainers,
242     IDxDiagContainerImpl_EnumChildContainerNames,
243     IDxDiagContainerImpl_GetChildContainer,
244     IDxDiagContainerImpl_GetNumberOfProps,
245     IDxDiagContainerImpl_EnumPropNames,
246     IDxDiagContainerImpl_GetProp
247 };
248
249
250 HRESULT DXDiag_CreateDXDiagContainer(REFIID riid, IDxDiagContainerImpl_Container *cont, IDxDiagProvider *pProv, LPVOID *ppobj) {
251   IDxDiagContainerImpl* container;
252
253   TRACE("(%p, %p)\n", debugstr_guid(riid), ppobj);
254   
255   container = HeapAlloc(GetProcessHeap(), 0, sizeof(IDxDiagContainerImpl));
256   if (NULL == container) {
257     *ppobj = NULL;
258     return E_OUTOFMEMORY;
259   }
260   container->lpVtbl = &DxDiagContainer_Vtbl;
261   container->ref = 0; /* will be inited with QueryInterface */
262   container->cont = cont;
263   container->pProv = pProv;
264   IDxDiagProvider_AddRef(pProv);
265   return IDxDiagContainerImpl_QueryInterface((PDXDIAGCONTAINER)container, riid, ppobj);
266 }