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.

197 lines
5.8 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 OLECHAR* 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: HrBuildPropertiesExFromProperties
  40. //
  41. // Purpose: Converts a NETCON_PROPERTIES and IPersistNetConnection to a
  42. // NETCON_PROPERTIES_EX
  43. //
  44. // Arguments: pProps [in] The NETCON_PROPERTIES to convert from
  45. // pPropsEx [out] The NETCON_PROPERTIES_EX to covert to. Caller allocated & free
  46. // pPersistNetConnection [in] The IPersistNetConnection used to build the NETCON_PROPERTIES_EX
  47. //
  48. // Returns: S_OK or an error code
  49. //
  50. HRESULT
  51. HrBuildPropertiesExFromProperties(IN const NETCON_PROPERTIES* pProps,
  52. OUT NETCON_PROPERTIES_EX* pPropsEx,
  53. IN IPersistNetConnection* pPersistNetConnection)
  54. {
  55. TraceFileFunc(ttidConman);
  56. HRESULT hr = S_OK;
  57. Assert(pProps);
  58. Assert(pPropsEx);
  59. BYTE* pbData;
  60. DWORD cbData;
  61. hr = pPersistNetConnection->GetSizeMax(&cbData);
  62. if (SUCCEEDED(hr))
  63. {
  64. hr = E_OUTOFMEMORY;
  65. pbData = new BYTE[cbData];
  66. if (pbData)
  67. {
  68. hr = pPersistNetConnection->Save (pbData, cbData);
  69. if (FAILED(hr))
  70. {
  71. delete [] pbData;
  72. }
  73. }
  74. }
  75. if (SUCCEEDED(hr))
  76. {
  77. hr = E_OUTOFMEMORY;
  78. pPropsEx->bstrPersistData = NULL;
  79. pPropsEx->bstrName = NULL;
  80. pPropsEx->bstrDeviceName = NULL;
  81. if ( (pPropsEx->bstrPersistData = SysAllocStringByteLen(reinterpret_cast<LPSTR>(pbData), cbData)) &&
  82. (SUCCEEDED(HrSysAllocString(&(pPropsEx->bstrName), pProps->pszwName))) &&
  83. (SUCCEEDED(HrSysAllocString(&(pPropsEx->bstrDeviceName), pProps->pszwDeviceName))) )
  84. {
  85. hr = S_OK;
  86. pPropsEx->guidId = pProps->guidId;
  87. pPropsEx->ncStatus = pProps->Status;
  88. pPropsEx->ncMediaType = pProps->MediaType;
  89. pPropsEx->dwCharacter = pProps->dwCharacter;
  90. pPropsEx->clsidThisObject = pProps->clsidThisObject;
  91. pPropsEx->clsidUiObject = pProps->clsidUiObject;
  92. pPropsEx->bstrPhoneOrHostAddress = SysAllocString(NULL);
  93. }
  94. else
  95. {
  96. SysFreeString(pPropsEx->bstrPersistData);
  97. SysFreeString(pPropsEx->bstrName);
  98. SysFreeString(pPropsEx->bstrDeviceName);
  99. }
  100. delete[] pbData;
  101. }
  102. TraceHr(ttidError, FAL, hr, FALSE, "HrBuildPropertiesExFromProperties");
  103. return hr;
  104. }
  105. //+---------------------------------------------------------------------------
  106. //
  107. // Function: HrGetPropertiesExFromINetConnection
  108. //
  109. // Purpose: Get the extended properties from INetConnection2, or get the
  110. // properties and build the extended properties
  111. //
  112. //
  113. // Arguments:
  114. // pPropsEx [in] The Properties to build from
  115. // ppsaProperties [out] The PropertiesEx to build from. Free with CoTaskMemFree
  116. //
  117. // Returns: HRESULT
  118. //
  119. // Author: ckotze 05 Apr 2001
  120. //
  121. // Notes: Caller must free ppPropsEx with CoTaskMemFree
  122. //
  123. //
  124. HRESULT HrGetPropertiesExFromINetConnection(IN INetConnection* pConn,
  125. OUT TAKEOWNERSHIP NETCON_PROPERTIES_EX** ppPropsEx)
  126. {
  127. TraceFileFunc(ttidConman);
  128. HRESULT hr = S_OK;
  129. CComPtr <INetConnection2> pConn2;
  130. Assert(ppPropsEx);
  131. *ppPropsEx = NULL;
  132. hr = pConn->QueryInterface(IID_INetConnection2, reinterpret_cast<LPVOID*>(&pConn2));
  133. if (SUCCEEDED(hr))
  134. {
  135. hr = pConn2->GetPropertiesEx(ppPropsEx);
  136. }
  137. else
  138. {
  139. NETCON_PROPERTIES_EX* pPropsEx = reinterpret_cast<NETCON_PROPERTIES_EX*>(CoTaskMemAlloc(sizeof(NETCON_PROPERTIES_EX)));
  140. if (pPropsEx)
  141. {
  142. NETCON_PROPERTIES* pProps;
  143. ZeroMemory(pPropsEx, sizeof(NETCON_PROPERTIES_EX));
  144. hr = pConn->GetProperties(&pProps);
  145. if (SUCCEEDED(hr))
  146. {
  147. CComPtr<IPersistNetConnection> pPersistNet;
  148. hr = pConn->QueryInterface(IID_IPersistNetConnection, reinterpret_cast<LPVOID*>(&pPersistNet));
  149. if (SUCCEEDED(hr))
  150. {
  151. hr = HrBuildPropertiesExFromProperties(pProps, pPropsEx, pPersistNet);
  152. if (SUCCEEDED(hr))
  153. {
  154. *ppPropsEx = pPropsEx;
  155. }
  156. else
  157. {
  158. HrFreeNetConProperties2(pPropsEx);
  159. pPropsEx = NULL;
  160. }
  161. }
  162. FreeNetconProperties(pProps);
  163. }
  164. if (FAILED(hr) && (pPropsEx))
  165. {
  166. CoTaskMemFree(pPropsEx);
  167. }
  168. }
  169. }
  170. return hr;
  171. }