C++打印机不想打印
C++ printer don't want to print
我的代码有错误。我的打印机名称为"XP-58"无法从C++程序中打印文本。但是当我从 notepat 运行打印时,它可以正常工作。我的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 <<结果<<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);
}
打印机不知道您已完成所有内容的发送,因此不会打印。
要完成一行,您需要将"rn"
字符添加到字符串中。
完成页面后,将"f"
添加到字符串中。
相关文章:
- 如何打印不带空格的输入整数?
- cout 打印不准确的结果,printf 打印准确的结果
- C++:犰狳不想与hdf5格式合作
- 在 Windows 上打印不起作用
- Libnoise 不想编译:找不到 -lnoise
- 雪碧不想合作
- 我的c ++数组值不想改变,它输出无限循环
- Ubuntu上的库ICU不想从Unicode转换为Windows-1251
- 递归 cin.getline() 不想要求输入
- 菱形打印不正确
- 我不想导出的函数的未记录代码对象警告
- C++11 监视器类,测试应用程序不想退出
- 排序向量不断修改数据并打印不正确的结果
- 类构造函数,它接受对不同对象的引用,我不想复制该对象
- 我不想要最后一个数字/值后面的逗号,如何删除它(c ++,循环)
- C++字符串到字符串打印不正确,无法获取 unicode 路径
- 如何将类型名传递给我不想模板化的类?
- C++打印机不想打印
- 程序不会打印我想要的所有内容
- 能够编译和运行.然而,当运行代码时,它不能打印我想打印的任何值