如何找出WSD打印机的IP地址?我想使用 WSD API,但无法使用 IWSDDiscoveredDevice

how to find out IP address of WSD printer ? i want to use WSD APIs, but unable to get using IWSDDiscoveredDevice

本文关键字:WSD API IWSDDiscoveredDevice 打印机 何找出 IP 地址      更新时间:2023-10-16

我尝试过使用WSDDeviceProxy,特别是在GetMetadata中,我正在获取除IP地址之外的所有打印机信息。 在一个站点中,我读到IP可以通过IWSDDiscoveredDevice获得,但是如何创建IWSDDiscoverDevice?

前段时间我遇到了类似的问题:查找已安装的 WSD 打印队列的传输地址。好的起点是阅读规范:http://docs.oasis-open.org/ws-dd/discovery/1.1/wsdd-discovery-1.1-spec.pdf。

如果处理已安装的 WSD 打印机队列,则具有打印机 UUID,并且可能只需发送 Resolve 请求并侦听 ResolveMatch 响应。最简单的实用方法是:

  1. 通过枚举打印机获取打印机队列端口名称(我使用了级别 5)
  2. 通过枚举端口获取端口监视器
  3. 从注册表路径获取"打印机 UUID"

计算机\HKEY_LOCAL_MACHINE\系统\当前控制集\控制\打印\监视器\WSD端口\端口

  1. 使用 utf-8 编码数据将 UDP 数据包发送到地址 239.255.255.250 端口 3702

            <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>"
    

其中 messageId urn:uuid:<GENERATED_GUID>,地址urn:uuid:<PRINTER_UUID>

  1. 接收 UDP 单播响应

  2. 传输地址存储在<wsd:XAddrs>

示例解析匹配:

 <?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>

如果您不处理已安装的打印队列,则必须执行 Probe 请求,侦听 ProbeMatch,然后才进行 Resolve/ResolveMatch。

我最终得到了 200 行完全托管的最小解决方案。如果您正在寻找 WSDAAPI,请查看以下示例(只是一个想法,肯定还没有准备好生产):

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;
    };
};

主.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;
}

希望这对某人有所帮助。