直接打印RAW文件到打印机

来源:互联网 发布:mac怎么直接关闭程序 编辑:程序博客网 时间:2024/06/12 01:37

http://support.microsoft.com/kb/322091/zh-cn?spid=548&sid=348#top
好像是微软官方的支持给出了VC#的解决办法,可以运行,也不报错,就是没啥反应啊!


基本调用函数过程是:二进制文件,

OpenPrinter-->StartDocPrinter-->StartPagePrinter-->WritePrinter-->EndPagePrinter-->EndDocPrinter-->ClosePrinter


各函数的解释:

OpenPrinter:
  OpenPrinter函数获取一个句柄到指定的打印机或打印服务器或打印子系统中其他类型的句柄。
BOOL OpenPrinter(
  LPTSTR pPrinterName,         // printer or server name
  LPHANDLE phPrinter,          // printer or server handle
  LPPRINTER_DEFAULTS pDefault  // printer defaults
);
参数:
  pPrinterName:[输入参数]非空字符串,表示打印机名称;
  phPrinter:[输出]接收打开的打印机的句柄;
  pDefault:[输入】PRINTER_DEFAULTS结构的指针。此值可以为NULL。
返回值:
  如果函数成功,返回值是一个非零值;如果函数失败,返回值是零。调用GetLastError获得更多错误信息。
备注:
  不要在DllMain中调用此方法;非线程安全的,多线程操作要做同步。




StartDocPrinter
  StartDocPrinter函数通知spooler一个文件已经spooled for printing。
DWORD StartDocPrinter(
  HANDLE hPrinter,  // handle to printer object
  DWORD Level,      // information level
  LPBYTE pDocInfo   // information buffer
);
参数
  hPrinter:打印机句柄;
  Level:指定版本的结构,pDocInfo指向的这个结构;在Windows NT/2000/XP中此值必须是1,在Windows 95/98/Me中,这个值可以是1或2;
  pDocInfo:一个结构的指针,描述要打印的文件。Windows NT/2000/XP中pDocInfo是到DOC_INFO_1结构的指针;在Windows 95/98/Me,pDocInfo是一个DOC_INFO_1或DOC_INFO_2结构的指针。
  DOC_INFO_1结构为:
typedef struct _DOC_INFO_1 { 
  LPTSTR pDocName; 
  LPTSTR pOutputFile; 
  LPTSTR pDatatype; 
} DOC_INFO_1; 


StartPagePrinter:通知spooler一个页面将在指定的打印机上打印。

WritePrinter:
  WritePrinter函数通知splooer数据应该被写入到指定的打印机。
BOOL WritePrinter(
  HANDLE hPrinter,   // handle to printer object
  LPVOID pBuf,       // array of printer data
  DWORD cbBuf,       // size of array
  LPDWORD pcWritten  // bytes written to printer
);
参数:
  hPrinter:[输入]打印机句柄;
  PBUF:[输入]一个字节数组的指针,其中包含应写入到打印机的数据。
  cbBuf:[输入]数组的大小,以字节为单位。
  pcWritten:[输出]值的指针,存放写入打印机的数组长度

http://support.microsoft.com/kb/138594这个地址是C++的,更好


[cpp] view plaincopyprint?
  1. #include <Windows.h>   
  2. #include <StdIO.h>   
  3.   
  4.   
  5. // **********************************************************************  
  6. // PrintError - uses printf() to display error code information  
  7. //    
  8. // Params:   
  9. //   dwError       - the error code, usually from GetLastError()  
  10. //   lpString      - some caller-defined text to print with the error info  
  11. //    
  12. // Returns: void   
  13. //    
  14. void PrintError( DWORD dwError, LPCTSTR lpString )  
  15. {  
  16. #define MAX_MSG_BUF_SIZE 512   
  17.     TCHAR   *msgBuf;  
  18.     DWORD   cMsgLen;  
  19.   
  20.     cMsgLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |  
  21.                 FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL, dwError,  
  22.                 MAKELANGID(0, SUBLANG_ENGLISH_US), (LPTSTR) &msgBuf,  
  23.                 MAX_MSG_BUF_SIZE, NULL );  
  24.     printf( TEXT("%s Error [%d]:: %s\n"), lpString, dwError, msgBuf );  
  25.     LocalFree( msgBuf );  
  26. #undef MAX_MSG_BUF_SIZE   
  27. }  
  28. // end PrintError   
  29. // **********************************************************************  
  30.   
  31. // **********************************************************************  
  32. // ReadFileWithAlloc - allocates memory for and reads contents of a file  
  33. //    
  34. // Params:   
  35. //   szFileName   - NULL terminated string specifying file name  
  36. //   pdwSize      - address of variable to receive file bytes size  
  37. //   ppBytes      - address of pointer which will be allocated and contain file bytes  
  38. //    
  39. // Returns: TRUE for success, FALSE for failure.  
  40. //   
  41. // Notes: Caller is responsible for freeing the memory using GlobalFree()  
  42. //    
  43. BOOL ReadFileWithAlloc( LPTSTR szFileName, LPDWORD pdwSize, LPBYTE *ppBytes )  
  44. {  
  45.     HANDLE      hFile;  
  46.     DWORD       dwBytes;  
  47.     BOOL        bSuccess = FALSE;  
  48.   
  49.     // Validate pointer parameters  
  50.     if( ( pdwSize == NULL ) || ( ppBytes == NULL ) )  
  51.         return FALSE;  
  52.     // Open the file for reading   
  53.     hFile = CreateFile( szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );  
  54.     if( hFile == INVALID_HANDLE_VALUE )  
  55.     {  
  56.         PrintError( GetLastError(), TEXT("CreateFile()") );  
  57.         return FALSE;  
  58.     }  
  59.     // How big is the file?   
  60.     *pdwSize = GetFileSize( hFile, NULL );  
  61.     if( *pdwSize == (DWORD)-1 )  
  62.         PrintError( GetLastError(), TEXT("GetFileSize()") );  
  63.     else  
  64.     {  
  65.         // Allocate the memory  
  66.         *ppBytes = (LPBYTE)GlobalAlloc( GPTR, *pdwSize );  
  67.         if( *ppBytes == NULL )  
  68.             PrintError( GetLastError(), TEXT("Failed to allocate memory\n") );  
  69.         else  
  70.         {  
  71.             // Read the file into the newly allocated memory  
  72.             bSuccess = ReadFile( hFile, *ppBytes, *pdwSize, &dwBytes, NULL );  
  73.             if( ! bSuccess )  
  74.                 PrintError( GetLastError(), TEXT("ReadFile()") );  
  75.         }  
  76.     }  
  77.     // Clean up   
  78.     CloseHandle( hFile );  
  79.     return bSuccess;  
  80. }  
  81. // End ReadFileWithAlloc   
  82. // **********************************************************************  
  83.   
  84. // **********************************************************************  
  85. // RawDataToPrinter - sends binary data directly to a printer  
  86. //    
  87. // Params:   
  88. //   szPrinterName - NULL terminated string specifying printer name  
  89. //   lpData        - Pointer to raw data bytes  
  90. //   dwCount       - Length of lpData in bytes  
  91. //    
  92. // Returns: TRUE for success, FALSE for failure.  
  93. //    
  94. BOOL RawDataToPrinter( LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount )  
  95. {  
  96.     HANDLE     hPrinter;  
  97.     DOC_INFO_1 DocInfo;  
  98.     DWORD      dwJob;  
  99.     DWORD      dwBytesWritten;  
  100.   
  101.     // Need a handle to the printer.  
  102.     if( ! OpenPrinter( szPrinterName, &hPrinter, NULL ) )  
  103.     {  
  104.         PrintError( GetLastError(), TEXT("OpenPrinter") );  
  105.         return FALSE;  
  106.     }  
  107.   
  108.     // Fill in the structure with info about this "document."  
  109.     DocInfo.pDocName = TEXT("My Document");  
  110.     DocInfo.pOutputFile = NULL;  
  111.     DocInfo.pDatatype = TEXT("RAW");  
  112.     // Inform the spooler the document is beginning.  
  113.     if( (dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo )) == 0 )  
  114.     {  
  115.         PrintError( GetLastError(), TEXT("StartDocPrinter") );  
  116.         ClosePrinter( hPrinter );  
  117.         return FALSE;  
  118.     }  
  119.     // Start a page.   
  120.     if( ! StartPagePrinter( hPrinter ) )  
  121.     {  
  122.         PrintError( GetLastError(), TEXT("StartPagePrinter") );  
  123.         EndDocPrinter( hPrinter );  
  124.         ClosePrinter( hPrinter );  
  125.         return FALSE;  
  126.     }  
  127.     // Send the data to the printer.  
  128.     if( ! WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )  
  129.     {  
  130.         PrintError( GetLastError(), TEXT("WritePrinter") );  
  131.         EndPagePrinter( hPrinter );  
  132.         EndDocPrinter( hPrinter );  
  133.         ClosePrinter( hPrinter );  
  134.         return FALSE;  
  135.     }  
  136.     // End the page.   
  137.     if( ! EndPagePrinter( hPrinter ) )  
  138.     {  
  139.         PrintError( GetLastError(), TEXT("EndPagePrinter") );  
  140.         EndDocPrinter( hPrinter );  
  141.         ClosePrinter( hPrinter );  
  142.         return FALSE;  
  143.     }  
  144.     // Inform the spooler that the document is ending.  
  145.     if( ! EndDocPrinter( hPrinter ) )  
  146.     {  
  147.         PrintError( GetLastError(), TEXT("EndDocPrinter") );  
  148.         ClosePrinter( hPrinter );  
  149.         return FALSE;  
  150.     }  
  151.     // Tidy up the printer handle.  
  152.     ClosePrinter( hPrinter );  
  153.     // Check to see if correct number of bytes were written.  
  154.     if( dwBytesWritten != dwCount )  
  155.     {  
  156.         printf( TEXT("Wrote %d bytes instead of requested %d bytes.\n"), dwBytesWritten, dwCount );  
  157.         return FALSE;  
  158.     }  
  159.     return TRUE;  
  160. }  
  161. // End RawDataToPrinter   
  162. // **********************************************************************  
  163.   
  164. // **********************************************************************  
  165. // main - entry point for this console application  
  166. //    
  167. // Params:   
  168. //   argc        - count of command line arguments  
  169. //   argv        - array of NULL terminated command line arguments  
  170. //   
  171. // Returns: 0 for success, non-zero for failure.  
  172. //    
  173. // Command line: c:\>RawPrint PrinterName FileName  
  174. //               sends raw data file to printer using spooler APIs  
  175. //               written nov 1999 jmh  
  176. //   
  177. int main( int argc, char* argv[] )  
  178. {  
  179.     LPBYTE  pBytes = NULL;  
  180.     DWORD   dwSize = 0;  
  181.   
  182.     if( argc != 3 )  
  183.         return printf( TEXT("Syntax: %s <PrinterName> <FileName>\n"), argv[0] );  
  184.   
  185.     printf( TEXT("Attempting to send file [%s] to printer [%s].\n"), argv[2], argv[1] );  
  186.   
  187.     if( ! ReadFileWithAlloc( argv[2], &dwSize, &pBytes ) )  
  188.         return printf( TEXT("Failed to allocate memory for and read file [%s].\n"), argv[2] );  
  189.   
  190.     if( ! RawDataToPrinter( argv[1], pBytes, dwSize ) )  
  191.         printf( TEXT("Failed to send data to printer.\n") );  
  192.     else  
  193.         printf( TEXT("Data sent to printer.\n") );  
  194.   
  195.     GlobalFree( (HGLOBAL)pBytes );  
  196.     return 0;  
  197. }  
  198. // end main   
  199. // **********************************************************************  

 

原创粉丝点击