首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows GDI打印机错误StartDocPrinter

Windows GDI打印机错误StartDocPrinter
EN

Stack Overflow用户
提问于 2016-06-24 22:27:51
回答 1查看 1.9K关注 0票数 1

实际上,我正在使用Qt打印,需要将RAW命令(ESCP命令)发送到打印机。在做了一些搜索之后,我发现我需要使用Windows来完成它。

请参阅以下线程:win32 32-c-打印字符串到打印机

我创建了如下代码:

代码语言:javascript
复制
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;

代码在StartDocPrinter上失败,它返回0,这意味着失败。使用GetLastError(),函数返回1804。参考,错误是ERROR_INVALID_DATATYPE。我不知道这是什么错误。我尝试使用不同的DocInfo.pDatatype来"RAW“、"TEXT”和"XPS_PASS",结果是一样的。

我能做些什么来修理它吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-25 23:17:42

代码语言:javascript
复制
DOC_INFO_1 DocInfo;
DocInfo.pDocName = (LPTSTR)_T("My Document");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = (LPTSTR)_T("RAW");

这个铸造可以归结为

代码语言:javascript
复制
DocInfo.pDocName = (wchar_t*)L"My Document";
...

如果pDocName被声明为wchar_t*,这是错误的,而强制转换只是隐藏错误和警告。尽量避免微软的T宏,这些宏是过时的和无用的。改用C++ charwchar_t*。若要在UTF16中声明L宽字符字符串,请使用L前缀。正确的用法如下:

代码语言:javascript
复制
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;

示例:

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38022965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档