alId);int GetPartType(IntPtr pPartType);int GetSubType(ref Guid pSubType);int GetControlInterfaceCount(ref uint pCount);int GetControlInterface(uint nIndex, ref IntPtr ppInterfaceDesc);int EnumPartsIncoming(ref IntPtr ppParts);int EnumPartsOutgoing(ref IntPtr ppParts);int GetTopologyObject(ref IntPtr ppTopology);int Activate(int dwClsContext, ref Guid refiid, ref IntPtr ppvObject);int RegisterControlChangeCallback(ref Guid riid, IntPtr pNotify);int UnregisterControlChangeCallback(IntPtr pNotify);}[Guid("4509F757-2D46-4637-8E62-CE7DB944F57B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]internal interface IKsJackDescription{int GetJackCount(ref uint pcJacks);int GetJackDescription(uint nJack, out KSJACK_DESCRIPTION pDescription);}[StructLayout(LayoutKind.Sequential)]internal struct KSJACK_DESCRIPTION{internal int ChannelMapping;internal int Color;internal uint ConnectionType;internal uint GeoLocation;internal uint GenLocation;internal uint PortConnection;internal bool IsConnected;}private struct PropertyKey{public PropertyKey(Guid initId, uint initPid){id = initId;pid = initPid;}Guid id;uint pid;}private static bool comInitialized;#endregion/// /// Constructor, initialize com interfaces/// internal EndpointCtrl(){int result = 0;//1.to obtain IMMDeviceEnumeratorresult = InitializedInstance();if (result != 0){throw new Win32Exception("Can't obtain IMMDeviceEnumerator: " + result);}//2.to obtain IMMDevice by IMMDeviceEnumerator//result = GetAudioDevice(deviceId);if (result != 0){throw new Win32Exception("Can't obtain IMMDevice: " + result);}}/// /// initialize com interfaces and get IMMDeviceEnumerator/// /// private static int InitializedInstance(){if (comInitialized == true){return 0;}const uint CLSCTX_INPROC_SERVER = 1;//1.to obtain IMMDeviceEnumeratorint result = NativeMethods.CoCreateInstance(ref CLSID_MMDeviceEnumerator, null, CLSCTX_INPROC_SERVER, ref IID_IUnknown, out oEnumerator);if (result != 0){return result;}comInitialized = true;if (oEnumerator == null){return E_FAIL;}iDeviceEnumerator = oEnumerator as IMMDeviceEnumerator;if (iDeviceEnumerator == null){return E_FAIL;}return result;}/// /// Jack Info method/// /// /// private static int GetJackInfo(IMMDevice iDevice){IDeviceTopology deviceTopology = null;IConnector connFrom = null;IConnector connTo = null;IPart part = null;IKsJackDescription jackDesc = null;IntPtr tempIntPtr = IntPtr.Zero;IntPtr pPart = IntPtr.Zero;object tempObject = null;uint nJacks = 0;const uint CLSCTX_ALL = 23;int result = 0;int jackInfo = -1;if (iDevice == null){throw new Exception("No device is specified");}result = iDevice.Activate(ref IID_IDeviceTopology, CLSCTX_ALL, IntPtr.Zero, ref tempIntPtr);if (result != 0 || tempIntPtr == IntPtr.Zero){return jackErrorInfo;}tempObject = Marshal.GetObjectForIUnknown(tempIntPtr);deviceTopology = tempObject as IDeviceTopology;tempIntPtr = IntPtr.Zero;tempObject = null;result = deviceTopology.GetConnector(0, ref tempIntPtr);if (result != 0 || tempIntPtr == IntPtr.Zero){return jackErrorInfo;}tempObject = Marshal.GetObjectForIUnknown(tempIntPtr);connFrom = tempObject as IConnector;tempIntPtr = IntPtr.Zero;tempObject = null;result = connFrom.GetConnectedTo(ref tempIntPtr);if (result != 0 || tempIntPtr == IntPtr.Zero){return jackErrorInfo;}tempObject = Marshal.GetObjectForIUnknown(tempIntPtr);connTo = tempObject as IConnector;tempObject = null;result = Marshal.QueryInterface(tempIntPtr, ref IID_IPart, out pPart);if (result != 0 || pPart == IntPtr.Zero){return jackErrorInfo;}tempObject = Marshal.GetObjectForIUnknown(pPart);part = tempObject as IPart;tempIntPtr = IntPtr.Zero;tempObject = null;try{result = part.Activate(1, ref IID_IKsJackDescription, ref tempIntPtr);}catch{return jackErrorInfo;}if (result != 0 || tempIntPtr == IntPtr.Zero){return jackErrorInfo;}tempObject = Marshal.GetObjectForIUnknown(tempIntPtr);jackDesc = tempObject as IKsJackDescription;result = jackDesc.GetJackCount(ref nJacks);for (uint iJack = 0; iJack < nJacks; iJack++){KSJACK_DESCRIPTION JackDescription = new KSJACK_DESCRIPTION();result = jackDesc.GetJackDescription(iJack, out JackDescription);if (result != 0){return jackErrorInfo;}jackInfo = (int)JackDescription.GeoLocation;}return jackInfo;}/// /// Get expected Audio Device/// /// int GetAudioDevice(string deviceId){IntPtr pDevice = IntPtr.Zero;int result = 0;if (iDeviceEnumerator == null)return E_FAIL;result = iDeviceEnumerator.GetDevice(deviceId, ref pDevice);if (result != 0){throw new Win32Exception("Can't obtain IMMDevice: " + result);}oDevice = Marshal.GetObjectForIUnknown(pDevice);iDevice = oDevice as IMMDevice;iDevice.GetState(ref state);return result;}/// /// Get default Audio Device/// /// [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]static string GetDefaultAudioDeviceId(string type, ERole role){IntPtr pDefaultDevice = IntPtr.Zero;IntPtr pID = IntPtr.Zero;string id = "";int result = 0;object oDefaultDevice;IMMDevice iDefaultDevice;EDataFlow enumDataFlow = EDataFlow.ECapture;if (iDeviceEnumerator == null)return "";if (type == "Render"){enumDataFlow = EDataFlow.ERender;}try{result = iDeviceEnumerator.GetDefaultAudioEndpoint(enumDataFlow, role, ref pDefaultDevice);if (result != 0){throw new Win32Exception("Can't obtain IMMDevice: " + result);}oDefaultDevice = Marshal.GetObjectForIUnknown(pDefaultDevice);iDefaultDevice = oDefaultDevice as IMMDevice;if (iDefaultDevice != null){iDefaultDevice.GetId(ref pID);}id = Marshal.PtrToStringAuto(pID);//return id;}catch (Exception){id = "error";}return id;}[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]internal class MMDeviceEnumerator{}/// /// Call this method to release all com objetcs/// [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]public virtual void Dispose(){if (iDevice != null){Marshal.ReleaseComObject(iDevice);iDevice = null;}if (oDevice != null){Marshal.ReleaseComObject(oDevice);oDevice = null;}if (iDeviceEnumerator != null){Marshal.ReleaseComObject(iDeviceEnumerator);iDeviceEnumerator = null;}if (oEnumerator != null){Marshal.ReleaseComObject(oEnumerator);oEnumerator = null;}}#region Public properties/// /// Get the default device state./// int state;public int State{get{return state;}}[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]public string EndpointId{get{IntPtr id = IntPtr.Zero;if (iDevice != null){iDevice.GetId(ref id);}return Marshal.PtrToStringAuto(id);}}#endregion#region Public Methodpublic string GetDefaultSpeakerEndpoint(){return GetDefaultAudioDeviceId("speakers", ERole.EConsole);}public EndpointList[] GetOutputDevicesByType(EDataFlow DataType){List list = new List();IntPtr pDevices = IntPtr.Zero;IntPtr pDevice = IntPtr.Zero;IntPtr ID = IntPtr.Zero;string deviceName = "";string adapterName = "";string id = "";int state = 0;int result = 0;bool bIsDefault = false;string DatatypeString = "Render";if (DataType == EDataFlow.ECapture){DatatypeString = "Capture";}result = InitializedInstance();if (result != 0 || iDeviceEnumerator == null){throw new Win32Exception("Can't obtain IMMDeviceEnumerator: " + result);}//2. Get audio device with certain typeif (iDeviceEnumerator == null)return null;result = iDeviceEnumerator.EnumAudioEndpoints(DataType, (int)DeviceState.All, ref pDevices);if (result != 0){return null;}if (pDevices == IntPtr.Zero)return null;oDevices = Marshal.GetObjectForIUnknown(pDevices);iDeviceCollection = oDevices as IMMDevices;uint deviceCount = 0;result = iDeviceCollection.GetCount(ref deviceCount);if (result != 0){return null;}PROPVARIANT deviceProperty = new PROPVARIANT();for (uint i = 0; i < deviceCount; i++){// reset the isDefault bool value //bIsDefault = false;result = iDeviceCollection.Item(i, ref pDevice);if (result != 0 || pDevice == null){return null;}oDevice = Marshal.GetObjectForIUnknown(pDevice);iDevice = oDevice as IMMDevice;result = iDevice.GetState(ref state);if (result != 0){continue;}if ((state & (int)DeviceState.NotPresent) != 0) { continue; }IntPtr pProperty = IntPtr.Zero;result = iDevice.OpenPropertyStore(STGM_READ, ref pProperty);if (result != 0){continue;}oProperty = Marshal.GetObjectForIUnknown(pProperty);iProperty = oProperty as IPropertyStore;result = iProperty.GetValue(ref PKEY_AudioEndpoint_FormFactor, ref deviceProperty);if (result != 0){continue;}result = iProperty.GetValue(ref PKEY_Device_DeviceDesc, ref deviceProperty);if (result != 0){continue;}deviceName = deviceProperty.DeviceName;result = iProperty.GetValue(ref PKEY_DeviceInterface_FriendlyName, ref deviceProperty);if (result != 0){continue;}adapterName = deviceProperty.DeviceName;// Get the endpointIDiDevice.GetId(ref ID);if (ID == IntPtr.Zero){continue;}id = Marshal.PtrToStringAuto(ID);// Jack Infoint jackInfo = -1;try{jackInfo = GetJackInfo(iDevice);}catch{jackInfo = jackErrorInfo;}// Get default Idstring defaultID = GetDefaultAudioDeviceId(DatatypeString, ERole.EConsole);// Match the defaultID with the current IDif ((defaultID != "error") && (defaultID == id)){bIsDefault = true;}else { bIsDefault = false; }list.Add(new EndpointList(deviceName, id, adapterName, jackInfo, bIsDefault,state));}return list.ToArray();}#endregion}[StructLayout(LayoutKind.Sequential)]public class WAVEFORMATEX{public UInt16 wFormatTag;public UInt16 nChannels;public UInt32 nSamplesPerSec;public UInt32 nAvgBytesPerSec;public UInt16 nBlockAlign;public UInt16 wBitsPerSample;public UInt16 cbSize;}[ComImport, Guid("870AF99C-171D-4f9e-AF0D-E63DF40C2BC9")]public class IPolicyConfigClass { }public static class LoadDLL{[DllImport(@"env:windir\diagnostics\system\Audio\AudioDiagnosticUtil.dll", EntryPoint = "AudioSetDeviceFormat", CharSet = CharSet.Auto)]public static extern int AudioSetDeviceFormat(string pszDeviceName, IntPtr pEndpointFormat, IntPtr MixFormat);}[Guid("6be54be8-a068-4875-a49d-0c2966473b11"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]public interface IPolicyConfig{int GetMixFormat(string pszDeviceName, ref IntPtr ppFormat);int GetDeviceFormat(string pszDeviceName, bool bDefault, ref IntPtr ppFormat);int ResetDeviceFormat(string pszDeviceName);int SetDeviceFormat(string pszDeviceName, IntPtr pEndpointFormat, IntPtr MixFormat);int GetProcessingPeriod(string pszDeviceName, bool bDefault, IntPtr pmftDefaultPeriod, IntPtr pmftMinimumPeriod);int SetProcessingPeriod(string pszDeviceName, IntPtr pmftPeriod);int GetShareMode(string pszDeviceName, IntPtr pMode);int SetShareMode(string pszDeviceName, IntPtr mode);int GetPropertyValue(string pszDeviceName, bool bFxStore, IntPtr key, IntPtr pv);int SetPropertyValue(string pszDeviceName, bool bFxStore, IntPtr key, IntPtr pv);int SetDefaultEndpoint(string pszDeviceName, ERole role);int SetEndpointVisibility(string pszDeviceName, bool bVisible);int SetScreenReaderState(bool bEnabled);int SetEndpointAbilityToBeDefault(string pszDeviceName, bool bEnable);int SetApplicationDefaultEndpoint(Int32 processId, ERole role, string pszInterfaceId);int ClearApplicationDefaultEndpoint(Int32 processId, ERole role, EDataFlow flow);int RegisterProxyAudioProcess();int SetDuckingGainForId(string pszStr1, float floatVal);int SetLayoutGainForId(Int32 something1, float floatVal);}public class IPolicyConfigHelper{public Byte[] GetDeviceFormat(string pszDeviceName, bool bDefault, out WAVEFORMATEX retFormat){IntPtr ptrToWaveFormat = new IntPtr();LoadDLL.AudioGetDeviceFormat(pszDeviceName, bDefault, ref ptrToWaveFormat);WAVEFORMATEX format = (WAVEFORMATEX)Marshal.PtrToStructure(ptrToWaveFormat, typeof(WAVEFORMATEX));int formatSize = format.cbSize + Marshal.SizeOf(typeof(WAVEFORMATEX));Byte[] formatExtData = new Byte[format.cbSize + Marshal.SizeOf(typeof(WAVEFORMATEX))];Marshal.Copy(ptrToWaveFormat, formatExtData, 0, formatSize);retFormat = format;return formatExtData;}public int SetDeviceFormat(string pszDeviceName, Byte[] formatExtData, int formatSize){IntPtr ptrToFormat = Marshal.AllocHGlobal(formatSize);Marshal.Copy(formatExtData, 0, ptrToFormat, formatSize);int rval = LoadDLL.AudioSetDeviceFormat(pszDeviceName, ptrToFormat, IntPtr.Zero);Marshal.FreeHGlobal(ptrToFormat);return rval;}}public static bool verifySamplingRate(string deviceID){EndpointCtrl control = new EndpointCtrl();bool result = false;string defSpeakerEndpoint = deviceID;IPolicyConfigHelper policyHelper = new IPolicyConfigHelper();WAVEFORMATEX defaultFormatEx = new WAVEFORMATEX();WAVEFORMATEX currentFormatEx = new WAVEFORMATEX();Byte[] defaultFormat = policyHelper.GetDeviceFormat(defSpeakerEndpoint, true, out defaultFormatEx);Byte[] currentFormat = policyHelper.GetDeviceFormat(defSpeakerEndpoint, false, out currentFormatEx);if (defaultFormatEx.nAvgBytesPerSec != currentFormatEx.nAvgBytesPerSec){result = true;}return result;}public static bool resetSamplingRate(string deviceID){EndpointCtrl control = new EndpointCtrl();string defSpeakerEndpoint = deviceID;IPolicyConfigHelper policyHelper = new IPolicyConfigHelper();WAVEFORMATEX defaultFormatEx = new WAVEFORMATEX();WAVEFORMATEX currentFormatEx = new WAVEFORMATEX();Byte[] defaultFormat = policyHelper.GetDeviceFormat(defSpeakerEndpoint, true, out defaultFormatEx);Byte[] currentFormat = policyHelper.GetDeviceFormat(defSpeakerEndpoint, false, out currentFormatEx);policyHelper.SetDeviceFormat(defSpeakerEndpoint, defaultFormat, defaultFormat.Length);WAVEFORMATEX currentFormatEx2 = new WAVEFORMATEX();Byte[] currentFormat2 = policyHelper.GetDeviceFormat(defSpeakerEndpoint, false, out currentFormatEx2);control.Dispose();return true;}public static EndpointList[] GetAudioEndPointsbyType(string type){EDataFlow dataflowtype = new EDataFlow();if (type == "Render"){dataflowtype = EDataFlow.ERender;}else { dataflowtype = EDataFlow.ECapture; }EndpointCtrl control = new EndpointCtrl();EndpointList[] list = control.GetOutputDevicesByType(dataflowtype);return list;}}}"@# Initializing LoadLibrary, LoadStringW, and FreeLibrary helper functionsAdd-Type -TypeDefinition @"namespace Microsoft.Windows.Diagnosis.Strings{using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;internal class NativeMethods{[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]public static extern int LoadStringW(IntPtr hInst, int id, StringBuilder buf, int buflen);[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]public static extern IntPtr LoadLibrary(string path);[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]public static extern bool FreeLibrary(IntPtr hInst);}public class PublicMethods{public static string GetString(string FileName, int ResourceId){string path = FileName;string value = ""; что делает этот скрипт