1

我想通过服务监控远程(共享)打印机上的打印作业。我有连接打印机的用户的模拟访问令牌(例如 \PC-PRINTER\Canon 打印机)。

冒充为用户后,我将 OpenPrinter 调用到打印机,然后调用 FindFirstPrinterChangeNotification。OpenPrinter 成功,但 FindFirstPrinterChangeNotification 失败并出现 ERROR_INVALID_HANDLE 错误。

如果我以普通用户(打印机的所有者 - 不是服务)的身份尝试它,所有成功并且通知都是有效的。此外,如果我在 Windows XP 上尝试它成功,它仅在 Windows 7 和可能是 Windows Vista 上失败。

我想我必须为模拟令牌设置一些安全属性或将一些其他属性传递给 OpenPrinter。我尝试了很多东西,但我没有找到解决方案。

我的代码示例:

PRINTER_NOTIFY_OPTIONS_TYPE optionsType;
unsigned short jobFields[] = {JOB_NOTIFY_FIELD_PRINTER_NAME};
optionsType.Type = JOB_NOTIFY_TYPE;
optionsType.Count = 1;
optionsType.pFields = &jobFields[0];

PRINTER_NOTIFY_OPTIONS options;
options.Version = 2; 
options.Count = 1;
options.pTypes = &optionsType;
options.Flags = 0;

DWORD dwFilter = PRINTER_CHANGE_DELETE_JOB | PRINTER_CHANGE_SET_JOB ;
HANDLE hPrinterHandle = NULL;

ImpersonateLoggedOnUser(hUserToken); //I retrieve token from printer owner's process (I try every process for this user)

OpenPrinter(L"\\\\PC-PRINTER\\Canon Printer", &hPrinterHandle, NULL); \\it succeeded and I try many other parameters as well

HANDLE hNotification = FindFirstPrinterChangeNotification(hPrinterHandle, dwFilter, 0, (LPVOID) &options);

if (hNotification == INVALID_HANDLE_VALUE)
{//it comes here, GetLastError() == ERROR_INVALID_HANDLE (Windows 7, process runs as service)
 //it succeded on Windows XP or when process runs in printer owner context
}

RevertToSelf();

感谢您的任何解决方案!

4

0 回答 0