您的位置:首页 > 其它

Send Raw Data to a Printer by Using the Win32 API

2010-04-09 22:00 417 查看
// RawDataToPrinter - sends binary data directly to a printer
//
// Params:
//   szPrinterName - NULL terminated string specifying printer name
//   lpData        - Pointer to raw data bytes
//   dwCount       - Length of lpData in bytes
//
// Returns: TRUE for success, FALSE for failure.
//
#include <winspool.h>
BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
HANDLE hPrinter = NULL;
DWORD  dwBytesWritten = 0;

// Need a handle to the printer.
if(  OpenPrinter( szPrinterName, &hPrinter, NULL ) )
{
// Fill in the structure with info about this "document."
DOC_INFO_1 DocInfo ={0};
DocInfo.pDocName = _T("My Document");
DocInfo.pDatatype = _T("RAW");
// Inform the spooler the document is beginning.
if( StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo ) )
{
// Start a page.
if( StartPagePrinter( hPrinter ) )
{
// Send the data to the printer.
if( WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
{
//Write ok
}
// End the page.
EndPagePrinter( hPrinter );
}
// Inform the spooler that the document is ending.
EndDocPrinter( hPrinter );
}
// Tidy up the printer handle.
ClosePrinter( hPrinter );
}

// Check to see if correct number of bytes were written.
if( dwBytesWritten != dwCount )
return FALSE;
return TRUE;
}


//带错误检查

// RawDataToPrinter - sends binary data directly to a printer
//
// Params:
//   szPrinterName - NULL terminated string specifying printer name
//   lpData        - Pointer to raw data bytes
//   dwCount       - Length of lpData in bytes
//
// Returns: TRUE for success, FALSE for failure.
//
#include <winspool.h>
BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
DWORD  dwErr = ERROR_SUCCESS;

// Need a handle to the printer.
HANDLE hPrinter = NULL;
if(  OpenPrinter( szPrinterName, &hPrinter, NULL ) )
{
// Fill in the structure with info about this "document."
DOC_INFO_1 DocInfo ={0};
DocInfo.pDocName = _T("My Document");
DocInfo.pDatatype = _T("RAW");
// Inform the spooler the document is beginning.
if( StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo ) )
{
// Start a page.
if( StartPagePrinter( hPrinter ) )
{
// Send the data to the printer.
DWORD  dwBytesWritten = 0;
if( WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
{
//Write ok
}
else //WritePrinter fail
{
dwErr = GetLastError();
}

// End the page.
if(EndPagePrinter( hPrinter ))
{
}
else //EndPagePrinter fail
{
dwErr = GetLastError();
}
}
else //StartPagePrinter fail
{
dwErr = GetLastError();
}

// Inform the spooler that the document is ending.
if(EndDocPrinter( hPrinter ))
{
}
else //EndDocPrinter fail
{
dwErr = GetLastError();
}
}
else //StartDocPrinter fail
{
dwErr = GetLastError();
}

// Tidy up the printer handle.
if(ClosePrinter( hPrinter ))
{
}
else //ClosePrinter fail
{
dwErr = GetLastError();
}
}
else //OpenPrinter fail
{
dwErr = GetLastError();
}

// Check Error
return ( dwErr == ERROR_SUCCESS )? TRUE:FALSE;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐