3

如何唯一且可靠地识别给定服务器上的 Windows 打印队列,包括跨打印队列重命名?

我想处理以下情况:

  1. Jdoe 创建打印机 A
  2. 我的程序有时会收集有关打印机 A 的信息
  3. Jdoe 将打印机 A 重命名为打印机 AA
  4. 我的程序在某个时候再次收集有关打印机 AA 的信息

如何判断打印机 A 和打印机 AA 是同一台打印机(名称已更改)?

我想在支持 Windows XP/2003 及更高版本的 C# 中执行此操作。

我尝试过的事情:

在 Windows 8/Server 2012 上,我似乎可以通过 WMI 查看 CIM_LogicalDevice->DeviceID 来执行此操作,这似乎在重命名时保持一致,但在早期版本的操作系统中,此属性仅包含队列名称并在队列为重命名。

我还查看了 Win32_PnPEntity 类(不包含 Windows 8 之前的打印机)和 Win32_Printer 类(除了名称之外不包含任何类型的 ID)。

在注册表中 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers[打印机名称] 有一个包含 GUID 的 QueueInstanceId 字段,但仅在 Windows 8/Server 2012 中。它不存在于早期的操作系统中。

打印后台处理程序 API 和 GDI 打印 API 似乎都按名称识别队列,所以我在那里没有发现任何有用的东西。

4

2 回答 2

1

这是我在旧项目中使用的一些代码,用于确定打印机名称和驱动程序名称(以及打印机的其他一些细节)。我不确定匹配驱动程序名称是否足以满足您的需求。我知道我已经在 Win7 上对其进行了测试,并且我大约 80% 确定我在 XP 上对其进行了测试(这是不久前):

在系统上返回打印机名称/驱动程序名称的辅助方法:

public static List<PrinterDriverItem> GetCardPrinterList()
{
    List<PrinterDriverItem> returnValue = new List<PrinterDriverItem>();

    uint cbNeeded = 0;
    uint cReturned = 0;
    bool ret = Win32PrintApi.EnumPrinters(Win32PrintApi.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);

    IntPtr addr = Marshal.AllocHGlobal((int)cbNeeded);
    ret = Win32PrintApi.EnumPrinters(Win32PrintApi.PRINTER_ENUM_LOCAL, null, 2, addr, cbNeeded, ref cbNeeded, ref cReturned);

    if (ret)
    {
        Win32PrintApi.PRINTER_INFO_2[] printerInfo = new Win32PrintApi.PRINTER_INFO_2[cReturned];
        int offset = addr.ToInt32();

        for (int i = 0; i < cReturned; i++)
        {
            printerInfo[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pComment = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pLocation = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pDevMode = Marshal.ReadIntPtr(new IntPtr(offset));
            offset += 4;
            printerInfo[i].pSepFile = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pDatatype = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
            offset += 4;
            printerInfo[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
            offset += 4;
            printerInfo[i].Attributes = (uint)Marshal.ReadInt32(new IntPtr(offset));
            offset += 4;
            printerInfo[i].Priority = (uint)Marshal.ReadInt32(new IntPtr(offset));
            offset += 4;
            printerInfo[i].DefaultPriority = (uint)Marshal.ReadInt32(new IntPtr(offset));
            offset += 4;
            printerInfo[i].StartTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
            offset += 4;
            printerInfo[i].UntilTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
            offset += 4;
            printerInfo[i].Status = (uint)Marshal.ReadInt32(new IntPtr(offset));
            offset += 4;
            printerInfo[i].cJobs = (uint)Marshal.ReadInt32(new IntPtr(offset));
            offset += 4;
            printerInfo[i].AveragePPM = (uint)Marshal.ReadInt32(new IntPtr(offset));
            offset += 4;

            returnValue.Add(new PrinterDriverItem() { PrinterName = printerInfo[i].pPrinterName, DriverName = printerInfo[i].pDriverName });
        }

    }

    Marshal.FreeHGlobal(addr);

    return returnValue;
}

Win32PrintApi 类:

public class Win32PrintApi
{
    public const int PRINTER_ENUM_LOCAL = 0x00000002;

    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool EnumPrinters(int Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned);

    [StructLayout(LayoutKind.Sequential)]
    public struct PRINTER_INFO_2
    {
        public string pServerName;
        public string pPrinterName;
        public string pShareName;
        public string pPortName;
        public string pDriverName;
        public string pComment;
        public string pLocation;
        public IntPtr pDevMode;
        public string pSepFile;
        public string pPrintProcessor;
        public string pDatatype;
        public string pParameters;
        public IntPtr pSecurityDescriptor;
        public uint Attributes;
        public uint Priority;
        public uint DefaultPriority;
        public uint StartTime;
        public uint UntilTime;
        public uint Status;
        public uint cJobs;
        public uint AveragePPM;
    }
}

编辑:对于它的价值,我只是逐步浏览了这段代码,看看我是否能找到任何独特的东西......这些值中的大多数对我来说都是空的或空白的,但这里有一些东西:

Printer Name: XPS Card Printer - NEW 
Driver Name: XPS Card Printer
Port Name: USB DXP01 Port
Attributes: 2624
DevMode: 3562584

不确定这是否会有所帮助...

于 2013-04-11T23:35:22.800 回答
1

您需要使用的是安装类中的以下功能。

这是我编写的粗略示例 Cpp 代码。无论打印机是本地/网络/重定向的 rdp 打印机,即使名称不同,硬件 ID 也将始终相同。

#include <Windows.h>
#include <stdio.h>
#include <SetupAPI.h>
#pragma comment(lib, "setupapi.lib")

void PrintPrinterIds(REFGUID ClassGuid)
{
    HDEVINFO hDevInfo = SetupDiGetClassDevs(&ClassGuid, NULL, NULL, DIGCF_PRESENT);
    if (hDevInfo == INVALID_HANDLE_VALUE)
    {
        wprintf(L"Cannot get devices : %d\n", GetLastError());
        return;
    }

    int idx = 0;
    DWORD errorVal = ERROR_SUCCESS;
    while (true)
    {
        SP_DEVINFO_DATA devInfoData = {};
        WCHAR regProp[512];
        devInfoData.cbSize = sizeof(devInfoData);

        if (!SetupDiEnumDeviceInfo(hDevInfo, idx, &devInfoData))
        {
            errorVal = GetLastError();
            break;
        }

        if (!SetupDiGetDeviceRegistryProperty(
            hDevInfo,
            &devInfoData,
            SPDRP_FRIENDLYNAME,
            NULL,
            (PBYTE)regProp,
            sizeof(regProp),
            NULL))
        {
            errorVal = GetLastError();
            break;
        }

        wprintf(L"Friendly name = %s\n", regProp);

        if (!SetupDiGetDeviceRegistryProperty(
            hDevInfo,
            &devInfoData,
            SPDRP_HARDWAREID,
            NULL,
            (PBYTE)regProp,
            sizeof(regProp),
            NULL))
        {
            errorVal = GetLastError();
            break;
        }

        // hardwareId is reg_multi_sz
        // Print all of the hardware ids for this device
        PWCHAR pId = regProp;
        do
        {
            wprintf(L"HardwareId = %s\n", pId);
            pId += wcslen(pId) + 1;
        } while (pId[0] != 0);

        // Point to next device
        idx++;
    }

    if (errorVal != ERROR_NO_MORE_ITEMS)
    {
        printf("Error : %d\n", errorVal);
    }

    SetupDiDestroyDeviceInfoList(hDevInfo);
}

int main()
{
    // {4d36e979-e325-11ce-bfc1-08002be10318}
    static const GUID PrinterClass =
    { 0x4d36e979, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
    PrintPrinterIds(PrinterClass);

    // L"{1ed2bbf9-11f0-4084-b21f-ad83a8e6dcdc}"
    static const GUID PrinterQueue =
    { 0x1ed2bbf9, 0x11f0, 0x4084, { 0xb2, 0x1f, 0xad, 0x83, 0xa8, 0xe6, 0xdc, 0xdc } };
    PrintPrinterIds(PrinterQueue);
}
于 2019-04-11T03:17:54.723 回答