gpt4 book ai didi

windows - 如何在 Delphi(或通过 Windows API)的 RDP session 中唯一标识由 EasyPrint 打印机重定向的打印机?

转载 作者:可可西里 更新时间:2023-11-01 11:42:27 28 4
gpt4 key购买 nike

我喜欢在 Delphi 的 RDP session 中唯一标识由 EasyPrint 打印机重定向的打印机。每次用户连接到 RDP session 时,打印机的名称都会更改,例如:“HPLJP1606(重定向 6)”。最后一个数字 (6) 始终在变化,以确保打印机名称在整个服务器中是唯一的。在我的程序中,我喜欢保存一些与打印机型号相关的打印机相关参数。我期待找到类似 GUID 的东西来识别某个地方的打印机,有点像 MAC 地址。它应该允许我的程序确保它与之前选择的打印机相同。

我和我的同事研究了这个解决方案: How can I uniquely identify a print queue on Windows even if the queue is renamed?这是行不通的,因为每个 session 的 GUID 都不同。

我们还尝试了一些系统解决方法来重命名打印机,但它并不容易推广。

有没有办法在不使用 Delphi 中的打印机名称或通过 Windows API 的情况下识别底层 EasyPrint 打印机?

我用的是 Delphi 10.2

提前致谢

最佳答案

我不使用 delphi,但这应该对您有所帮助。您需要使用的是设置类中的以下函数。

这是我编写的粗略示例 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);
}

关于windows - 如何在 Delphi(或通过 Windows API)的 RDP session 中唯一标识由 EasyPrint 打印机重定向的打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55510441/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com