C++打印机不想打印

标签 c++ windows printing

我的代码有错误。我的名为“XP-58”的打印机不打印来自 C++ 程序的文本。 但是当我从记事本运行打印时,它可以正常工作。 我的 C++ 代码

int _tmain(int argc, _TCHAR* argv[])
{
    LPTSTR printerName = (LPTSTR)_T("XP-58");
    CString str = "la-la-la";
    LPBYTE pByte = new BYTE[str.GetLength() + 1];
    memcpy(pByte, (VOID*)LPCTSTR(str), str.GetLength());
    DWORD count = 7;
    BOOL result = RawDataToPrinter(printerName, pByte, count);
    std::cout << result << std::endl;
    system("pause");
    return 0;
}

我使用这里的函数 https://msdn.microsoft.com/en-us/library/windows/desktop/dd162959(v=vs.85).aspx

如您所见,我在程序结束后得到结果“std::cout << result << std::endl;”结果总是显示“1”。

问题是什么? 可能我需要为打印机设置指定端口?我的打印机连接到 USB002 端口。 当我从记事本开始打印时,我在“打印队列管理器”中看到了这个端口,但是当从我的程序添加任务时,我在管理器中看不到任何端口。 请帮忙)

完整代码

#include "stdafx.h"
#include <Windows.h>
#include <Winspool.h>
#include <CommDlg.h>
#include <math.h>
#include <atlstr.h>
#include <iostream>


BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
    BOOL     bStatus = FALSE;
    HANDLE     hPrinter = NULL;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob = 0L;
    DWORD      dwBytesWritten = 0L;

    // Open a handle to the printer. 
    bStatus = OpenPrinter( szPrinterName, &hPrinter, NULL );
    if (bStatus) {
        // Fill in the structure with info about this "document." 
        DocInfo.pDocName = (LPTSTR)_T("chargebox barcode check");
        DocInfo.pOutputFile = NULL;
        DocInfo.pDatatype = (LPTSTR)_T("RAW");

        // Inform the spooler the document is beginning. 
        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
        if (dwJob > 0) {
            // Start a page. 
            bStatus = StartPagePrinter( hPrinter );
            if (bStatus) {
                // Send the data to the printer. 
                bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);
                EndPagePrinter (hPrinter);
            }
            // Inform the spooler that the document is ending. 
            EndDocPrinter( hPrinter );
        }
        // Close the printer handle. 
        ClosePrinter( hPrinter );
    }
    // Check to see if correct number of bytes were written. 
    if (!bStatus || (dwBytesWritten != dwCount)) {
        bStatus = FALSE;
    } else {
        bStatus = TRUE;
    }
    return bStatus;
}


int _tmain(int argc, _TCHAR* argv[])
{
    LPTSTR printerName = (LPTSTR)_T("XP-58");
    CStringW str = L"unicode";
    int bytelen = 2 * str.GetLength();
    LPBYTE pByte = new BYTE[bytelen];
    memcpy(pByte, str, bytelen);
    DWORD count = bytelen;
    BOOL result = RawDataToPrinter(printerName, pByte, count);
    std::cout << GetLastError() << std::endl;
    std::cout << result << std::endl;
    system("pause");
    return(0);
}

最佳答案

打印机不知道您已发送完所有内容,因此不会打印。

要结束一行,您需要在字符串中添加字符"\r\n"

当您完成一个页面时,将 "\f" 添加到字符串中。

关于C++打印机不想打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29601374/

相关文章:

C++ 监视变量的更改值

C++ STL--以 pair<int,string> 为数据类型的 make_heap

windows - MFC:地址栏控件,如 Windows 资源管理器

java - 如何在 Android 应用程序中指定和添加自定义打印机?

java - 从 Java 打印 - 打印作业永远挂起

c++ - 如何渲染到不同的层,然后将它们与 OpenGL 组合在一起?

C++11 延迟 "thread"创建(即指定线程函数但不等待创建物理线程)

windows - 如何在此PowerShell代码中添加异常处理?

regex - 连字符和撇号字符 - 不同语言的相同 ASCII 代码?

Haskell如何打印可变向量