使用网络打印机的FindFirstPrinterChangeNotification因ERROR_INVALID_HANDLE(从服务调用)而失败

时间:2013-07-10 05:03:06

标签: windows printing network-printers

我想监控远程(共享)打印机上的打印作业。我为连接打印机的用户设置了模拟访问令牌(例如\ PC-PRINTER \ Canon Printer)。

作为用户模拟后,我将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();

感谢任何解决方案!

0 个答案:

没有答案