[英]Windows GDI Printer error StartDocPrinter
Actually I am working with Qt printing and need to send the RAW command (ESCP command) to printer. 实际上我正在使用Qt打印,需要将RAW命令(ESCP命令)发送到打印机。 After did some search and I found that I need to use the Windows API to do it.
经过一些搜索,我发现我需要使用Windows API来完成它。
Refer to this thread : win32-c-print-string-to-printer 请参阅此主题: win32-c-print-string-to-printer
I have created the code like below : 我创建了如下代码:
const QString pName = "EPSON LX-300+ /II";
LPBYTE lpData;
BOOL bStatus = FALSE;
HANDLE hPrinter = NULL;
DOC_INFO_1 DocInfo;
DWORD dwPrtJob = 0L;
DWORD dwBytesWritten = 0L;
LPTSTR printerName = new wchar_t[pName.length() + 1];
pName.toWCharArray(printerName);
printerName[pName.length()] = '\0';
QString so = "\x1b@Is it works?";
QByteArray ba = so.toUtf8();
lpData = (unsigned char*)(ba.data());
DWORD dwCount = ba.length();
qDebug() << so;
bStatus = OpenPrinter(printerName, &hPrinter, NULL);
if(bStatus) {
DocInfo.pDocName = (LPTSTR)_T("My Document");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = (LPTSTR)_T("RAW");
dwPrtJob = StartDocPrinter (
hPrinter,
1,
(LPBYTE)&DocInfo);
qDebug() << GetLastError();
if (dwPrtJob > 0) {
qDebug() << "COMMAND";
// Send the data to the printer.
bStatus = WritePrinter (
hPrinter,
lpData,
dwCount,
&dwBytesWritten);
}
qDebug() << dwCount;
qDebug() << dwBytesWritten;
EndDocPrinter (hPrinter);
// Close the printer handle.
bStatus = ClosePrinter(hPrinter);
qDebug() << bStatus;
}
if (!bStatus || (dwCount != dwBytesWritten)) {
bStatus = FALSE;
} else {
bStatus = TRUE;
}
delete printerName;
And the code failed on StartDocPrinter, it returning 0 which mean failed. 并且代码在StartDocPrinter上失败,它返回0表示失败。 And using the GetLastError(), the function return 1804. And refer to this , the error is ERROR_INVALID_DATATYPE.
并使用GetLastError(),函数返回1804.并参考此 ,错误是ERROR_INVALID_DATATYPE。 I am not sure what error is it.
我不确定它是什么错误。 And i try to use different DocInfo.pDatatype to "RAW", "TEXT", and "XPS_PASS", the result is the same.
我尝试使用不同的DocInfo.pDatatype来“RAW”,“TEXT”和“XPS_PASS”,结果是一样的。
Is there anything I can do how to fix it? 我有什么办法可以解决它吗?
DOC_INFO_1 DocInfo;
DocInfo.pDocName = (LPTSTR)_T("My Document");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = (LPTSTR)_T("RAW");
This casting boils down to 这个演员归结为
DocInfo.pDocName = (wchar_t*)L"My Document";
...
Where pDocName
is declared as wchar_t*
This is wrong and the cast simply hides the errors and warnings. 其中
pDocName
被声明为wchar_t*
这是错误的,并且pDocName
只是隐藏错误和警告。 Try to avoid Microsoft T
macro altogether, these macros are outdated and useless. 尽量避免使用Microsoft
T
宏,这些宏已经过时且无用。 Use C++ char
and wchar_t*
instead. 请改用C ++
char
和wchar_t*
。 To declare UTF16 wide char string in Windows, use the L
prefix. 要在Windows中声明UTF16宽字符串,请使用
L
前缀。 The correct usage is as follows: 正确用法如下:
DOC_INFO_1 DocInfo;
wchar_t docName[100], dataType[100];
wcscpy_s(docName, 100, L"Print Job");
wcscpy_s(dataType, 100, L"RAW");
DocInfo.pDocName = docName;
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = dataType;
Example: 例:
const QString pName = "EPSON LX-300+ /II";
wchar_t printerName[100];
pName.toWCharArray(printerName);
pName[pName.length()] = '\0';
HANDLE hprinter;
if (OpenPrinter(printerName, &hprinter, NULL))
{
DOC_INFO_1 DocInfo;
wchar_t docName[100], dataType[100];
wcscpy_s(docName, 100, L"Print Job");
wcscpy_s(dataType, 100, L"RAW");
DocInfo.pDocName = docName;
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = dataType;
DWORD printJob = StartDocPrinter(hprinter, 1, (LPBYTE)&DocInfo);
if (printJob && StartPagePrinter(hprinter))
{
MessageBox(0, L"StartPagePrinter OKAY", 0, 0);
DWORD written = 0;
int buflen = 100;
char *buf = new char[buflen];
strcpy_s(buf, buflen, "123");
if (WritePrinter(hprinter, buf, 3, &written))
MessageBox(0, L"OKAY", 0, 0);
delete[]buf;
EndPagePrinter(hprinter);
EndDocPrinter(hprinter);
}
ClosePrinter(hprinter);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.