Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: C O N M A N 2 . C P P
  7. //
  8. // Contents: Connection manager 2.
  9. //
  10. // Notes:
  11. //
  12. // Author: ckotze 16 Mar 2001
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include <atlbase.h>
  18. #include "cobase.h"
  19. #include "conman.h"
  20. #include "conman2.h"
  21. #include "ncnetcon.h"
  22. HRESULT CConnectionManager2::EnumConnectionProperties(OUT SAFEARRAY** ppsaConnectionProperties)
  23. {
  24. TraceFileFunc(ttidConman);
  25. CComPtr<INetConnectionManager> pConMan;
  26. NETCON_PROPERTIES_EX* pPropsEx;
  27. HRESULT hr = S_OK;
  28. hr = CoCreateInstance(CLSID_ConnectionManager, NULL, CLSCTX_INPROC, IID_INetConnectionManager, reinterpret_cast<void**>(&pConMan));
  29. if (SUCCEEDED(hr))
  30. {
  31. CComPtr<IEnumNetConnection> pNetConnection;
  32. hr = pConMan->EnumConnections(NCME_DEFAULT, &pNetConnection);
  33. if (SUCCEEDED(hr))
  34. {
  35. INetConnection* pConn;
  36. ULONG ulFetched;
  37. LISTNETCONPROPEX listNCProperties;
  38. ITERNETCONPROPEX iterProps;
  39. HRESULT hrFetched = S_OK;
  40. do
  41. {
  42. hrFetched = pNetConnection->Next(1, &pConn, &ulFetched);
  43. if ( (S_OK == hr) && (ulFetched) )
  44. {
  45. CComPtr<INetConnection2> pConn2;
  46. // Does the class manager support INetConnection2?
  47. // if so use it directly
  48. hr = pConn->QueryInterface(IID_INetConnection2, reinterpret_cast<void**>(&pConn2));
  49. if (SUCCEEDED(hr))
  50. {
  51. hr = pConn2->GetPropertiesEx(&pPropsEx);
  52. }
  53. else
  54. {
  55. // This class manager does not support INetConnection2. Convert the connection
  56. NETCON_PROPERTIES* pProps;
  57. hr = pConn->GetProperties(&pProps);
  58. if (SUCCEEDED(hr))
  59. {
  60. pPropsEx = reinterpret_cast<NETCON_PROPERTIES_EX*>(CoTaskMemAlloc(sizeof(NETCON_PROPERTIES_EX)));
  61. if (pPropsEx)
  62. {
  63. CComPtr<IPersistNetConnection> pPersistNetConnection;
  64. hr = pConn->QueryInterface(IID_IPersistNetConnection, reinterpret_cast<LPVOID *>(&pPersistNetConnection));
  65. if (SUCCEEDED(hr))
  66. {
  67. ZeroMemory(pPropsEx, sizeof(NETCON_PROPERTIES_EX));
  68. hr = HrBuildPropertiesExFromProperties(pProps, pPropsEx, pPersistNetConnection);
  69. }
  70. }
  71. else
  72. {
  73. hr = E_OUTOFMEMORY;
  74. }
  75. FreeNetconProperties(pProps);
  76. }
  77. }
  78. if (S_OK == hr)
  79. {
  80. listNCProperties.insert(listNCProperties.end(), pPropsEx);
  81. }
  82. else
  83. {
  84. TraceTag(ttidError, "Failed to retrieve connection information for connection. Connection will be ommitted from Connections Folder.");
  85. }
  86. ReleaseObj(pConn);
  87. }
  88. } while ( (S_OK == hrFetched) && (ulFetched) );
  89. if (listNCProperties.size())
  90. {
  91. hr = S_OK;
  92. if (listNCProperties.size() != 0)
  93. {
  94. SAFEARRAYBOUND rgsaBound[1];
  95. LONG lIndex;
  96. rgsaBound[0].cElements = listNCProperties.size();
  97. rgsaBound[0].lLbound = 0;
  98. lIndex = rgsaBound[0].lLbound;
  99. *ppsaConnectionProperties = SafeArrayCreate(VT_VARIANT, 1, rgsaBound);
  100. for (ITERNETCONPROPEX iter = listNCProperties.begin(); iter != listNCProperties.end(); iter++)
  101. {
  102. HRESULT hrT = S_OK;
  103. VARIANT varElement;
  104. SAFEARRAY* psaPropertiesEx = NULL;
  105. pPropsEx = *iter;
  106. hrT = HrSafeArrayFromNetConPropertiesEx(pPropsEx, &psaPropertiesEx);
  107. if (SUCCEEDED(hrT))
  108. {
  109. VariantInit(&varElement);
  110. varElement.vt = VT_VARIANT | VT_ARRAY;
  111. varElement.parray = psaPropertiesEx;
  112. hrT = SafeArrayPutElement(*ppsaConnectionProperties, &lIndex, &varElement);
  113. VariantClear(&varElement);
  114. }
  115. if (FAILED(hrT))
  116. {
  117. hr = hrT;
  118. break;
  119. }
  120. lIndex++;
  121. }
  122. }
  123. }
  124. for (ITERNETCONPROPEX iter = listNCProperties.begin(); iter != listNCProperties.end(); iter++)
  125. {
  126. HrFreeNetConProperties2(*iter);
  127. }
  128. }
  129. }
  130. if (SUCCEEDED(hr) && !(*ppsaConnectionProperties) )
  131. {
  132. hr = S_FALSE;
  133. }
  134. TraceHr (ttidError, FAL, hr, FALSE, "CConnectionManager2::EnumConnectionProperties");
  135. return hr;
  136. }