在终端服务器中打印原始数据

Printing Raw Data in Terminal Server

本文关键字:打印 原始数据 服务器 终端      更新时间:2023-10-16

场景如下:

  • 我有一个Windows Server 2008与终端服务器(没有域控制器,没有加入到域)
  • 我有一台Windows XP SP3更新的客户端机器。.NET 3.0 SP1和.NET 4.0)
  • 我正在使用Embarcadero c++ Builder (BCB6)
  • 我有一个票据打印机(热敏打印机,POS打印机,爱普生,斑马等)

当我连接到终端服务器,打印机工作正常。我试着打印一个测试页。

当我使用我的软件在本地计算机上发送终端服务器中的原始数据时,我得到这个错误:

Windows Presentation Foundation terminal server print W has encountered a
problem and needs to close. We are sorry for the inconvenience.

我遵循了这个支持页面的建议,但运气不好。

我过去是直接打印到LPT1:,但是在Windows Server 2008中,这变得越来越困难,所以我们必须改变我们打印到这种打印机的方式。

这是我正在使用的代码。我在本地测试了,它工作得很好,但在终端服务器不工作:

bool TForm1::RawDataToPrinter(char* szPrinterName, char* lpData, unsigned int dwCount )
{
    int BytesWritten;
    HANDLE hPrinter;
    TDocInfo1 DocInfo;
    bool bStatus = false;
    int dwJob = 0;
    unsigned long dwBytesWritten = 0;
    // Open a handle to the printer.
    bStatus = OpenPrinter( szPrinterName, &hPrinter, NULL );
    if( bStatus )
    {
        // Fill in the structure with info about this "document."
        DocInfo.pDocName = "My Document";
        DocInfo.pOutputFile = NULL;
        DocInfo.pDatatype = "RAW";
        // to indicate that the application will be sending document data to the printer.
        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
        if ( dwJob > 0 )
        {
            // Start a page.
            bStatus = StartPagePrinter( hPrinter );
            bStatus = true;
            if( bStatus )
            {
                // Send the data to the printer.
                bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten );
                EndPagePrinter ( hPrinter );
            }
            // Inform the spooler that the document is ending.
            EndDocPrinter( hPrinter );
        }
        // Close the printer handle.
        ClosePrinter( hPrinter );
    }
    // Check to see if correct number of bytes were written.
    if (!bStatus || (dwBytesWritten != dwCount))
        bStatus = false;
    else
        bStatus = true;
    return bStatus;
}

我从微软的支持中复制了这段代码。我还尝试将"RAW"更改为"TEXT",但我得到同样的错误。

我尝试了下面的代码,因为它使用GDI打印:
long pageline;
char prueba[255];
Printer()->SetPrinter(ListBox1->Items->Strings[ListBox1->ItemIndex].c_str(), "WINSPOOL", "", NULL);
Printer()->BeginDoc();
pageline = 0;
while(pageline < Memo1->Lines->Count)
{
    Printer()->Canvas->TextOut(10, (10 + Printer()->Canvas->TextHeight("Hi! There")) * pageline, Memo1->Lines->Strings[pageline]);
    pageline++;
}
Printer()->EndDoc();

这是我在Embarcadero论坛上找到的一个例子。

我还验证了TsWpfWrp.exe。我试着用服务器中的一个替换它,但它什么也不做,不发送错误,但也不会发送任何数据。

还有别的方法吗?我在代码中有什么错误吗?

我发现了问题,是Easy Print驱动程序,它期望在RAW模式下使用XPS规范,但我只发送文本。

我禁用了Easy Print,将打印机置于回退模式(类似的东西),这是终端服务器,首先它寻找安装的驱动程序,然后寻找Easy Print(这可以在打印机的高级选项中的属性中进行验证)。