在应用程序的服务模式下CPrintDialog创建失败 [英] CPrintDialog Creation Failing in Service Mode of an Application

查看:95
本文介绍了在应用程序的服务模式下CPrintDialog创建失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以自动打印报告的应用程序.我正在使用CPrintDialog获取打印机DC.

I have an application that prints the report automatically. I am using CPrintDialog to get the Printer DC.

void CMyClass::PrintReport()
{
    CDC dc;
    CPrintDialog printDlg(FALSE);
    printDlg.GetDefaults ();
    ::DeleteDC( printDlg.m_pd.hDC );
    LPDEVMODE pDevMode = printDlg.GetDevMode();
    if(pDevMode)
    {
        pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
        ::GlobalUnlock(pDevMode);
    }
    HDC hDC;
    if( (hDC = printDlg.CreatePrinterDC()) == NULL )
    {
        ::GlobalFree( printDlg.m_pd.hDevMode );
        ::GlobalFree( printDlg.m_pd.hDevNames );
        return;
    }
    ::GlobalFree( printDlg.m_pd.hDevMode );
    ::GlobalFree( printDlg.m_pd.hDevNames );
    dc.Attach(hDC);         // Attach a printer DC
    dc.m_bPrinting = TRUE;
    dc.SetMapMode(MM_LOENGLISH);
    /* 

        Printing Logic using dc
    */
}

当我在控制台应用程序附带的调试"模式下运行应用程序时,此方法工作正常.

This works fine when I run my application in the Debug mode which comes a a Console application.

但是,当我将应用程序作为Windows服务运行时,CPrintDialog创建失败.

But, the CPrintDialog creation is failing when I run the application as a Windows Service.

我做错什么了吗? :(请帮助我.

Am I doing anything wrong? :( Please help me.

注意:该应用程序的设计使其可以在安装中作为服务运行.

Note: The Application is designed in a way to run as a Service in the Installation.

推荐答案

当我将应用程序作为Windows服务运行时,CPrintDialog创建失败.

the CPrintDialog creation is failing when I run the application as a Windows Service.

您不能在Windows服务中显示对话框(或任何类型的用户界面).所以CPrintDialog永远都行不通.

You cannot display dialogs (or any type of user interface) in a Windows Service. So CPrintDialog is never going to work.

但是,假设您已经知道要打印到哪台打印机,则无需创建对话框即可获取打印机设备上下文.而且由于您是作为非交互式服务运行的,因此您必须已经知道这一点,因为用户无法选择打印机.

But you don't need to create a dialog to get a printer device context, assuming that you already know which printer you want to print to. And since you're running as a non-interactive service, you must already know this, because there's no way that the user can choose a printer.

为此,只需调用 CreateDC ,直接指定"WINSPOOL"作为设备和打印机名称.您可以使用 EnumPrinters 函数.如何操作文章中都方便地记录了这些内容:获取打印机设备上下文.

To do so, just call CreateDC directly, specifying "WINSPOOL" as the device and the name of the printer. You can obtain the name of the desired printer by enumerating the installed printers using the EnumPrinters function. This is all conveniently documented in a how-to article: Retrieve a Printer Device Context.

这篇关于在应用程序的服务模式下CPrintDialog创建失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆