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.

225 lines
7.0 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) 1997-1999, Microsoft Corporation **/
  4. /**********************************************************************/
  5. /*
  6. ndisutil.cpp
  7. FILE HISTORY:
  8. */
  9. #include "stdafx.h"
  10. #include "ndisutil.h"
  11. #include "rtrstr.h"
  12. #include "raserror.h"
  13. #include "ustringp.h"
  14. #include <wchar.h>
  15. #include <assert.h>
  16. #include "ncutil.h"
  17. #include <ndispnp.h>
  18. #define c_szDevice L"\\Device\\"
  19. #define c_szEmpty L""
  20. //-------------------------------------------------------------------
  21. // Function: SetUnicodeString
  22. //
  23. // Purpose: given a UNICODE_STRING initialize it to the given WSTR
  24. //
  25. // Parameters:
  26. // pustr - the UNICODE_STRING to initialize
  27. // psz - the WSTR to use to initialize the UNICODE_STRING
  28. //
  29. // Notes: This differs from the RtlInitUnicodeString in that the
  30. // MaximumLength value contains the terminating null
  31. //
  32. //-------------------------------------------------------------------
  33. void
  34. SetUnicodeString (
  35. IN OUT UNICODE_STRING* pustr,
  36. IN LPCWSTR psz )
  37. {
  38. AssertSz( pustr != NULL, "Invalid Argument" );
  39. AssertSz( psz != NULL, "Invalid Argument" );
  40. pustr->Buffer = const_cast<PWSTR>(psz);
  41. pustr->Length = (USHORT)(lstrlenW(pustr->Buffer) * sizeof(WCHAR));
  42. pustr->MaximumLength = pustr->Length + sizeof(WCHAR);
  43. }
  44. //-------------------------------------------------------------------
  45. // Function: SetUnicodeMultiString
  46. //
  47. // Purpose: given a UNICODE_STRING initialize it to the given WSTR
  48. // multi string buffer
  49. //
  50. // Parameters:
  51. // pustr - the UNICODE_STRING to initialize
  52. // pmsz - the multi sz WSTR to use to initialize the UNICODE_STRING
  53. //
  54. //-------------------------------------------------------------------
  55. void
  56. SetUnicodeMultiString (
  57. IN OUT UNICODE_STRING* pustr,
  58. IN LPCWSTR pmsz )
  59. {
  60. AssertSz( pustr != NULL, "Invalid Argument" );
  61. AssertSz( pmsz != NULL, "Invalid Argument" );
  62. pustr->Buffer = const_cast<PWSTR>(pmsz);
  63. // Note: Length does NOT include terminating NULL
  64. pustr->Length = wcslen(pustr->Buffer) * sizeof(WCHAR);
  65. pustr->MaximumLength = pustr->Length;
  66. }
  67. //-------------------------------------------------------------------
  68. // Function: HrSendNdisHandlePnpEvent
  69. //
  70. // Purpose: Send to Ndis a HandlePnpEvent notification
  71. //
  72. // Parameters:
  73. // uiLayer - either NDIS or TDI
  74. // uiOperation - either BIND, RECONFIGURE, or UNBIND
  75. // pszUpper - a WIDE string containing the upper component name
  76. // pszLower - a WIDE string containing the lower component name
  77. // This is one of the Export names from that component
  78. // The values NULL and c_szEmpty are both supported
  79. // pmszBindList - a WIDE string containing the NULL terminiated list of strings
  80. // representing the bindlist, vaid only for reconfigure
  81. // The values NULL and c_szEmpty are both supported
  82. // pvData - Pointer to ndis component notification data. Content
  83. // determined by each component.
  84. // dwSizeData - Count of bytes in pvData
  85. //
  86. // Returns: HRESULT S_OK on success, HrFromLastWin32Error otherwise
  87. //
  88. // Notes: Do not use this routine directly, see...
  89. // HrSendNdisPnpBindOrderChange,
  90. // HrSendNdisPnpBindStateChange, or
  91. // HrSendNdisPnpReconfig
  92. //
  93. //-------------------------------------------------------------------
  94. HRESULT
  95. HrSendNdisHandlePnpEvent (
  96. UINT uiLayer,
  97. UINT uiOperation,
  98. LPCWSTR pszUpper,
  99. LPCWSTR pszLower,
  100. LPCWSTR pmszBindList,
  101. PVOID pvData,
  102. DWORD dwSizeData)
  103. {
  104. UNICODE_STRING umstrBindList;
  105. UNICODE_STRING ustrLower;
  106. UNICODE_STRING ustrUpper;
  107. UINT nRet;
  108. HRESULT hr = S_OK;
  109. Assert(NULL != pszUpper);
  110. Assert((NDIS == uiLayer)||(TDI == uiLayer));
  111. Assert( (BIND == uiOperation) || (RECONFIGURE == uiOperation) || (UNBIND == uiOperation) );
  112. // AssertSz( FImplies( ((NULL != pmszBindList) && (0 != lstrlenW( pmszBindList ))),
  113. // (RECONFIGURE == uiOperation) &&
  114. // (TDI == uiLayer) &&
  115. // (0 == lstrlenW( pszLower ))),
  116. // "bind order change requires a bind list, no lower, only for TDI, and with Reconfig for the operation" );
  117. // optional strings must be sent as empty strings
  118. //
  119. if (NULL == pszLower)
  120. {
  121. pszLower = c_szEmpty;
  122. }
  123. if (NULL == pmszBindList)
  124. {
  125. pmszBindList = c_szEmpty;
  126. }
  127. // build UNICDOE_STRINGs
  128. SetUnicodeMultiString( &umstrBindList, pmszBindList );
  129. SetUnicodeString( &ustrUpper, pszUpper );
  130. SetUnicodeString( &ustrLower, pszLower );
  131. // Now submit the notification
  132. nRet = NdisHandlePnPEvent( uiLayer,
  133. uiOperation,
  134. &ustrLower,
  135. &ustrUpper,
  136. &umstrBindList,
  137. (PVOID)pvData,
  138. dwSizeData );
  139. if (!nRet)
  140. {
  141. hr = HRESULT_FROM_WIN32(GetLastError());
  142. }
  143. // TraceError( "HrSendNdisHandlePnpEvent", hr );
  144. return( hr );
  145. }
  146. //-------------------------------------------------------------------
  147. // Function: HrSendNdisPnpReconfig
  148. //
  149. // Purpose: Send to Ndis a HandlePnpEvent reconfig notification
  150. //
  151. // Parameters: uiLayer - either NDIS or TDI
  152. // wszUpper - a WIDE string containing the upper component name
  153. // (typically a protocol)
  154. // wszLower - a WIDE string containing the lower component name
  155. // (typically an adapter bindname) The values NULL and
  156. // c_szEmpty are both supported
  157. // pvData - Pointer to ndis component notification data. Content
  158. // determined by each component.
  159. // dwSizeData - Count of bytes in pvData
  160. //
  161. // Returns: HRESULT S_OK on success, HrFromLastWin32Error otherwise
  162. //
  163. //-------------------------------------------------------------------
  164. HRESULT
  165. HrSendNdisPnpReconfig (
  166. UINT uiLayer,
  167. LPCWSTR wszUpper,
  168. LPCWSTR wszLower,
  169. PVOID pvData,
  170. DWORD dwSizeData)
  171. {
  172. Assert(NULL != wszUpper);
  173. Assert((NDIS == uiLayer)||(TDI == uiLayer));
  174. if (NULL == wszLower)
  175. {
  176. wszLower = c_szEmpty;
  177. }
  178. WCHAR* pLower = NULL;
  179. // tstring strLower;
  180. // If a lower component is specified, prefix with "\Device\" else
  181. // strLower's default of an empty string will be used.
  182. if ( wszLower && lstrlenW(wszLower))
  183. {
  184. pLower = (WCHAR*)_alloca((lstrlenW(wszLower) + lstrlenW(c_szDevice) + 2) * sizeof(WCHAR));
  185. wcscpy(pLower, c_szDevice);
  186. wcscat(pLower, wszLower);
  187. }
  188. HRESULT hr = HrSendNdisHandlePnpEvent( uiLayer,
  189. RECONFIGURE,
  190. wszUpper,
  191. pLower,
  192. c_szEmpty,
  193. pvData,
  194. dwSizeData);
  195. // TraceError( "HrSendNdisPnpReconfig", hr);
  196. return hr;
  197. }