C++在斑马标签打印机里打印

#include "stdafx.h"
#include<stdio.h>
#include <windows.h>
#include <string>
#include <vector>

#if 0
HANDLE   OpenDevice(LPGUID   pGuid, char   *outNameBuf)
{
    HANDLE   hOut = INVALID_HANDLE_VALUE;
    HDEVINFO   hardwareDeviceInfo;
    SP_INTERFACE_DEVICE_DATA   deviceInfoData;

    hardwareDeviceInfo = SetupDiGetClassDevs(pGuid, NULL, NULL, (DIGCF_PRESENT | DIGCF_INTERFACEDEVICE));

    deviceInfoData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

    if (SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0, pGuid, 0, &deviceInfoData))
    {
        hOut = OpenOneDevice(hardwareDeviceInfo, &deviceInfoData, outNameBuf);

        if (hOut == INVALID_HANDLE_VALUE)
            return   INVALID_HANDLE_VALUE;
    }

    DWORD   reErr = GetLastError();

    SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);

    return   hOut;
}

HANDLE   OpenOneDevice(HDEVINFO   HardwareDeviceInfo, PSP_INTERFACE_DEVICE_DATA   DeviceInfoData, char   *devName)
{
    PSP_INTERFACE_DEVICE_DETAIL_DATA   functionClassDeviceData = NULL;
    ULONG   predictedLength = 0;
    ULONG   requiredLength = 0;
    HANDLE   hOut = INVALID_HANDLE_VALUE;

    //   allocate   a   function   class   device   data   structure   to   receive   the   goods   about   this   particular   device.
    SetupDiGetInterfaceDeviceDetail(HardwareDeviceInfo, DeviceInfoData, NULL, 0, &requiredLength, NULL);

    predictedLength = requiredLength;
    //   sizeof   (SP_FNCLASS_DEVICE_DATA)   +   512;

    functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(predictedLength);
    functionClassDeviceData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

    //   Retrieve   the   information   from   Plug   and   Play.
    if (!SetupDiGetInterfaceDeviceDetail(
        HardwareDeviceInfo,
        DeviceInfoData,
        functionClassDeviceData,
        predictedLength,
        &requiredLength,
        NULL))
    {
        free(functionClassDeviceData);
        return   INVALID_HANDLE_VALUE;
    }

    strcpy(devName, functionClassDeviceData->DevicePath);
    printf("Attempting   to   open   %s ", devName);

    hOut = CreateFile(functionClassDeviceData->DevicePath,
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);

    if (INVALID_HANDLE_VALUE == hOut)
        printf("FAILED   to   open   %s ", devName);

    free(functionClassDeviceData);

    return   hOut;
}


HANDLE   hDEV = OpenDevice((LPGUID)&GUID_BUS_UMSS, completeDeviceName);
if (hDEV == INVALID_HANDLE_VALUE)
return;

DeviceIoControl(
    hDEV,
    IOCTL_Vender_VCMD_WRITE,
    buf,
    31,   //CBW
    buf,
    13,   //   Data   +   CSW
    (LPDWORD)&nBytes,
    NULL
    );
#endif

int printrjtlabel( const char * zebraPrintername,const char * invdate,
    const char * listno,const char * sellerlistno,const char * supno,const char * supname)
{
    LPTSTR szPrinterName;
    LPBYTE lpData;
    DWORD dwCount;
    HANDLE     hPrinter;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob;
    DWORD      dwBytesWritten;


    wchar_t * zebraPrinternamew = new wchar_t[strlen(zebraPrintername) + 1];
    mbstowcs_s(NULL, zebraPrinternamew, strlen(zebraPrintername) + 1, zebraPrintername, strlen(zebraPrintername));

    //    lpData = (LPBYTE)("^XA ^LH0,0 ^FO300,203 ^BY3,2.4,50 ^B3N,Y,,Y,N ^FDABC123^FS ^XZ   ");
    //    dwCount = strlen("^XA ^LH0,0 ^FO300,203 ^BY3,2.4,50 ^B3N,Y,,Y,N ^FDABC123^FS ^XZ   ");

    /*lpData = (LPBYTE)("^XA^LH0,0^XZ "
        "^XA^FO35,35^A0,50,50^FD "
        "^FS "
        "^CI26 "
        "^SEE:GB18030.DAT "
        "^CW1,E:SIMSUN.FNT "
        "^FO250,10^A0,25,25^FDZebra Technologies^FS "
        "^FO250,40^A0,25,25^FDVernon Hills, IL^FS "
        "^FO250,70^A0,25,25^FD847-913-2259^FS "
        "^FO250,100^A1N,25,25^FD中文^FS "
        "^XZ");
    dwCount = strlen("^XA^LH0,0^XZ "
        "^XA^FO35,35^A0,50,50^FD "
        "^FS "
        "^CI26 "
        "^SEE:GB18030.DAT "
        "^CW1,E:SIMSUN.FNT "
        "^FO250,10^A0,25,25^FDZebra Technologies^FS "
        "^FO250,40^A0,25,25^FDVernon Hills, IL^FS "
        "^FO250,70^A0,25,25^FD847-913-2259^FS "
        "^FO250,100^A1N,25,25^FD中文^FS "
        "^XZ");*/

    std::string printcontents = "^XA^LH0,0^XZ "
        "^XA^FO35,35^A0,50,50^FD "
        "^FS "
        "^CI26 "
        "^SEE:GB18030.DAT "
        "^CW1,E:SIMSUN.FNT ";
        //    "^FO250,10^A0,25,25^FDZebra Technologies^FS "
        //    "^FO250,40^A0,25,25^FDVernon Hills, IL^FS "
        //    "^FO250,70^A0,25,25^FD847-913-2259^FS "
     std::string invdatestr = "^FO250,10^A1N,25,25^FD日期:" + std::string(invdate);
     invdatestr = invdatestr + "^FS ";

     std::string listnostr = "^FO250,40^A1N,25,25^FD单号:" + std::string(listno);
     listnostr = listnostr + "^FS ";

     std::string sellerlistnostr = "^FO250,70^A1N,25,25^FD对单:" + std::string(sellerlistno);
     sellerlistnostr = sellerlistnostr + "^FS ";

     std::string supnostr = "^FO250,100^A1N,25,25^FD编码:" + std::string(supno);
     supnostr = supnostr + "^FS ";

     std::string supnamestr = "^FO250,130^A1N,25,25^FD供应商:^FS ";

     int j = 130;
     std::string tmpsupname;
     std::string accumlatesupname;
    
     for (int i = 0; i < strlen(supname); i++)
     {
         accumlatesupname = accumlatesupname+ std::string(supname, i, 1);
         
         if (i % 19 == 0 && i>0)
         {
             
             tmpsupname = tmpsupname+ "^FO350," + std::to_string(j);
             tmpsupname = tmpsupname + "^A1N,25,25^FD";
             tmpsupname = tmpsupname + accumlatesupname;
             tmpsupname = tmpsupname + "^FS ";
             accumlatesupname = "";
             
             j = j + 30;

             
         }
     }

     if (strlen(supname) % 19 != 0)
     {
         tmpsupname = tmpsupname+"^FO350," + std::to_string(j);
         tmpsupname = tmpsupname + "^A1N,25,25^FD";
         tmpsupname = tmpsupname + accumlatesupname;
         tmpsupname = tmpsupname + "^FS ";
         accumlatesupname = "";
         j = j + 30;
     }
    

        std::string endstr = "^XZ";

        printcontents = printcontents + invdatestr;
        printcontents = printcontents + listnostr;
        printcontents = printcontents + sellerlistnostr;
        printcontents = printcontents + supnostr;
        printcontents = printcontents + supnamestr;
        printcontents = printcontents + tmpsupname;
        printcontents = printcontents + endstr;


        lpData = (LPBYTE)printcontents.c_str();
        dwCount = strlen(printcontents.c_str());

    // Need a handle to the printer. 
    //szPrinterName = _T("ZDesigner GK888t (EPL)");
    szPrinterName = zebraPrinternamew;
    if (!OpenPrinter(szPrinterName, &hPrinter, NULL))
    {

        return -1;
    }

    // Fill in the structure with info about this "document."  
    DocInfo.pDocName = TEXT("My Document");
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = TEXT("RAW");
    // Inform the spooler the document is beginning.  
    if ((dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo)) == 0)
    {

        ClosePrinter(hPrinter);
        return -2;
    }
    // Start a page.  
    if (!StartPagePrinter(hPrinter))
    {

        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return -3;
    }
    // Send the data to the printer.  
    if (!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten))
    {

        EndPagePrinter(hPrinter);
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return -4;
    }
    // End the page.  
    if (!EndPagePrinter(hPrinter))
    {

        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return -5;
    }
    // Inform the spooler that the document is ending.  
    if (!EndDocPrinter(hPrinter))
    {

        ClosePrinter(hPrinter);
        return -6;
    }
    // Tidy up the printer handle.  
    ClosePrinter(hPrinter);
    // Check to see if correct number of bytes were written.  
    if (dwBytesWritten != dwCount)
    {

        return -7;
    }
    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    const char * zebraPrintername = "ZDesigner GK888t (EPL)";
    const char * invdate = "2020-06-01";
    const char * listno = "DEP2020601ZE003";
    const char * sellerlistno = "XSCK202005042038";
    const char * supno = "G1006";
    const char * supname = "测试有限公司";
    
    int result = printrjtlabel(zebraPrintername, invdate,
        listno, sellerlistno, supno, supname);
    return 0;
}

原文地址:https://www.cnblogs.com/redmondfan/p/14251395.html