如果程序是从 Windows 服务执行的,为什么 DocumentProperties() 会错误地报告打印机的方向设置



我有一个VC 控制台应用程序,该应用具有读取打印机当前方向设置的函数。使用此MS页面中的示例代码,从CMD提示符执行程序时,我可以成功阅读打印机的设置。但是,如果我从Windows服务(以C#编写)执行它,即使其他设置看起来正确,当前方向也将始终为1(肖像)。为什么是?

总结:对于将方向设置为景观的打印机,以下代码(如果从cmd.exe运行),则可以正确输出:

原始打印机方向= 2

但是,如果从用C#编写的Windows服务运行,请始终输出:

原始打印机方向= 1

/*
* Step 1:
* Allocate a buffer of the correct size.
*/
dwNeeded = DocumentProperties(NULL,
    hPrinter,       /* Handle to our printer. */
    deviceName,        /* Name of the printer. */
    NULL,           /* Asking for size, so */
    NULL,           /* these are not used. */
    0);             /* Zero returns buffer size. */
pDevMode = (LPDEVMODE)malloc(dwNeeded);
/*
* Step 2:
* Get the default DevMode for the printer and
* modify it for your needs.
*/
dwRet = DocumentProperties(NULL,
    hPrinter,
    deviceName,
    pDevMode,       /* The address of the buffer to fill. */
    NULL,           /* Not using the input buffer. */
    DM_OUT_BUFFER); /* Have the output buffer filled. */
if (dwRet != IDOK)
{
    /* If failure, cleanup and return failure. */
    free(pDevMode);
    ClosePrinter(hPrinter);
    return NULL;
}
cout << "original printer orientation=";
cout << pDevMode->dmOrientation;

这是由于权限。Windows服务在"本地系统"下运行,当然,这当然无法识别我在自己的登录名称中进行的打印机设置更改。感谢退休的忍者,他想到了权限。

最新更新