/// <summary> /// Scans a filter's pins looking for a pin with the specified connection status /// </summary> /// <param name="vSource">The filter to scan</param> /// <param name="vStat">The status to find (connected/unconnected)</param> /// <param name="iIndex">Zero based index (ie 2 will return the third pin with the specified status)</param> /// <returns>The matching pin, or null if not found</returns> public static IPin ByConnectionStatus(IBaseFilter vSource, PinConnectedStatus vStat, int iIndex) { int hr; IEnumPins ppEnum; IPin pRet = null; IPin pOutPin; IPin[] pPins = new IPin[1]; if (vSource == null) { return(null); } // Get the pin enumerator hr = vSource.EnumPins(out ppEnum); DsError.ThrowExceptionForHR(hr); try { // Walk the pins looking for a match int fetched; while (ppEnum.Next(1, pPins, out fetched) >= 0 && fetched == 1) { // Read the connected status hr = pPins[0].ConnectedTo(out pOutPin); // Check for VFW_E_NOT_CONNECTED. Anything else is bad. if (hr != DsResults.E_NotConnected) { DsError.ThrowExceptionForHR(hr); // The ConnectedTo call succeeded, release the interface DsUtils.ReleaseComObject(pOutPin); } // Is it the right status? if ((hr == 0 && vStat == PinConnectedStatus.Connected) || (hr == DsResults.E_NotConnected && vStat == PinConnectedStatus.Unconnected)) { // Is is the right index? if (iIndex == 0) { pRet = pPins[0]; break; } iIndex--; } DsUtils.ReleaseComObject(pPins[0]); } } finally { DsUtils.ReleaseComObject(ppEnum); } return(pRet); }
public static IPin ByConnectionStatus(IBaseFilter vSource, PinConnectedStatus vStat, int iIndex) { IEnumPins pins; IPin[] ppPins = new IPin[1]; if (vSource == null) { return(null); } DsError.ThrowExceptionForHR(vSource.EnumPins(out pins)); try { while (pins.Next(1, ppPins, IntPtr.Zero) == 0) { IPin pin2; int hr = ppPins[0].ConnectedTo(out pin2); if (hr != -2147220983) { DsError.ThrowExceptionForHR(hr); Marshal.ReleaseComObject(pin2); } if (((hr == 0) && (vStat == PinConnectedStatus.Connected)) || ((hr == -2147220983) && (vStat == PinConnectedStatus.Unconnected))) { if (iIndex == 0) { return(ppPins[0]); } iIndex--; } Marshal.ReleaseComObject(ppPins[0]); } } finally { Marshal.ReleaseComObject(pins); } return(null); }