mardi 28 juin 2016

Windows GDI Printer error StartDocPrinter

Actually I am working with Qt printing and need to send the RAW command (ESCP command) to printer. After did some search and I found that I need to use the Windows API to do it.

Refer to this thread : 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()] = '�';
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. And using the GetLastError(), the function return 1804. And refer to this, the error is 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.

Is there anything I can do how to fix it?

Aucun commentaire:

Enregistrer un commentaire