如何唯一且可靠地识别给定服务器上的Windows打印队列,包括跨打印队列重命名?
我想处理以下情况:
如何判断打印机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 [printer name]中有一个包含GUID的QueueInstanceId字段,但仅限于Windows 8 / Server 2012.它在早期操作系统中不存在。< / p>
打印假脱机程序API和GDI打印API似乎都按名称识别队列,所以我没有找到任何有用的东西。
答案 0 :(得分:1)
以下是我在旧项目中用于确定打印机名称和驱动程序名称的一些代码(以及打印机的其他一些细节)。我不确定是否匹配驱动程序名称就足够了。我知道我已经在Win7上测试了它,我大约80%肯定我在XP上测试过它(不久之前):
在系统上返回打印机名称/驱动程序名称的Helper方法:
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
不确定这是否有帮助...
答案 1 :(得分: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);
}