Send Raw Data to a Printer by Using the Win32 API

來源:互聯網
上載者:User

// 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 />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.