Source code of Windows XP (NT5)
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.

187 lines
5.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: C O B A S E . C P P
  7. //
  8. // Contents: Connection Objects Shared code
  9. //
  10. // Notes:
  11. //
  12. // Author: ckotze 16 Mar 2001
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "cobase.h"
  18. #include "netconp.h"
  19. #include "ncnetcon.h"
  20. HRESULT HrSysAllocString(BSTR *bstrDestination, const BSTR bstrSource)
  21. {
  22. HRESULT hr = S_OK;
  23. if (bstrSource)
  24. {
  25. *bstrDestination = SysAllocString(bstrSource);
  26. if (!*bstrDestination)
  27. {
  28. return E_OUTOFMEMORY;
  29. }
  30. }
  31. else
  32. {
  33. *bstrDestination = SysAllocString(NULL);
  34. }
  35. return hr;
  36. }
  37. //+---------------------------------------------------------------------------
  38. //
  39. // Member: CDialupConnection::GetDefault
  40. //
  41. // Purpose: Get the default RAS connection
  42. //
  43. // Arguments: pbDefault - Is this the default connection
  44. //
  45. // Returns: S_OK or an error code
  46. //
  47. HRESULT
  48. HrBuildPropertiesExFromProperties(NETCON_PROPERTIES* pProps, NETCON_PROPERTIES_EX* pPropsEx, IPersistNetConnection* pPersistNetConnection)
  49. {
  50. HRESULT hr = S_OK;
  51. Assert(pProps);
  52. Assert(pPropsEx);
  53. BYTE* pbData;
  54. DWORD cbData;
  55. hr = pPersistNetConnection->GetSizeMax(&cbData);
  56. if (SUCCEEDED(hr))
  57. {
  58. hr = E_OUTOFMEMORY;
  59. pbData = new BYTE[cbData];
  60. if (pbData)
  61. {
  62. hr = pPersistNetConnection->Save (pbData, cbData);
  63. if (FAILED(hr))
  64. {
  65. delete [] pbData;
  66. }
  67. }
  68. }
  69. if (SUCCEEDED(hr))
  70. {
  71. hr = E_OUTOFMEMORY;
  72. pPropsEx->bstrPersistData = NULL;
  73. pPropsEx->bstrName = NULL;
  74. pPropsEx->bstrDeviceName = NULL;
  75. if ( (pPropsEx->bstrPersistData = SysAllocStringByteLen(reinterpret_cast<LPSTR>(pbData), cbData)) &&
  76. (SUCCEEDED(HrSysAllocString(&(pPropsEx->bstrName), pProps->pszwName))) &&
  77. (SUCCEEDED(HrSysAllocString(&(pPropsEx->bstrDeviceName), pProps->pszwDeviceName))) )
  78. {
  79. hr = S_OK;
  80. pPropsEx->guidId = pProps->guidId;
  81. pPropsEx->ncStatus = pProps->Status;
  82. pPropsEx->ncMediaType = pProps->MediaType;
  83. pPropsEx->dwCharacter = pProps->dwCharacter;
  84. pPropsEx->clsidThisObject = pProps->clsidThisObject;
  85. pPropsEx->clsidUiObject = pProps->clsidUiObject;
  86. pPropsEx->bstrPhoneOrHostAddress = SysAllocString(NULL);
  87. }
  88. else
  89. {
  90. SysFreeString(pPropsEx->bstrPersistData);
  91. SysFreeString(pPropsEx->bstrName);
  92. SysFreeString(pPropsEx->bstrDeviceName);
  93. }
  94. delete[] pbData;
  95. }
  96. TraceHr(ttidError, FAL, hr, FALSE, "HrBuildPropertiesExFromProperties");
  97. return hr;
  98. }
  99. //+---------------------------------------------------------------------------
  100. //
  101. // Function: HrGetPropertiesExFromINetConnection
  102. //
  103. // Purpose: Get the extended properties from INetConnection2, or get the
  104. // properties and build the extended properties
  105. //
  106. //
  107. // Arguments:
  108. // pPropsEx [in] Properties to use to build the safe array.
  109. // ppsaProperties [out] Safe array in which to store data.
  110. //
  111. // Returns: HRESULT
  112. //
  113. // Author: ckotze 05 Apr 2001
  114. //
  115. // Notes: Caller must free array and contents.
  116. //
  117. //
  118. HRESULT HrGetPropertiesExFromINetConnection(INetConnection* pConn, NETCON_PROPERTIES_EX** ppPropsEx)
  119. {
  120. HRESULT hr = S_OK;
  121. CComPtr <INetConnection2> pConn2;
  122. Assert(ppPropsEx);
  123. *ppPropsEx = NULL;
  124. hr = pConn->QueryInterface(IID_INetConnection2, reinterpret_cast<LPVOID*>(&pConn2));
  125. if (SUCCEEDED(hr))
  126. {
  127. hr = pConn2->GetPropertiesEx(ppPropsEx);
  128. }
  129. else
  130. {
  131. NETCON_PROPERTIES_EX* pPropsEx = reinterpret_cast<NETCON_PROPERTIES_EX*>(CoTaskMemAlloc(sizeof(NETCON_PROPERTIES_EX)));
  132. if (pPropsEx)
  133. {
  134. NETCON_PROPERTIES* pProps;
  135. ZeroMemory(pPropsEx, sizeof(NETCON_PROPERTIES_EX));
  136. hr = pConn->GetProperties(&pProps);
  137. if (SUCCEEDED(hr))
  138. {
  139. CComPtr<IPersistNetConnection> pPersistNet;
  140. hr = pConn->QueryInterface(IID_IPersistNetConnection, reinterpret_cast<LPVOID*>(&pPersistNet));
  141. if (SUCCEEDED(hr))
  142. {
  143. hr = HrBuildPropertiesExFromProperties(pProps, pPropsEx, pPersistNet);
  144. if (SUCCEEDED(hr))
  145. {
  146. *ppPropsEx = pPropsEx;
  147. }
  148. else
  149. {
  150. HrFreeNetConProperties2(pPropsEx);
  151. pPropsEx = NULL;
  152. }
  153. }
  154. FreeNetconProperties(pProps);
  155. }
  156. if (FAILED(hr) && (pPropsEx))
  157. {
  158. CoTaskMemFree(pPropsEx);
  159. }
  160. }
  161. }
  162. return hr;
  163. }