Compartilhar via


Find an Unconnected Pin on a Filter

 
Microsoft DirectShow 9.0

Find an Unconnected Pin on a Filter

The following function searches on a filter for an unconnected pin, either input or output. It returns the first matching pin. Finding unconnected pins is useful when you are connecting filters.

HRESULT GetUnconnectedPin(
    IBaseFilter *pFilter,   // Pointer to the filter.
    PIN_DIRECTION PinDir,   // Direction of the pin to find.
    IPin **ppPin)           // Receives a pointer to the pin.
{
    *ppPin = 0;
    IEnumPins *pEnum = 0;
    IPin *pPin = 0;
    HRESULT hr = pFilter->EnumPins(&pEnum);
    if (FAILED(hr))
    {
        return hr;
    }
    while (pEnum->Next(1, &pPin, NULL) == S_OK)
    {
        PIN_DIRECTION ThisPinDir;
        pPin->QueryDirection(&ThisPinDir);
        if (ThisPinDir == PinDir)
        {
            IPin *pTmp = 0;
            hr = pPin->ConnectedTo(&pTmp);
            if (SUCCEEDED(hr))  // Already connected, not the pin we want.
            {
                pTmp->Release();
            }
            else  // Unconnected, this is the pin we want.
            {
                pEnum->Release();
                *ppPin = pPin;
                return S_OK;
            }
        }
        pPin->Release();
    }
    pEnum->Release();
    // Did not find a matching pin.
    return E_FAIL;
}

The function uses the IEnumPins interface to loop through the pins on the filter. For each pin, it calls IPin::QueryDirection to determine the direction of the pin. If the direction matches, it calls the IPin::ConnectedTo method to test whether the pin is already connected to another pin.

The following code example uses this function to find an unconnected output pin on a filter:

IPin *pOut = NULL;
HRESULT hr = GetUnconnectedPin(pFilter, PINDIR_OUTPUT, &pOut);
if (SUCCEEDED(hr))
{
    /* ... */
    pOut->Release();
}

See Also