0
votes

I have mistakes in my code. My printer which name is "XP-58" does not print text from C++ program. But when I run print from notepat, it works correctrly. My C++ code

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;
}

I using function from here https://msdn.microsoft.com/en-us/library/windows/desktop/dd162959(v=vs.85).aspx

As you can see I have result after program end "std::cout << result << std::endl;" and result always shows "1".

Which is the problem? May be I need set specify port for printer? My printer connected to USB002 port. And when I start printing from notepad I see this port in "Print queue manager", but when task added from my program I don't see any port in manager. Please help)

Full code

#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);
}
1
GetLastError should help you.weltensturm

1 Answers

3
votes

The printer doesn't know that you're finished sending it everything, so it doesn't print.

To finish a line, you need to add the characters "\r\n" to the string.

When you're finished with a page, add "\f" to the string.