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; }
相关文章推荐
- How to send raw data to a printer by using Visual C# .NET
- Using the data layer API to query your Kentico database
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- How To Perform a Full Export And Exclude Certain Schemas Using The Data Pump API? [ID 1340781.1]
- Unable to determine the principal end of an association between the types '***. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
- How to populate the datagrid on background thread with data binding by using Visual C#
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- QT(Send raw data to printer)发送原始数据到USB打印机
- QT(Send raw data to printer)发送原始数据到USB打印机
- Using Ldp.exe to Find Data in the Active Directory
- dotNET C# Programmer’s Guide to the Win32 API (Win32 API 声明C#版)
- Using R to read and plot the csv data
- Openstack: python API “how to download image from glance using the python api”
- Python Web-第六周-JSON and the REST Architecture(Using Python to Access Web Data)
- How to open the dialog out of iframe by using jquery dialog
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- Using the JavaScript API to Embed BIRT Viewer Toolkit into PHP, .NET, and More
- How to enable the use of 'Ad Hoc Distributed Queries' by using sp_configure
- How to: Send Data Using the WebRequest Class
- Using the this API wsh_deliveries_pub.Delivery_Action to ship confirm for the delvery