Make acpi compile with Visual C++
[reactos.git] / dll / win32 / netcfgx / inetcfgcomp_iface.c
1 #include "precomp.h"
2 #include <devguid.h>
3
4 typedef struct
5 {
6 const INetCfgComponent * lpVtbl;
7 LONG ref;
8 NetCfgComponentItem * pItem;
9 INetCfgComponentPropertyUi * pProperty;
10 INetCfgComponentControl * pNCCC;
11 INetCfg * pNCfg;
12 }INetCfgComponentImpl;
13
14 typedef struct
15 {
16 const IEnumNetCfgComponent * lpVtbl;
17 LONG ref;
18 NetCfgComponentItem * pCurrent;
19 NetCfgComponentItem * pHead;
20 INetCfg * pNCfg;
21 }IEnumNetCfgComponentImpl;
22
23
24
25 HRESULT
26 STDCALL
27 INetCfgComponent_fnQueryInterface(
28 INetCfgComponent * iface,
29 REFIID iid,
30 LPVOID * ppvObj)
31 {
32 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
33 *ppvObj = NULL;
34
35 if (IsEqualIID (iid, &IID_IUnknown) ||
36 IsEqualIID (iid, &IID_INetCfgComponent))
37 {
38 *ppvObj = This;
39 INetCfg_AddRef(iface);
40 return S_OK;
41 }
42
43 return E_NOINTERFACE;
44 }
45
46
47 ULONG
48 STDCALL
49 INetCfgComponent_fnAddRef(
50 INetCfgComponent * iface)
51 {
52 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
53 ULONG refCount = InterlockedIncrement(&This->ref);
54
55 return refCount;
56 }
57
58 ULONG
59 STDCALL
60 INetCfgComponent_fnRelease(
61 INetCfgComponent * iface)
62 {
63 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
64 ULONG refCount = InterlockedDecrement(&This->ref);
65
66 if (!refCount)
67 {
68 CoTaskMemFree(This);
69 }
70 return refCount;
71 }
72
73 HRESULT
74 STDCALL
75 INetCfgComponent_fnGetDisplayName(
76 INetCfgComponent * iface,
77 LPWSTR * ppszwDisplayName)
78 {
79 LPWSTR szName;
80 UINT Length;
81 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
82
83 if (This == NULL || ppszwDisplayName == NULL)
84 return E_POINTER;
85
86 if (This->pItem->szDisplayName)
87 Length = wcslen(This->pItem->szDisplayName)+1;
88 else
89 Length = 1;
90
91 szName = CoTaskMemAlloc(Length * sizeof(WCHAR));
92 if (!szName)
93 return E_OUTOFMEMORY;
94
95 if (Length > 1)
96 wcscpy(szName, This->pItem->szDisplayName);
97 else
98 szName[0] = L'\0';
99
100 *ppszwDisplayName = szName;
101
102 return S_OK;
103 }
104
105 HRESULT
106 STDCALL
107 INetCfgComponent_fnSetDisplayName(
108 INetCfgComponent * iface,
109 LPCWSTR ppszwDisplayName)
110 {
111 LPWSTR szName;
112 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
113
114 if (This == NULL || ppszwDisplayName == NULL)
115 return E_POINTER;
116
117 /* setting name is only supported for network cards */
118 if (!IsEqualGUID(&This->pItem->ClassGUID, &GUID_DEVCLASS_NET))
119 return E_NOTIMPL;
120
121 /// FIXME
122 /// check for invalid characters
123 /// check for write lock
124
125 szName = CoTaskMemAlloc((wcslen(ppszwDisplayName)+1) * sizeof(WCHAR));
126 if (!szName)
127 return E_OUTOFMEMORY;
128
129 wcscpy(szName, ppszwDisplayName);
130 CoTaskMemFree(This->pItem->szDisplayName);
131 This->pItem->szDisplayName = szName;
132 This->pItem->bChanged = TRUE;
133
134 return S_OK;
135 }
136
137 HRESULT
138 STDCALL
139 INetCfgComponent_fnGetHelpText(
140 INetCfgComponent * iface,
141 LPWSTR * ppszwHelpText)
142 {
143 LPWSTR szHelp;
144 UINT Length;
145 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
146
147 if (This == NULL || ppszwHelpText == NULL)
148 return E_POINTER;
149
150 if (This->pItem->szHelpText)
151 Length = wcslen(This->pItem->szHelpText)+1;
152 else
153 Length = 1;
154
155 szHelp = CoTaskMemAlloc(Length * sizeof(WCHAR));
156 if (!szHelp)
157 return E_OUTOFMEMORY;
158
159 if (Length > 1)
160 wcscpy(szHelp, This->pItem->szHelpText);
161 else
162 szHelp[0] = L'\0';
163
164 *ppszwHelpText = szHelp;
165
166 return S_OK;
167 }
168
169 HRESULT
170 STDCALL
171 INetCfgComponent_fnGetId(
172 INetCfgComponent * iface,
173 LPWSTR * ppszwId)
174 {
175 LPWSTR szId;
176 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
177
178 if (This == NULL || ppszwId == NULL)
179 return E_POINTER;
180
181 szId = CoTaskMemAlloc((wcslen(This->pItem->szId)+1) * sizeof(WCHAR));
182 if (!szId)
183 return E_OUTOFMEMORY;
184
185 wcscpy(szId, This->pItem->szId);
186 *ppszwId = szId;
187
188 return S_OK;
189 }
190
191 HRESULT
192 STDCALL
193 INetCfgComponent_fnGetCharacteristics(
194 INetCfgComponent * iface,
195 DWORD * pdwCharacteristics)
196 {
197 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
198
199 if (This == NULL || pdwCharacteristics == NULL)
200 return E_POINTER;
201
202 *pdwCharacteristics = This->pItem->dwCharacteristics;
203
204 return S_OK;
205 }
206
207 HRESULT
208 STDCALL
209 INetCfgComponent_fnGetInstanceGuid(
210 INetCfgComponent * iface,
211 GUID * pGuid)
212 {
213 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
214
215 if (This == NULL || pGuid == NULL)
216 return E_POINTER;
217
218 CopyMemory(pGuid, &This->pItem->InstanceId, sizeof(GUID));
219 return S_OK;
220 }
221
222 HRESULT
223 STDCALL
224 INetCfgComponent_fnGetPnpDevNodeId(
225 INetCfgComponent * iface,
226 LPWSTR * ppszwDevNodeId)
227 {
228 LPWSTR szNode;
229 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
230
231 if (This == NULL || ppszwDevNodeId == NULL)
232 return E_POINTER;
233
234 if (!IsEqualGUID(&GUID_DEVCLASS_NET, &This->pItem->ClassGUID))
235 return E_NOTIMPL;
236
237 szNode = CoTaskMemAlloc((wcslen(This->pItem->szNodeId)+1) * sizeof(WCHAR));
238 if (!szNode)
239 return E_OUTOFMEMORY;
240
241 wcscpy(szNode, This->pItem->szNodeId);
242 *ppszwDevNodeId = szNode;
243 return S_OK;
244 }
245
246 HRESULT
247 STDCALL
248 INetCfgComponent_fnGetClassGuid(
249 INetCfgComponent * iface,
250 GUID * pGuid)
251 {
252 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
253
254 if (This == NULL || pGuid == NULL)
255 return E_POINTER;
256
257 CopyMemory(pGuid, &This->pItem->ClassGUID, sizeof(GUID));
258 return S_OK;
259 }
260
261 HRESULT
262 STDCALL
263 INetCfgComponent_fnGetBindName(
264 INetCfgComponent * iface,
265 LPWSTR * ppszwBindName)
266 {
267 LPWSTR szBind;
268 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
269
270 if (This == NULL || ppszwBindName == NULL)
271 return E_POINTER;
272
273 szBind = CoTaskMemAlloc((wcslen(This->pItem->szBindName)+1) * sizeof(WCHAR));
274 if (!szBind)
275 return E_OUTOFMEMORY;
276
277 wcscpy(szBind, This->pItem->szBindName);
278 *ppszwBindName = szBind;
279
280 return S_OK;
281 }
282
283 HRESULT
284 STDCALL
285 INetCfgComponent_fnGetDeviceStatus(
286 INetCfgComponent * iface,
287 ULONG * pStatus)
288 {
289 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
290
291 if (This == NULL || pStatus == NULL)
292 return E_POINTER;
293
294 if (!IsEqualGUID(&GUID_DEVCLASS_NET, &This->pItem->ClassGUID))
295 return E_UNEXPECTED;
296
297 *pStatus = This->pItem->Status;
298
299 return S_OK;
300 }
301
302 HRESULT
303 STDCALL
304 INetCfgComponent_fnOpenParamKey(
305 INetCfgComponent * iface,
306 HKEY * phkey)
307 {
308 WCHAR szBuffer[200] = L"SYSTEM\\CurrentControlSet\\Services\\";
309 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
310
311 if (This == NULL || phkey == NULL)
312 return E_POINTER;
313
314 wcscat(szBuffer, This->pItem->szBindName);
315 wcscat(szBuffer, L"\\Parameters");
316
317 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szBuffer, 0, KEY_READ | KEY_WRITE, phkey) == ERROR_SUCCESS)
318 return S_OK;
319 else
320 return E_FAIL;
321 }
322
323
324 HRESULT
325 CreateNotificationObject(
326 INetCfgComponentImpl * This,
327 INetCfgComponent * iface,
328 IUnknown *pUnk)
329 {
330 WCHAR szName[150];
331 HKEY hKey;
332 DWORD dwSize, dwType;
333 GUID CLSID_NotifyObject;
334 LPOLESTR pStr;
335 INetCfgComponentPropertyUi * pNCCPU;
336 INetCfgComponentControl * pNCCC;
337 HRESULT hr;
338 LONG lRet;
339 CLSID ClassGUID;
340 //CLSID InstanceGUID;
341
342 wcscpy(szName,L"SYSTEM\\CurrentControlSet\\Control\\Network\\");
343
344 /* get the Class GUID */
345 hr = INetCfgComponent_GetClassGuid(iface, &ClassGUID);
346 if (FAILED(hr))
347 return hr;
348
349 hr = StringFromCLSID(&ClassGUID, &pStr);
350 if (FAILED(hr))
351 return hr;
352
353 wcscat(szName, pStr);
354 CoTaskMemFree(pStr);
355 wcscat(szName, L"\\");
356
357 /* get the Instance GUID */
358 #if 0
359 hr = INetCfgComponent_GetInstanceGuid(iface, &InstanceGUID);
360 if (FAILED(hr))
361 return hr;
362
363 hr = StringFromCLSID(&InstanceGUID, &pStr);
364 if (FAILED(hr))
365 return hr;
366
367 wcscat(szName, pStr);
368 CoTaskMemFree(pStr);
369 #else
370 // HACK HACK HACK HACK HACK
371 wcscat(szName, L"{RandomProtocolGUID_TCPIP}");
372 #endif
373
374 wcscat(szName, L"\\NDI");
375
376 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szName, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
377 return E_FAIL;
378
379 dwSize = sizeof(szName);
380 lRet = RegQueryValueExW(hKey, L"ClsID", NULL, &dwType, (LPBYTE)szName, &dwSize);
381 RegCloseKey(hKey);
382
383 if (lRet != ERROR_SUCCESS && dwType != REG_SZ)
384 return E_FAIL;
385
386 hr = CLSIDFromString(szName, &CLSID_NotifyObject);
387 if (FAILED(hr))
388 return E_FAIL;
389
390 hr = CoCreateInstance(&CLSID_NotifyObject, NULL, CLSCTX_INPROC_SERVER, &IID_INetCfgComponentPropertyUi, (LPVOID*)&pNCCPU);
391 if (FAILED(hr))
392 return E_FAIL;
393
394 hr = INetCfgComponentPropertyUi_QueryInterface(pNCCPU, &IID_INetCfgComponentControl, (LPVOID*)&pNCCC);
395 if (FAILED(hr))
396 {
397 INetCfgComponentPropertyUi_Release(pNCCPU);
398 return hr;
399 }
400
401 hr = INetCfgComponentPropertyUi_QueryPropertyUi(pNCCPU, pUnk);
402 if (FAILED(hr))
403 {
404 INetCfgComponentPropertyUi_Release(pNCCPU);
405 return hr;
406 }
407
408 hr = INetCfgComponentControl_Initialize(pNCCC, iface, This->pNCfg, FALSE);
409 if (FAILED(hr))
410 {
411 INetCfgComponentControl_Release(pNCCC);
412 INetCfgComponentPropertyUi_Release(pNCCPU);
413 return hr;
414 }
415
416 hr = INetCfgComponentPropertyUi_SetContext(pNCCPU, pUnk);
417 if (FAILED(hr))
418 {
419 INetCfgComponentPropertyUi_Release(pNCCPU);
420 return hr;
421 }
422 This->pProperty = pNCCPU;
423 This->pNCCC = pNCCC;
424
425 return S_OK;
426 }
427
428 HRESULT
429 STDCALL
430 INetCfgComponent_fnRaisePropertyUi(
431 INetCfgComponent * iface,
432 IN HWND hwndParent,
433 IN DWORD dwFlags,
434 IN IUnknown *pUnk)
435 {
436 HRESULT hr;
437 DWORD dwDefPages;
438 UINT Pages;
439 PROPSHEETHEADERW pinfo;
440 HPROPSHEETPAGE * hppages;
441 INT_PTR iResult;
442 INetCfgComponentImpl * This = (INetCfgComponentImpl*)iface;
443
444 if (!This->pProperty)
445 {
446 hr = CreateNotificationObject(This,iface, pUnk);
447 if (FAILED(hr))
448 return hr;
449 if (dwFlags == NCRP_QUERY_PROPERTY_UI)
450 return S_OK;
451 }
452
453 dwDefPages = 0;
454 Pages = 0;
455
456 hr = INetCfgComponentPropertyUi_MergePropPages(This->pProperty, &dwDefPages, (BYTE**)&hppages, &Pages, hwndParent, NULL);
457 if (FAILED(hr) || !Pages)
458 {
459 return hr;
460 }
461 ZeroMemory(&pinfo, sizeof(PROPSHEETHEADERW));
462 pinfo.dwSize = sizeof(PROPSHEETHEADERW);
463 pinfo.dwFlags = PSH_NOCONTEXTHELP | PSH_PROPTITLE | PSH_NOAPPLYNOW;
464 pinfo.u3.phpage = hppages;
465 pinfo.hwndParent = hwndParent;
466 pinfo.nPages = Pages;
467 pinfo.pszCaption = This->pItem->szDisplayName;
468
469 iResult = PropertySheetW(&pinfo);
470
471 CoTaskMemFree(hppages);
472 if (iResult < 0)
473 {
474 //FIXME
475 INetCfgComponentControl_CancelChanges(This->pNCCC);
476 return E_ABORT;
477 }
478 else
479 {
480 //FIXME
481 INetCfgComponentControl_ApplyRegistryChanges(This->pNCCC);
482 return S_OK;
483 }
484 }
485 static const INetCfgComponentVtbl vt_NetCfgComponent =
486 {
487 INetCfgComponent_fnQueryInterface,
488 INetCfgComponent_fnAddRef,
489 INetCfgComponent_fnRelease,
490 INetCfgComponent_fnGetDisplayName,
491 INetCfgComponent_fnSetDisplayName,
492 INetCfgComponent_fnGetHelpText,
493 INetCfgComponent_fnGetId,
494 INetCfgComponent_fnGetCharacteristics,
495 INetCfgComponent_fnGetInstanceGuid,
496 INetCfgComponent_fnGetPnpDevNodeId,
497 INetCfgComponent_fnGetClassGuid,
498 INetCfgComponent_fnGetBindName,
499 INetCfgComponent_fnGetDeviceStatus,
500 INetCfgComponent_fnOpenParamKey,
501 INetCfgComponent_fnRaisePropertyUi
502 };
503
504 HRESULT
505 STDCALL
506 INetCfgComponent_Constructor (IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv, NetCfgComponentItem * pItem, INetCfg * pNCfg)
507 {
508 INetCfgComponentImpl *This;
509
510 if (!ppv)
511 return E_POINTER;
512
513 This = (INetCfgComponentImpl *) CoTaskMemAlloc(sizeof (INetCfgComponentImpl));
514 if (!This)
515 return E_OUTOFMEMORY;
516
517 This->ref = 1;
518 This->lpVtbl = (const INetCfgComponent*)&vt_NetCfgComponent;
519 This->pProperty = NULL;
520 This->pItem = pItem;
521 This->pNCfg = pNCfg;
522
523 if (!SUCCEEDED (INetCfgComponent_QueryInterface ((INetCfgComponent*)This, riid, ppv)))
524 {
525 INetCfgComponent_Release((INetCfg*)This);
526 return E_NOINTERFACE;
527 }
528
529 INetCfgComponent_Release((INetCfgComponent*)This);
530 return S_OK;
531 }
532
533
534 /***************************************************************
535 * IEnumNetCfgComponent
536 */
537
538 HRESULT
539 STDCALL
540 IEnumNetCfgComponent_fnQueryInterface(
541 IEnumNetCfgComponent * iface,
542 REFIID iid,
543 LPVOID * ppvObj)
544 {
545 IEnumNetCfgComponentImpl * This = (IEnumNetCfgComponentImpl*)iface;
546 *ppvObj = NULL;
547
548 if (IsEqualIID (iid, &IID_IUnknown) ||
549 IsEqualIID (iid, &IID_IEnumNetCfgComponent))
550 {
551 *ppvObj = This;
552 INetCfg_AddRef(iface);
553 return S_OK;
554 }
555
556 return E_NOINTERFACE;
557 }
558
559
560 ULONG
561 STDCALL
562 IEnumNetCfgComponent_fnAddRef(
563 IEnumNetCfgComponent * iface)
564 {
565 IEnumNetCfgComponentImpl * This = (IEnumNetCfgComponentImpl*)iface;
566 ULONG refCount = InterlockedIncrement(&This->ref);
567
568 return refCount;
569 }
570
571 ULONG
572 STDCALL
573 IEnumNetCfgComponent_fnRelease(
574 IEnumNetCfgComponent * iface)
575 {
576 IEnumNetCfgComponentImpl * This = (IEnumNetCfgComponentImpl*)iface;
577 ULONG refCount = InterlockedDecrement(&This->ref);
578
579 return refCount;
580 }
581
582 HRESULT
583 STDCALL
584 IEnumNetCfgComponent_fnNext(
585 IEnumNetCfgComponent * iface,
586 ULONG celt,
587 INetCfgComponent **rgelt,
588 ULONG *pceltFetched)
589 {
590 HRESULT hr;
591 IEnumNetCfgComponentImpl * This = (IEnumNetCfgComponentImpl*)iface;
592
593 if (!iface || !rgelt)
594 return E_POINTER;
595
596 if (celt != 1)
597 return E_INVALIDARG;
598
599 if (!This->pCurrent)
600 return S_FALSE;
601
602 hr = INetCfgComponent_Constructor (NULL, &IID_INetCfgComponent, (LPVOID*)rgelt, This->pCurrent, This->pNCfg);
603 if (SUCCEEDED(hr))
604 {
605 This->pCurrent = This->pCurrent->pNext;
606 if (pceltFetched)
607 *pceltFetched = 1;
608 }
609 return hr;
610 }
611
612 HRESULT
613 STDCALL
614 IEnumNetCfgComponent_fnSkip(
615 IEnumNetCfgComponent * iface,
616 ULONG celt)
617 {
618 IEnumNetCfgComponentImpl * This = (IEnumNetCfgComponentImpl*)iface;
619
620 if (!This->pCurrent)
621 return S_FALSE;
622
623 while(celt-- > 0 && This->pCurrent)
624 This->pCurrent = This->pCurrent->pNext;
625
626 if (!celt)
627 return S_OK;
628 else
629 return S_FALSE;
630 }
631
632 HRESULT
633 STDCALL
634 IEnumNetCfgComponent_fnReset(
635 IEnumNetCfgComponent * iface)
636 {
637 IEnumNetCfgComponentImpl * This = (IEnumNetCfgComponentImpl*)iface;
638
639 This->pCurrent = This->pHead;
640 return S_OK;
641 }
642
643 HRESULT
644 STDCALL
645 IEnumNetCfgComponent_fnClone(
646 IEnumNetCfgComponent * iface,
647 IEnumNetCfgComponent **ppenum)
648 {
649 return E_NOTIMPL;
650 }
651
652 static const IEnumNetCfgComponentVtbl vt_EnumNetCfgComponent =
653 {
654 IEnumNetCfgComponent_fnQueryInterface,
655 IEnumNetCfgComponent_fnAddRef,
656 IEnumNetCfgComponent_fnRelease,
657 IEnumNetCfgComponent_fnNext,
658 IEnumNetCfgComponent_fnSkip,
659 IEnumNetCfgComponent_fnReset,
660 IEnumNetCfgComponent_fnClone
661 };
662
663 HRESULT
664 STDCALL
665 IEnumNetCfgComponent_Constructor (IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv, NetCfgComponentItem * pItem, INetCfg * pNCfg)
666 {
667 IEnumNetCfgComponentImpl *This;
668
669 if (!ppv)
670 return E_POINTER;
671
672 This = (IEnumNetCfgComponentImpl *) CoTaskMemAlloc(sizeof (IEnumNetCfgComponentImpl));
673 if (!This)
674 return E_OUTOFMEMORY;
675
676 This->ref = 1;
677 This->lpVtbl = (const IEnumNetCfgComponent*)&vt_EnumNetCfgComponent;
678 This->pCurrent = pItem;
679 This->pHead = pItem;
680 This->pNCfg = pNCfg;
681
682 if (!SUCCEEDED (IEnumNetCfgComponent_QueryInterface ((INetCfgComponent*)This, riid, ppv)))
683 {
684 IEnumNetCfgComponent_Release((INetCfg*)This);
685 return E_NOINTERFACE;
686 }
687
688 IEnumNetCfgComponent_Release((IEnumNetCfgComponent*)This);
689 return S_OK;
690 }
691
692