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.

179 lines
4.5 KiB

  1. // MediaState.cpp : Implementation of CMediaState
  2. //
  3. //
  4. //
  5. //
  6. #include "stdafx.h"
  7. #include "MSSANic.h"
  8. #include "MediaState.h"
  9. #include "lm.h"
  10. #include "subauth.h"
  11. #include "ndispnp.h"
  12. VOID RtlInitUnicodeString( PUNICODE_STRING DestinationString, PCWSTR SourceString OPTIONAL );
  13. #define DEVICE_PREFIX_W L"\\Device\\"
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CMediaState
  16. STDMETHODIMP CMediaState::InterfaceSupportsErrorInfo(REFIID riid)
  17. {
  18. static const IID* arr[] =
  19. {
  20. &IID_IMediaState
  21. };
  22. for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
  23. {
  24. if (InlineIsEqualGUID(*arr[i],riid))
  25. return S_OK;
  26. }
  27. return S_FALSE;
  28. }
  29. //////////////////////////////////////////////////////////////////////////////
  30. //
  31. // CMediaState::IsConnected
  32. //
  33. // Description:
  34. //
  35. // Arguments:
  36. // [in] bstrGUID The Device GUID
  37. //
  38. // Returns:
  39. // TRUE if the device is connected
  40. // FALSE if not
  41. //
  42. //
  43. //////////////////////////////////////////////////////////////////////////////
  44. STDMETHODIMP CMediaState::IsConnected(BSTR bstrGUID, VARIANT_BOOL *fConnected)
  45. {
  46. HRESULT hr = S_OK;
  47. try
  48. {
  49. WCHAR Device[512], *pDevice;
  50. ULONG Len;
  51. UNICODE_STRING NdisDevice;
  52. NIC_STATISTICS NdisStats;
  53. *fConnected = VARIANT_FALSE;
  54. //
  55. // First convert LPSTR to LPWSTR
  56. //
  57. if( NULL == bstrGUID )
  58. {
  59. hr = E_FAIL;
  60. throw hr;
  61. }
  62. //
  63. // Format the device path.
  64. //
  65. int cchWritten = _snwprintf(Device,
  66. sizeof(Device) / sizeof(Device[0]),
  67. L"%s%s",
  68. DEVICE_PREFIX_W,
  69. bstrGUID);
  70. if( 0 > cchWritten || sizeof(Device) / sizeof(Device[0]) <= cchWritten )
  71. {
  72. hr = E_FAIL;
  73. throw hr;
  74. }
  75. ZeroMemory( &NdisStats, sizeof(NdisStats) );
  76. NdisStats.Size = sizeof( NdisStats );
  77. RtlInitUnicodeString(&NdisDevice, Device);
  78. //
  79. // NdisQueryStatistics is an undocumented API that returns the status of the device
  80. //
  81. if( FALSE == NdisQueryStatistics(&NdisDevice, &NdisStats) )
  82. {
  83. ULONG Error;
  84. //
  85. // Could not get statistics.. use default answer.
  86. //
  87. Error = GetLastError();
  88. if( ERROR_NOT_READY == Error )
  89. {
  90. *fConnected = VARIANT_FALSE;
  91. hr = S_OK;
  92. throw hr;
  93. }
  94. hr = E_FAIL;
  95. throw hr;;
  96. }
  97. if( NdisStats.MediaState == MEDIA_STATE_DISCONNECTED )
  98. {
  99. *fConnected = VARIANT_FALSE;
  100. } else if( NdisStats.MediaState == MEDIA_STATE_CONNECTED )
  101. {
  102. *fConnected = VARIANT_TRUE;
  103. }
  104. else
  105. {
  106. //
  107. // unknown media state? fail request
  108. //
  109. hr = E_FAIL;
  110. throw hr;
  111. }
  112. }
  113. catch(...)
  114. {
  115. }
  116. return hr;
  117. }
  118. //////////////////////////////////////////////////////////////////////////////
  119. //
  120. //Routine Description:
  121. //
  122. // The RtlInitUnicodeString function initializes an NT counted
  123. // unicode string. The DestinationString is initialized to point to
  124. // the SourceString and the Length and MaximumLength fields of
  125. // DestinationString are initialized to the length of the SourceString,
  126. // which is zero if SourceString is not specified.
  127. //
  128. //Arguments:
  129. //
  130. // DestinationString - Pointer to the counted string to initialize
  131. //
  132. // SourceString - Optional pointer to a null terminated unicode string that
  133. // the counted string is to point to.
  134. //
  135. //
  136. //Return Value:
  137. //
  138. // None.
  139. //
  140. //////////////////////////////////////////////////////////////////////////////
  141. VOID RtlInitUnicodeString( PUNICODE_STRING DestinationString, PCWSTR SourceString OPTIONAL )
  142. {
  143. ULONG Length;
  144. DestinationString->Buffer = (PWSTR)SourceString;
  145. Length = wcslen( SourceString ) * sizeof( WCHAR );
  146. DestinationString->Length = (USHORT)Length;
  147. DestinationString->MaximumLength = (USHORT)(Length + sizeof(UNICODE_NULL));
  148. }