C/C++/MFC 获取打印机状态和控制打印机打印txt文件

wb_rock 2015-01-15 11:52:56
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("sql - 记事本");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = TEXT("RAW");
//DocInfo.pDatatype = TEXT("PMJOURNAL");
// 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;
TCHAR szDefaultPrinter[1024]={0}; // 保存默认打印机名称
DWORD dwLen = 1024;
int ret = ::GetDefaultPrinter(szDefaultPrinter, &dwLen);
printf("Default Printer Name: %s\n", szDefaultPrinter);
#if 1
// if( argc != 3 )
// return printf( TEXT("Syntax: %s <PrinterName> <FileName>\n"), argv[0] );

printf( TEXT("Attempting to send file [%s] to printer [%s].\n")
, szDefaultPrinter, "d:\\sql.txt");

// if( ! ReadFileWithAlloc( argv[2], &dwSize, &pBytes ) )
if( ! ReadFileWithAlloc("D:\\sql.txt", &dwSize, &pBytes ) )
return printf( TEXT("Failed to allocate memory for and read file [%s].\n"), "d:\\sql.txt" );
printf("[FILE]: dwSize = %d\n context = %s\n", dwSize, pBytes);

if( ! RawDataToPrinter(szDefaultPrinter, pBytes, dwSize ) )
printf( TEXT("Failed to send data to printer.\n") );
else
printf( TEXT("Data sent to printer.\n") );

GlobalFree( (HGLOBAL)pBytes );

#endif
...全文
716 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqzj70 2015-01-27
  • 打赏
  • 举报
回复
楼主的代码是用来测试打印机物理状态的
wb_rock 2015-01-16
  • 打赏
  • 举报
回复
// Send the data to the printer. // szPrinterName - NULL terminated string specifying printer name // lpData - Pointer to raw data bytes // dwCount - Length of lpData in bytes WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) lpData raw data bytes怎么理解,我用CreateFile读取的txt文件,打印不出来
赵4老师 2015-01-16
  • 打赏
  • 举报
回复
MSDN98_1.ISO http://pan.baidu.com/s/1dDF41ix, MSDN98_2.ISO http://pan.baidu.com/s/1bnGo0Vl 先下载安装MSDN98 再参考 MSDN98\SAMPLES\VC98\SDK\GRAPHICS\GDI\PRINTER\*.*
wb_rock 2015-01-15
  • 打赏
  • 举报
回复
目前程序可以把文件添加到打印机序列中,但是一直提示“正在打印”,然后过一会就完了,结果没有打印出文件。打印机状态均正常。

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧