1

I have tried with WSDDeviceProxy & in that particularly GetMetadata, i am getting all printer information except the IP Address. in one site i have read that IP can be get through IWSDDiscoveredDevice but How create IWSDDiscoveredDevice ?

Ganesh
  • 89
  • 3
  • 14

1 Answers1

0

Some time ago I faced a similar issue: find the transport address of the installed WSD printing queue. The good point to start from is to read the specification: http://docs.oasis-open.org/ws-dd/discovery/1.1/wsdd-discovery-1.1-spec.pdf.

If you deal with the installed WSD printer queue, you have printer UUID and may simply send a Resolve request and listen for the ResolveMatch response. The simplest practical way is:

  1. Get the printer queue port name via EnumPrinters (I used level 5)
  2. Get the port monitor via EnumPorts
  3. Get the "Printer UUID" from the registry path

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\WSD Port\Ports<YOUR_PORT_NAME>

  1. Send UDP packet to address 239.255.255.250 port 3702 with utf-8 encoded data

            <soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" 
              xmlns:wsa=""http://schemas.xmlsoap.org/ws/2004/08/addressing""
              xmlns:wsd=""http://schemas.xmlsoap.org/ws/2005/04/discovery"">
                <soap:Header>
                    <wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>
                    <wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/Resolve</wsa:Action>
                    <wsa:MessageID>{0}</wsa:MessageID>
                </soap:Header>
                <soap:Body>
                    <wsd:Resolve>
                        <wsa:EndpointReference>
                            <wsa:Address>{1}</wsa:Address>
                        </wsa:EndpointReference>
                    </wsd:Resolve>
                </soap:Body>
            </soap:Envelope>"
    

where messageId is urn:uuid:<GENERATED_GUID> and address is urn:uuid:<PRINTER_UUID>

  1. Receive UDP unicast response

  2. Tranport addresses is stored inside <wsd:XAddrs>

Example ResolveMatch:

 <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:wsd="http://schemas.xmlsoap.org/ws/2005/04/discovery" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsdp="http://schemas.xmlsoap.org/ws/2006/02/devprof" xmlns:wsvc0="http://schemas.microsoft.com/windows/2006/08/wdp/scan" xmlns:wsvc1="http://schemas.microsoft.com/windows/2006/08/wdp/print">
    <soap:Header>
        <wsa:Action>http://schemas.xmlsoap.org/ws/2005/04/discovery/ResolveMatches</wsa:Action>
        <wsa:MessageID>urn:uuid:d8a491ad-0d87-4429-b900-84f102a91097</wsa:MessageID>
        <wsa:RelatesTo>urn:uuid:8fc371bb-edfb-49a4-b631-7e5264e25c20</wsa:RelatesTo>
        <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
        <wsd:AppSequence InstanceId="1529651446" MessageNumber="1790"/>
    </soap:Header>
    <soap:Body>
        <wsd:ResolveMatches>
            <wsd:ResolveMatch>
                <wsa:EndpointReference>
                    <wsa:Address>urn:uuid:edec3c87-cfbc-4b02-bcb8-077be723bd32</wsa:Address>
                </wsa:EndpointReference>
                <wsd:XAddrs>http://192.168.1.101:65001 http://[fe80::221:b7ff:fe19:8a4a]:65001</wsd:XAddrs>
                <wsd:MetadataVersion>168</wsd:MetadataVersion>
            </wsd:ResolveMatch>
        </wsd:ResolveMatches>
    </soap:Body>
</soap:Envelope>

If you don't deal with the installed printing queue, you have to perform a Probe request, listen to ProbeMatch and only then go for Resolve / ResolveMatch.

I ended up with fully managed minimal solution in 200 lines. In case you are looking for WSDAAPI have a look at the below sample (just an idea, surely not production ready):

WSDiscoveryProviderNotifier.cpp:

#include <Wsdapi.h>
#include <Wsddisco.h>
#include "guid_helper.h"
import std.core;

constexpr const IID IID_WSDiscoveryProviderNotifier = guid_parse::make_guid("{EFECF0A1-399E-40B8-A13C-ACE28DB40212}");

class WSDiscoveryProviderNotifier : public IWSDiscoveryProviderNotify {
private:
    LONG referenceCount = 0;
public:
    /* IUnknown */
    HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID *ppvObj) override {
        // Always set out parameter to NULL, validating it first.
        if (!ppvObj)
            return E_INVALIDARG;
        *ppvObj = nullptr;
        if (riid == IID_IUnknown || riid == IID_WSDiscoveryProviderNotifier) {
            // Increment the reference count and return the pointer.
            *ppvObj = (LPVOID) this;
            AddRef();
            return NOERROR;
        }
        return E_NOINTERFACE;
    }

    ULONG STDMETHODCALLTYPE AddRef() override {
        InterlockedIncrement(&referenceCount);
        return referenceCount;
    };

    ULONG STDMETHODCALLTYPE Release() override {
        ULONG ulRefCount = InterlockedDecrement(&referenceCount);
        if (0 == ulRefCount)
        {
            delete this;
        }
        return ulRefCount;
    };

    /* IWSDiscoveryProviderNotify */
    HRESULT STDMETHODCALLTYPE SearchFailed(
            /* [in] */ HRESULT hr,
            /* [annotation][optional][in] */
                       _In_opt_  LPCWSTR pszTag) override {
        std::wcout << "Search failed" << std::endl;
        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE SearchComplete(
            /* [annotation][optional][in] */
            _In_opt_  LPCWSTR pszTag) override {
        std::wcout << "Search completed" << std::endl;;
        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE Add (/* [in] */ IWSDiscoveredService *pService) override {
        WSD_URI_LIST *uriList;
        pService->GetXAddrs(&uriList);
        WSD_URI_LIST *currentElement = uriList;
        do {
            std::wcout << currentElement->Element << std::endl;
            currentElement = currentElement->Next;
        }
        while (currentElement != nullptr);
        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE Remove(/* [in] */ IWSDiscoveredService *pService) override {
        return S_OK;
    };
};

main.cpp:

#include <Wsdxml.h>
#include "WSDiscoveryProviderNotifier.cpp"

void ResolveWSDDeviceById(LPCWSTR deviceId) {
    HRESULT hr = S_OK;
    IWSDXMLContext *context = nullptr;
    IWSDiscoveryProvider *discoveryProvider = nullptr;
    IWSDiscoveryProviderNotify *providerNotifier = new WSDiscoveryProviderNotifier();

    hr = WSDCreateDiscoveryProvider(context, &discoveryProvider);
    if (S_OK == hr) {
        std::wcout << "WSDCreateDiscoveryProvider reported success" << std::endl;
        discoveryProvider->Attach(providerNotifier);
        discoveryProvider->SearchById(deviceId, nullptr);
    } else {
        throw std::exception("Failed to create IWSDiscoveryProvider");
    }
}

int main() {
    std::cout << "Hello, World!" << std::endl;
    ResolveWSDDeviceById(L"urn:uuid:edec3c87-cfbc-4b02-bcb8-077be723bd32");
    std::string _;
    std::getline (std::cin,_);
    return 0;
}

Hope this helps somebody.

metacube
  • 328
  • 3
  • 14