这是场景:
- 我有一个带有终端服务器的 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;
}
我从 Microsoft 支持中的示例复制了此代码。我也尝试将“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。我尝试用服务器中的替换它,但它什么也不做,不发送错误,也不会发送任何数据。
还有另一种方法可以做到这一点吗?我的代码有问题吗?
我感谢任何帮助或见解。