// RawDataToPrinter - sends binary data directly to a printer<br />//<br />// Params:<br />// szPrinterName - NULL terminated string specifying printer name<br />// lpData - Pointer to raw data bytes<br />// dwCount - Length of lpData in bytes<br />//<br />// Returns: TRUE for success, FALSE for failure.<br />//<br />#include <winspool.h><br />BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)<br />{<br /> HANDLE hPrinter = NULL;<br /> DWORD dwBytesWritten = 0;</p><p> // Need a handle to the printer.<br /> if( OpenPrinter( szPrinterName, &hPrinter, NULL ) )<br /> {<br /> // Fill in the structure with info about this "document."<br /> DOC_INFO_1 DocInfo ={0};<br /> DocInfo.pDocName = _T("My Document");<br /> DocInfo.pDatatype = _T("RAW");<br /> // Inform the spooler the document is beginning.<br /> if( StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo ) )<br /> {<br /> // Start a page.<br /> if( StartPagePrinter( hPrinter ) )<br /> {<br /> // Send the data to the printer.<br /> if( WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )<br /> {<br /> //Write ok<br /> }<br /> // End the page.<br /> EndPagePrinter( hPrinter );<br /> }<br /> // Inform the spooler that the document is ending.<br /> EndDocPrinter( hPrinter );<br /> }<br /> // Tidy up the printer handle.<br /> ClosePrinter( hPrinter );<br /> }</p><p> // Check to see if correct number of bytes were written.<br /> if( dwBytesWritten != dwCount )<br /> return FALSE;<br /> return TRUE;<br />}
//帶錯誤檢查
// RawDataToPrinter - sends binary data directly to a printer<br />//<br />// Params:<br />// szPrinterName - NULL terminated string specifying printer name<br />// lpData - Pointer to raw data bytes<br />// dwCount - Length of lpData in bytes<br />//<br />// Returns: TRUE for success, FALSE for failure.<br />//<br />#include <winspool.h><br />BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)<br />{<br /> DWORD dwErr = ERROR_SUCCESS;</p><p> // Need a handle to the printer.<br /> HANDLE hPrinter = NULL;<br /> if( OpenPrinter( szPrinterName, &hPrinter, NULL ) )<br /> {<br /> // Fill in the structure with info about this "document."<br /> DOC_INFO_1 DocInfo ={0};<br /> DocInfo.pDocName = _T("My Document");<br /> DocInfo.pDatatype = _T("RAW");<br /> // Inform the spooler the document is beginning.<br /> if( StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo ) )<br /> {<br /> // Start a page.<br /> if( StartPagePrinter( hPrinter ) )<br /> {<br /> // Send the data to the printer.<br /> DWORD dwBytesWritten = 0;<br /> if( WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )<br /> {<br /> //Write ok<br /> }<br /> else //WritePrinter fail<br /> {<br /> dwErr = GetLastError();<br /> }</p><p> // End the page.<br /> if(EndPagePrinter( hPrinter ))<br /> {<br /> }<br /> else //EndPagePrinter fail<br /> {<br /> dwErr = GetLastError();<br /> }<br /> }<br /> else //StartPagePrinter fail<br /> {<br /> dwErr = GetLastError();<br /> }</p><p> // Inform the spooler that the document is ending.<br /> if(EndDocPrinter( hPrinter ))<br /> {<br /> }<br /> else //EndDocPrinter fail<br /> {<br /> dwErr = GetLastError();<br /> }<br /> }<br /> else //StartDocPrinter fail<br /> {<br /> dwErr = GetLastError();<br /> } </p><p> // Tidy up the printer handle.<br /> if(ClosePrinter( hPrinter ))<br /> {<br /> }<br /> else //ClosePrinter fail<br /> {<br /> dwErr = GetLastError();<br /> }<br /> }<br /> else //OpenPrinter fail<br /> {<br /> dwErr = GetLastError();<br /> } </p><p> // Check Error<br /> return ( dwErr == ERROR_SUCCESS )? TRUE:FALSE;<br />}<br />