#include <atlbase.h>
#include <windows.h>
#include <dshow.h>
#include <stdio.h>

HRESULT GetPin(IBaseFilter *pFilter, PIN_DIRECTION dirrequired, int iNum, IPin **ppPin)
{
    CComPtr< IEnumPins > pEnum;
    *ppPin = NULL;
    HRESULT hr = pFilter->EnumPins(&pEnum);
    if(FAILED(hr))
        return hr;
    ULONG ulFound;
    IPin *pPin;
    hr = E_FAIL;
    while(S_OK == pEnum->Next(1, &pPin, &ulFound)) {
        PIN_DIRECTION pindir = (PIN_DIRECTION) 3;
        pPin->QueryDirection(&pindir);
        if(pindir == dirrequired) {
            if(iNum == 0) {
                *ppPin = pPin;
                break;
            }
            iNum--;
        }
        pPin->Release();
    }

    return hr;
}

IPin *GetInPin(IBaseFilter *pFilter, int Num)
{
    CComPtr< IPin > pComPin;
    GetPin(pFilter, PINDIR_INPUT, Num, &pComPin);
    return pComPin;
}

IPin *GetOutPin(IBaseFilter *pFilter, int Num)
{
    CComPtr< IPin > pComPin;
    GetPin(pFilter, PINDIR_OUTPUT, Num, &pComPin);
    return pComPin;
}
