2 * IDxDiagContainer Implementation
4 * Copyright 2004 Raphael Junqueira
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.
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.
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
25 #include "dxdiag_private.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);
31 /* IDxDiagContainer IUnknown parts follow: */
32 HRESULT WINAPI IDxDiagContainerImpl_QueryInterface(PDXDIAGCONTAINER iface, REFIID riid, LPVOID *ppobj)
34 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
36 if (!ppobj) return E_INVALIDARG;
38 if (IsEqualGUID(riid, &IID_IUnknown)
39 || IsEqualGUID(riid, &IID_IDxDiagContainer)) {
40 IUnknown_AddRef(iface);
45 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
50 static ULONG WINAPI IDxDiagContainerImpl_AddRef(PDXDIAGCONTAINER iface) {
51 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
52 ULONG refCount = InterlockedIncrement(&This->ref);
54 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
61 static ULONG WINAPI IDxDiagContainerImpl_Release(PDXDIAGCONTAINER iface) {
62 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
63 ULONG refCount = InterlockedDecrement(&This->ref);
65 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
68 HeapFree(GetProcessHeap(), 0, This);
71 DXDIAGN_UnlockModule();
76 /* IDxDiagContainer Interface follow: */
77 static HRESULT WINAPI IDxDiagContainerImpl_GetNumberOfChildContainers(PDXDIAGCONTAINER iface, DWORD* pdwCount) {
78 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
79 TRACE("(%p)\n", iface);
80 if (NULL == pdwCount) {
83 *pdwCount = This->nSubContainers;
87 static HRESULT WINAPI IDxDiagContainerImpl_EnumChildContainerNames(PDXDIAGCONTAINER iface, DWORD dwIndex, LPWSTR pwszContainer, DWORD cchContainer) {
88 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
89 IDxDiagContainerImpl_SubContainer *p;
92 TRACE("(%p, %u, %p, %u)\n", iface, dwIndex, pwszContainer, cchContainer);
94 if (NULL == pwszContainer || 0 == cchContainer) {
98 LIST_FOR_EACH_ENTRY(p, &This->subContainers, IDxDiagContainerImpl_SubContainer, entry)
101 TRACE("Found container name %s, copying string\n", debugstr_w(p->contName));
102 lstrcpynW(pwszContainer, p->contName, cchContainer);
103 return (cchContainer <= strlenW(p->contName)) ?
104 DXDIAG_E_INSUFFICIENT_BUFFER : S_OK;
109 TRACE("Failed to find container name at specified index\n");
110 *pwszContainer = '\0';
114 static HRESULT IDxDiagContainerImpl_GetChildContainerInternal(PDXDIAGCONTAINER iface, LPCWSTR pwszContainer, IDxDiagContainer** ppInstance) {
115 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
116 IDxDiagContainerImpl_SubContainer *p;
118 LIST_FOR_EACH_ENTRY(p, &This->subContainers, IDxDiagContainerImpl_SubContainer, entry)
120 if (0 == lstrcmpW(p->contName, pwszContainer)) {
121 *ppInstance = p->pCont;
129 static HRESULT WINAPI IDxDiagContainerImpl_GetChildContainer(PDXDIAGCONTAINER iface, LPCWSTR pwszContainer, IDxDiagContainer** ppInstance) {
130 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
131 IDxDiagContainer* pContainer = (PDXDIAGCONTAINER)This;
132 LPWSTR tmp, orig_tmp;
135 HRESULT hr = E_INVALIDARG;
137 TRACE("(%p, %s, %p)\n", iface, debugstr_w(pwszContainer), ppInstance);
139 if (NULL == ppInstance || NULL == pwszContainer) {
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);
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) || NULL == pContainer)
158 cur++; /* go after '.' (just replaced by \0) */
160 cur = strchrW(tmp, '.');
163 TRACE("Trying to get container %s\n", debugstr_w(tmp));
164 hr = IDxDiagContainerImpl_GetChildContainerInternal(pContainer, tmp, ppInstance);
166 TRACE("Succeeded in getting the container instance\n");
167 IDxDiagContainerImpl_AddRef(*ppInstance);
171 HeapFree(GetProcessHeap(), 0, orig_tmp);
175 static HRESULT WINAPI IDxDiagContainerImpl_GetNumberOfProps(PDXDIAGCONTAINER iface, DWORD* pdwCount) {
176 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
177 TRACE("(%p)\n", iface);
178 if (NULL == pdwCount) {
181 *pdwCount = This->nProperties;
185 static HRESULT WINAPI IDxDiagContainerImpl_EnumPropNames(PDXDIAGCONTAINER iface, DWORD dwIndex, LPWSTR pwszPropName, DWORD cchPropName) {
186 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
187 IDxDiagContainerImpl_Property *p;
190 TRACE("(%p, %u, %p, %u)\n", iface, dwIndex, pwszPropName, cchPropName);
192 if (NULL == pwszPropName || 0 == cchPropName) {
196 LIST_FOR_EACH_ENTRY(p, &This->properties, IDxDiagContainerImpl_Property, entry)
199 TRACE("Found property name %s, copying string\n", debugstr_w(p->propName));
200 lstrcpynW(pwszPropName, p->propName, cchPropName);
201 return (cchPropName <= strlenW(p->propName)) ?
202 DXDIAG_E_INSUFFICIENT_BUFFER : S_OK;
207 TRACE("Failed to find property name at specified index\n");
211 static HRESULT WINAPI IDxDiagContainerImpl_GetProp(PDXDIAGCONTAINER iface, LPCWSTR pwszPropName, VARIANT* pvarProp) {
212 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
213 IDxDiagContainerImpl_Property *p;
215 TRACE("(%p, %s, %p)\n", iface, debugstr_w(pwszPropName), pvarProp);
217 if (NULL == pvarProp || NULL == pwszPropName) {
221 LIST_FOR_EACH_ENTRY(p, &This->properties, IDxDiagContainerImpl_Property, entry)
223 if (0 == lstrcmpW(p->propName, pwszPropName)) {
224 HRESULT hr = VariantClear(pvarProp);
225 if (hr == DISP_E_ARRAYISLOCKED || hr == DISP_E_BADVARTYPE)
226 VariantInit(pvarProp);
228 return VariantCopy(pvarProp, &p->vProp);
235 HRESULT WINAPI IDxDiagContainerImpl_AddProp(PDXDIAGCONTAINER iface, LPCWSTR pwszPropName, VARIANT* pVarProp) {
236 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
237 IDxDiagContainerImpl_Property *pNew;
239 TRACE("(%p, %s, %p)\n", iface, debugstr_w(pwszPropName), pVarProp);
241 if (NULL == pVarProp || NULL == pwszPropName) {
245 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(IDxDiagContainerImpl_Property));
247 return E_OUTOFMEMORY;
250 pNew->propName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pwszPropName) + 1) * sizeof(WCHAR));
251 if (NULL == pNew->propName) {
252 HeapFree(GetProcessHeap(), 0, pNew);
253 return E_OUTOFMEMORY;
255 lstrcpyW(pNew->propName, pwszPropName);
256 VariantInit(&pNew->vProp);
257 VariantCopy(&pNew->vProp, pVarProp);
259 list_add_tail(&This->properties, &pNew->entry);
264 HRESULT WINAPI IDxDiagContainerImpl_AddChildContainer(PDXDIAGCONTAINER iface, LPCWSTR pszContName, PDXDIAGCONTAINER pSubCont) {
265 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
266 IDxDiagContainerImpl_SubContainer *pNew;
268 TRACE("(%p, %s, %p)\n", iface, debugstr_w(pszContName), pSubCont);
270 if (NULL == pSubCont || NULL == pszContName) {
274 pNew = HeapAlloc(GetProcessHeap(), 0, sizeof(IDxDiagContainerImpl_SubContainer));
276 return E_OUTOFMEMORY;
279 pNew->contName = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(pszContName) + 1) * sizeof(WCHAR));
280 if (NULL == pNew->contName) {
281 HeapFree(GetProcessHeap(), 0, pNew);
282 return E_OUTOFMEMORY;
284 lstrcpyW(pNew->contName, pszContName);
285 pNew->pCont = pSubCont;
287 list_add_tail(&This->subContainers, &pNew->entry);
288 ++This->nSubContainers;
292 static const IDxDiagContainerVtbl DxDiagContainer_Vtbl =
294 IDxDiagContainerImpl_QueryInterface,
295 IDxDiagContainerImpl_AddRef,
296 IDxDiagContainerImpl_Release,
297 IDxDiagContainerImpl_GetNumberOfChildContainers,
298 IDxDiagContainerImpl_EnumChildContainerNames,
299 IDxDiagContainerImpl_GetChildContainer,
300 IDxDiagContainerImpl_GetNumberOfProps,
301 IDxDiagContainerImpl_EnumPropNames,
302 IDxDiagContainerImpl_GetProp
306 HRESULT DXDiag_CreateDXDiagContainer(REFIID riid, LPVOID *ppobj) {
307 IDxDiagContainerImpl* container;
309 TRACE("(%p, %p)\n", debugstr_guid(riid), ppobj);
311 container = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagContainerImpl));
312 if (NULL == container) {
314 return E_OUTOFMEMORY;
316 container->lpVtbl = &DxDiagContainer_Vtbl;
317 container->ref = 0; /* will be inited with QueryInterface */
318 list_init(&container->properties);
319 list_init(&container->subContainers);
320 return IDxDiagContainerImpl_QueryInterface((PDXDIAGCONTAINER)container, riid, ppobj);