直接打印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++的,更好
- #include <Windows.h>
- #include <StdIO.h>
- // **********************************************************************
- // PrintError - uses printf() to display error code information
- //
- // Params:
- // dwError - the error code, usually from GetLastError()
- // lpString - some caller-defined text to print with the error info
- //
- // Returns: void
- //
- void PrintError( DWORD dwError, LPCTSTR lpString )
- {
- #define MAX_MSG_BUF_SIZE 512
- TCHAR *msgBuf;
- DWORD cMsgLen;
- cMsgLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL, dwError,
- MAKELANGID(0, SUBLANG_ENGLISH_US), (LPTSTR) &msgBuf,
- MAX_MSG_BUF_SIZE, NULL );
- printf( TEXT("%s Error [%d]:: %s\n"), lpString, dwError, msgBuf );
- LocalFree( msgBuf );
- #undef MAX_MSG_BUF_SIZE
- }
- // end PrintError
- // **********************************************************************
- // **********************************************************************
- // ReadFileWithAlloc - allocates memory for and reads contents of a file
- //
- // Params:
- // szFileName - NULL terminated string specifying file name
- // pdwSize - address of variable to receive file bytes size
- // ppBytes - address of pointer which will be allocated and contain file bytes
- //
- // Returns: TRUE for success, FALSE for failure.
- //
- // Notes: Caller is responsible for freeing the memory using GlobalFree()
- //
- BOOL ReadFileWithAlloc( LPTSTR szFileName, LPDWORD pdwSize, LPBYTE *ppBytes )
- {
- HANDLE hFile;
- DWORD dwBytes;
- BOOL bSuccess = FALSE;
- // Validate pointer parameters
- if( ( pdwSize == NULL ) || ( ppBytes == NULL ) )
- return FALSE;
- // Open the file for reading
- hFile = CreateFile( szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
- if( hFile == INVALID_HANDLE_VALUE )
- {
- PrintError( GetLastError(), TEXT("CreateFile()") );
- return FALSE;
- }
- // How big is the file?
- *pdwSize = GetFileSize( hFile, NULL );
- if( *pdwSize == (DWORD)-1 )
- PrintError( GetLastError(), TEXT("GetFileSize()") );
- else
- {
- // Allocate the memory
- *ppBytes = (LPBYTE)GlobalAlloc( GPTR, *pdwSize );
- if( *ppBytes == NULL )
- PrintError( GetLastError(), TEXT("Failed to allocate memory\n") );
- else
- {
- // Read the file into the newly allocated memory
- bSuccess = ReadFile( hFile, *ppBytes, *pdwSize, &dwBytes, NULL );
- if( ! bSuccess )
- PrintError( GetLastError(), TEXT("ReadFile()") );
- }
- }
- // Clean up
- CloseHandle( hFile );
- return bSuccess;
- }
- // End ReadFileWithAlloc
- // **********************************************************************
- // **********************************************************************
- // 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.
- //
- BOOL RawDataToPrinter( LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount )
- {
- HANDLE hPrinter;
- DOC_INFO_1 DocInfo;
- DWORD dwJob;
- DWORD dwBytesWritten;
- // Need a handle to the printer.
- if( ! OpenPrinter( szPrinterName, &hPrinter, NULL ) )
- {
- PrintError( GetLastError(), TEXT("OpenPrinter") );
- return FALSE;
- }
- // Fill in the structure with info about this "document."
- DocInfo.pDocName = TEXT("My Document");
- DocInfo.pOutputFile = NULL;
- DocInfo.pDatatype = TEXT("RAW");
- // Inform the spooler the document is beginning.
- if( (dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo )) == 0 )
- {
- PrintError( GetLastError(), TEXT("StartDocPrinter") );
- ClosePrinter( hPrinter );
- return FALSE;
- }
- // Start a page.
- if( ! StartPagePrinter( hPrinter ) )
- {
- PrintError( GetLastError(), TEXT("StartPagePrinter") );
- EndDocPrinter( hPrinter );
- ClosePrinter( hPrinter );
- return FALSE;
- }
- // Send the data to the printer.
- if( ! WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
- {
- PrintError( GetLastError(), TEXT("WritePrinter") );
- EndPagePrinter( hPrinter );
- EndDocPrinter( hPrinter );
- ClosePrinter( hPrinter );
- return FALSE;
- }
- // End the page.
- if( ! EndPagePrinter( hPrinter ) )
- {
- PrintError( GetLastError(), TEXT("EndPagePrinter") );
- EndDocPrinter( hPrinter );
- ClosePrinter( hPrinter );
- return FALSE;
- }
- // Inform the spooler that the document is ending.
- if( ! EndDocPrinter( hPrinter ) )
- {
- PrintError( GetLastError(), TEXT("EndDocPrinter") );
- ClosePrinter( hPrinter );
- return FALSE;
- }
- // Tidy up the printer handle.
- ClosePrinter( hPrinter );
- // Check to see if correct number of bytes were written.
- if( dwBytesWritten != dwCount )
- {
- printf( TEXT("Wrote %d bytes instead of requested %d bytes.\n"), dwBytesWritten, dwCount );
- return FALSE;
- }
- return TRUE;
- }
- // End RawDataToPrinter
- // **********************************************************************
- // **********************************************************************
- // main - entry point for this console application
- //
- // Params:
- // argc - count of command line arguments
- // argv - array of NULL terminated command line arguments
- //
- // Returns: 0 for success, non-zero for failure.
- //
- // Command line: c:\>RawPrint PrinterName FileName
- // sends raw data file to printer using spooler APIs
- // written nov 1999 jmh
- //
- int main( int argc, char* argv[] )
- {
- LPBYTE pBytes = NULL;
- DWORD dwSize = 0;
- if( argc != 3 )
- return printf( TEXT("Syntax: %s <PrinterName> <FileName>\n"), argv[0] );
- printf( TEXT("Attempting to send file [%s] to printer [%s].\n"), argv[2], argv[1] );
- if( ! ReadFileWithAlloc( argv[2], &dwSize, &pBytes ) )
- return printf( TEXT("Failed to allocate memory for and read file [%s].\n"), argv[2] );
- if( ! RawDataToPrinter( argv[1], pBytes, dwSize ) )
- printf( TEXT("Failed to send data to printer.\n") );
- else
- printf( TEXT("Data sent to printer.\n") );
- GlobalFree( (HGLOBAL)pBytes );
- return 0;
- }
- // end main
- // **********************************************************************
#include <Windows.h>#include <StdIO.h>// **********************************************************************// PrintError - uses printf() to display error code information// // Params:// dwError - the error code, usually from GetLastError()// lpString - some caller-defined text to print with the error info// // Returns: void// void PrintError( DWORD dwError, LPCTSTR lpString ){#define MAX_MSG_BUF_SIZE 512TCHAR *msgBuf;DWORDcMsgLen;cMsgLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL, dwError,MAKELANGID(0, SUBLANG_ENGLISH_US), (LPTSTR) &msgBuf,MAX_MSG_BUF_SIZE, NULL );printf( TEXT("%s Error [%d]:: %s\n"), lpString, dwError, msgBuf );LocalFree( msgBuf );#undef MAX_MSG_BUF_SIZE}// end PrintError// **********************************************************************// **********************************************************************// ReadFileWithAlloc - allocates memory for and reads contents of a file// // Params:// szFileName - NULL terminated string specifying file name// pdwSize - address of variable to receive file bytes size// ppBytes - address of pointer which will be allocated and contain file bytes// // Returns: TRUE for success, FALSE for failure.//// Notes: Caller is responsible for freeing the memory using GlobalFree()// BOOL ReadFileWithAlloc( LPTSTR szFileName, LPDWORD pdwSize, LPBYTE *ppBytes ){HANDLEhFile;DWORDdwBytes;BOOLbSuccess = FALSE;// Validate pointer parametersif( ( pdwSize == NULL ) || ( ppBytes == NULL ) )return FALSE;// Open the file for readinghFile = CreateFile( szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );if( hFile == INVALID_HANDLE_VALUE ){PrintError( GetLastError(), TEXT("CreateFile()") );return FALSE;}// How big is the file?*pdwSize = GetFileSize( hFile, NULL );if( *pdwSize == (DWORD)-1 )PrintError( GetLastError(), TEXT("GetFileSize()") );else{// Allocate the memory*ppBytes = (LPBYTE)GlobalAlloc( GPTR, *pdwSize );if( *ppBytes == NULL )PrintError( GetLastError(), TEXT("Failed to allocate memory\n") );else{// Read the file into the newly allocated memorybSuccess = ReadFile( hFile, *ppBytes, *pdwSize, &dwBytes, NULL );if( ! bSuccess )PrintError( GetLastError(), TEXT("ReadFile()") );}}// Clean upCloseHandle( hFile );return bSuccess;}// End ReadFileWithAlloc// **********************************************************************// **********************************************************************// 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.// BOOL RawDataToPrinter( LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount ){HANDLE hPrinter;DOC_INFO_1 DocInfo;DWORD dwJob;DWORD dwBytesWritten;// Need a handle to the printer.if( ! OpenPrinter( szPrinterName, &hPrinter, NULL ) ){PrintError( GetLastError(), TEXT("OpenPrinter") );return FALSE;}// Fill in the structure with info about this "document."DocInfo.pDocName = TEXT("My Document");DocInfo.pOutputFile = NULL;DocInfo.pDatatype = TEXT("RAW");// Inform the spooler the document is beginning.if( (dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo )) == 0 ){PrintError( GetLastError(), TEXT("StartDocPrinter") );ClosePrinter( hPrinter );return FALSE;}// Start a page.if( ! StartPagePrinter( hPrinter ) ){PrintError( GetLastError(), TEXT("StartPagePrinter") );EndDocPrinter( hPrinter );ClosePrinter( hPrinter );return FALSE;}// Send the data to the printer.if( ! WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) ){PrintError( GetLastError(), TEXT("WritePrinter") );EndPagePrinter( hPrinter );EndDocPrinter( hPrinter );ClosePrinter( hPrinter );return FALSE;}// End the page.if( ! EndPagePrinter( hPrinter ) ){PrintError( GetLastError(), TEXT("EndPagePrinter") );EndDocPrinter( hPrinter );ClosePrinter( hPrinter );return FALSE;}// Inform the spooler that the document is ending.if( ! EndDocPrinter( hPrinter ) ){PrintError( GetLastError(), TEXT("EndDocPrinter") );ClosePrinter( hPrinter );return FALSE;}// Tidy up the printer handle.ClosePrinter( hPrinter );// Check to see if correct number of bytes were written.if( dwBytesWritten != dwCount ){printf( TEXT("Wrote %d bytes instead of requested %d bytes.\n"), dwBytesWritten, dwCount );return FALSE;}return TRUE;}// End RawDataToPrinter// **********************************************************************// **********************************************************************// main - entry point for this console application// // Params:// argc - count of command line arguments// argv - array of NULL terminated command line arguments//// Returns: 0 for success, non-zero for failure.// // Command line: c:\>RawPrint PrinterName FileName// sends raw data file to printer using spooler APIs// written nov 1999 jmh//int main( int argc, char* argv[] ){LPBYTEpBytes = NULL;DWORDdwSize = 0;if( argc != 3 )return printf( TEXT("Syntax: %s <PrinterName> <FileName>\n"), argv[0] );printf( TEXT("Attempting to send file [%s] to printer [%s].\n"), argv[2], argv[1] );if( ! ReadFileWithAlloc( argv[2], &dwSize, &pBytes ) )return printf( TEXT("Failed to allocate memory for and read file [%s].\n"), argv[2] );if( ! RawDataToPrinter( argv[1], pBytes, dwSize ) )printf( TEXT("Failed to send data to printer.\n") );elseprintf( TEXT("Data sent to printer.\n") );GlobalFree( (HGLOBAL)pBytes );return 0;}// end main// **********************************************************************
- 直接打印RAW文件到打印机(一)
- 直接打印RAW文件到打印机(二)
- 直接打印RAW文件到打印机
- 直接利用工具将prn文件打印到打印机的方法
- C#直接发送打印机命令到打印机及ZPL常用打印命令 - 条码打印机
- fastreport2.5中直接打印报表到打印机
- c#通过程序将文件直接送到打印机打印
- C# 直接打印指定路径文件 + 可选择指定打印机
- Java直接控制打印机打印
- Java直接控制打印机打印
- Java直接控制打印机打印
- 小技巧:不显示打印对话框直接打印网页到打印机
- fastreport2.5中直接打印报表到打印机 - 我的专栏 - CSDNBlog
- 从FORM中调用REPORT时,如何使打印结果直接输出到打印机上?
- C#控制打印机(直接发送打印机命令到打印机)
- 传送打印指令到打印机
- java打印表格到打印机
- 打印到TIFF, 图片打印机
- [common-sln]: base工程初步功能
- 虚拟打印机的开发----控制打印任务
- 创建单链表,并排序插入一元素后仍有序
- openssl aes 加密
- 淘宝技术发展
- 直接打印RAW文件到打印机
- 简单记录,Notification.builder的简单实用
- Fatal signal总结
- 如何阅读别人的源代码
- 使用GCD
- bex selection option variable在dashboard 4内不可用
- 职场圆周率:把握好的10个“度”
- LoadRunner参数化取值与连接数据库
- Java1.5泛型指南中文版(Java1.5 Generic Tutorial)