Ejemplo n.º 1
0
// <HostBuilderImplementation>
HRESULT CreateFileServiceHost(
                LPCWSTR pszDeviceAddress,
                const WSD_THIS_DEVICE_METADATA* pThisDeviceMetadata,
                IFileService* pIFileService,
                IWSDDeviceHost** ppHostOut,
                IWSDXMLContext** ppContextOut)
{
    HRESULT hr = S_OK;
    IWSDXMLContext* pContext = NULL;
    IWSDDeviceHost* pHost = NULL;

    // 
    // Validate parameters
    // 
    if( NULL == pszDeviceAddress )
    {
        return E_INVALIDARG;
    }

    if( NULL == pThisDeviceMetadata )
    {
        return E_INVALIDARG;
    }

    // pIFileService is optional
    if( NULL == ppHostOut )
    {
        return E_POINTER;
    }

    // ppContextOut is optional

    *ppHostOut = NULL;
    if( NULL != ppContextOut )
    {
        *ppContextOut = NULL;
    }

    // 
    // Create XML context for namespace and type registration
    // 
    hr = WSDXMLCreateContext(&pContext);

    // 
    // Register types used by the service
    // 
    if( S_OK == hr )
    {
        hr = FileServiceRegisterTypes(pContext);
    }

    // 
    // Register namespaces used by the service
    // 
    if( S_OK == hr )
    {
        hr = FileServiceRegisterNamespaces(pContext);
    }

    // 
    // Create device host
    // 
    if( S_OK == hr )
    {
        hr = WSDCreateDeviceHost(pszDeviceAddress, pContext, &pHost);
    }

    // 
    // Register port types
    // 
    if( S_OK == hr )
    {
        hr = pHost->RegisterPortType(&PortType_FileService);
    }

    // 
    // Set metadata
    // 
    if( S_OK == hr )
    {
        hr = pHost->SetMetadata(&thisModelMetadata, pThisDeviceMetadata, &hostMetadata, NULL);
    }

    // 
    // Register services and set discoverability
    // 
    if( S_OK == hr && pIFileService != NULL )
    {
        hr = pHost->RegisterService(L"http://www.example.com/ncd/FileService/FileService0", pIFileService);
    }

    // 
    // Cleanup
    // 
    if( S_OK == hr && ppContextOut )
    {
        *ppContextOut = pContext;
    }
    else
    {
        if( NULL != pContext )
        {
            pContext->Release();
        }
    }

    if( S_OK == hr )
    {
        *ppHostOut = pHost;
    }
    else
    {
        if( NULL != pHost )
        {
            pHost->Release();
        }
    }

    return hr;
}
// <ProxyBuilderImplementations>
HRESULT CreateCFileServiceProxy(
                LPCWSTR pszDeviceAddress,
                LPCWSTR pszLocalAddress,
                CFileServiceProxy** ppProxyOut,
                IWSDXMLContext** ppContextOut)
{
    HRESULT hr = S_OK;
    IWSDXMLContext* pContext = NULL;
    IWSDDeviceProxy* pDeviceProxy = NULL;
    IWSDServiceProxy* pServiceProxy = NULL;
    CFileServiceProxy* pProxy = NULL;

    // 
    // Validate parameters
    // 
    if( NULL == pszDeviceAddress )
    {
        return E_INVALIDARG;
    }

    if( NULL == pszLocalAddress )
    {
        return E_INVALIDARG;
    }

    if( NULL == ppProxyOut )
    {
        return E_POINTER;
    }

    // ppContextOut is optional

    *ppProxyOut = NULL;
    if( NULL != ppContextOut )
    {
        *ppContextOut = NULL;
    }

    // 
    // Create XML context for namespace and type registration
    // 
    hr = WSDXMLCreateContext(&pContext);

    // 
    // Register types used by the service
    // 
    if( S_OK == hr )
    {
        hr = FileServiceRegisterTypes(pContext);
    }

    // 
    // Register namespaces used by the service
    // 
    if( S_OK == hr )
    {
        hr = FileServiceRegisterNamespaces(pContext);
    }

    // 
    // Create a proxy for the device host
    // 
    if( S_OK == hr )
    {
        hr = WSDCreateDeviceProxy(pszDeviceAddress, pszLocalAddress, pContext, &pDeviceProxy);
    }

    // 
    // Create a proxy for the service
    // 
    if( S_OK == hr )
    {
        hr = pDeviceProxy->GetServiceProxyByType(&Names_FileService[5], &pServiceProxy);
    }

    // 
    // Create a proxy for the port type
    // 
    if( S_OK == hr )
    {
        pProxy = new CFileServiceProxy();
        if( NULL == pProxy )
        {
            hr = E_OUTOFMEMORY;
        }
    }

    if( S_OK == hr )
    {
        hr = pProxy->Init(pServiceProxy);
    }

    // 
    // Cleanup
    // 
    if( NULL != pServiceProxy )
    {
        pServiceProxy->Release();
    }

    if( NULL != pDeviceProxy )
    {
        pDeviceProxy->Release();
    }

    if( S_OK == hr && ppContextOut )
    {
        *ppContextOut = pContext;
    }
    else
    {
        if( NULL != pContext )
        {
            pContext->Release();
        }
    }

    if( S_OK == hr )
    {
        *ppProxyOut = pProxy;
    }
    else
    {
        if( NULL != pProxy )
        {
            pProxy->Release();
        }
    }

    return hr;
}