📄 dsutils.cs
字号:
catch
{
}
return s;
}
}
/// <summary>
/// Returns an array of DsDevices of type devcat.
/// </summary>
/// <param name="cat">Any one of FilterCategory</param>
public static DsDevice[] GetDevicesOfCat(Guid FilterCategory)
{
int hr;
// Use arrayList to build the retun list since it is easily resizable
DsDevice[] devret;
ArrayList devs = new ArrayList();
#if USING_NET11
UCOMIEnumMoniker enumMon;
#else
IEnumMoniker enumMon;
#endif
ICreateDevEnum enumDev = (ICreateDevEnum)new CreateDevEnum();
hr = enumDev.CreateClassEnumerator(FilterCategory, out enumMon, 0);
DsError.ThrowExceptionForHR(hr);
// CreateClassEnumerator returns null for enumMon if there are no entries
if (hr != 1)
{
try
{
try
{
#if USING_NET11
UCOMIMoniker[] mon = new UCOMIMoniker[1];
#else
IMoniker[] mon = new IMoniker[1];
#endif
#if USING_NET11
int j;
while ((enumMon.Next(1, mon, out j) == 0))
#else
while ((enumMon.Next(1, mon, IntPtr.Zero) == 0))
#endif
{
try
{
// The devs array now owns this object. Don't
// release it if we are going to be successfully
// returning the devret array
devs.Add(new DsDevice(mon[0]));
}
catch
{
Marshal.ReleaseComObject(mon[0]);
throw;
}
}
}
finally
{
Marshal.ReleaseComObject(enumMon);
}
// Copy the ArrayList to the DsDevice[]
devret = new DsDevice[devs.Count];
devs.CopyTo(devret);
}
catch
{
foreach (DsDevice d in devs)
{
d.Dispose();
}
throw;
}
}
else
{
devret = new DsDevice[0];
}
return devret;
}
/// <summary>
/// Get the FriendlyName for a moniker
/// </summary>
/// <returns>String or null on error</returns>
private string GetFriendlyName()
{
IPropertyBag bag = null;
string ret = null;
object bagObj = null;
object val = null;
try
{
Guid bagId = typeof (IPropertyBag).GUID;
m_Mon.BindToStorage(null, null, ref bagId, out bagObj);
bag = (IPropertyBag) bagObj;
int hr = bag.Read("FriendlyName", out val, null);
DsError.ThrowExceptionForHR(hr);
ret = val as string;
}
catch
{
ret = null;
}
finally
{
bag = null;
if (bagObj != null)
{
Marshal.ReleaseComObject(bagObj);
bagObj = null;
}
}
return ret;
}
public void Dispose()
{
if (Mon != null)
{
Marshal.ReleaseComObject(Mon);
m_Mon = null;
GC.SuppressFinalize(this);
}
m_Name = null;
}
}
sealed public class DsFindPin
{
private DsFindPin()
{
// Prevent people from trying to instantiate this class
}
/// <summary>
/// Scans a filter's pins looking for a pin in the specified direction
/// </summary>
/// <param name="vSource">The filter to scan</param>
/// <param name="vDir">The direction to find</param>
/// <param name="iIndex">Zero based index (ie 2 will return the third pin in the specified direction)</param>
/// <returns>The matching pin, or null if not found</returns>
public static IPin ByDirection(IBaseFilter vSource, PinDirection vDir, int iIndex)
{
int hr;
int lFetched;
IEnumPins ppEnum;
PinDirection ppindir;
IPin pRet = null;
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
while ((ppEnum.Next(1, pPins, out lFetched) >= 0) && (lFetched == 1))
{
// Read the direction
hr = pPins[0].QueryDirection(out ppindir);
DsError.ThrowExceptionForHR(hr);
// Is it the right direction?
if (ppindir == vDir)
{
// Is is the right index?
if (iIndex == 0)
{
pRet = pPins[0];
break;
}
iIndex--;
}
Marshal.ReleaseComObject(pPins[0]);
}
}
finally
{
Marshal.ReleaseComObject(ppEnum);
}
return pRet;
}
/// <summary>
/// Scans a filter's pins looking for a pin with the specified name
/// </summary>
/// <param name="vSource">The filter to scan</param>
/// <param name="vPinName">The pin name to find</param>
/// <returns>The matching pin, or null if not found</returns>
public static IPin ByName(IBaseFilter vSource, string vPinName)
{
int hr;
int lFetched;
IEnumPins ppEnum;
PinInfo ppinfo;
IPin pRet = null;
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
while ((ppEnum.Next(1, pPins, out lFetched) >= 0) && (lFetched == 1))
{
// Read the info
hr = pPins[0].QueryPinInfo(out ppinfo);
DsError.ThrowExceptionForHR(hr);
// Is it the right name?
if (ppinfo.name == vPinName)
{
DsUtils.FreePinInfo(ppinfo);
pRet = pPins[0];
break;
}
Marshal.ReleaseComObject(pPins[0]);
DsUtils.FreePinInfo(ppinfo);
}
}
finally
{
Marshal.ReleaseComObject(ppEnum);
}
return pRet;
}
/// <summary>
/// Scan's a filter's pins looking for a pin with the specified category
/// </summary>
/// <param name="vSource">The filter to scan</param>
/// <param name="guidPinCat">The guid from PinCategory to scan for</param>
/// <param name="iIndex">Zero based index (ie 2 will return the third pin of the specified category)</param>
/// <returns>The matching pin, or null if not found</returns>
public static IPin ByCategory(IBaseFilter vSource, Guid PinCategory, int iIndex)
{
int hr;
int lFetched;
IEnumPins ppEnum;
IPin pRet = null;
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
while ((ppEnum.Next(1, pPins, out lFetched) >= 0) && (lFetched == 1))
{
// Is it the right category?
if (DsUtils.GetPinCategory(pPins[0]) == PinCategory)
{
// Is is the right index?
if (iIndex == 0)
{
pRet = pPins[0];
break;
}
iIndex--;
}
Marshal.ReleaseComObject(pPins[0]);
}
}
finally
{
Marshal.ReleaseComObject(ppEnum);
}
return pRet;
}
/// <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;
int lFetched;
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
while ((ppEnum.Next(1, pPins, out lFetched) >= 0) && (lFetched == 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -