Send Raw Data to a Printer by Using the Win32 API

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

你可能感兴趣的:(api,String,null,UP,structure)