Показать сообщение отдельно
Старый 13.06.2018, 15:09   #6  
someOne is offline
someOne
Участник
Аватар для someOne
 
172 / 409 (14) +++++++
Регистрация: 11.12.2008
Адрес: Москва
Написал в итоге скрипт (без использования сторонних библиотек, только WinApi) который позволяет из кода Ax печатать командами ZPL/EPL напрямую в принтер этикеток (ZEBRA например) через драйвер принтера без использования COM порта (через порт USB например)


X++:
static void Job364(Args _args)
{
    int g;
    
    Binary      DOC_INFO_1;
    Binary      bytes;
    Binary      hPrinter        = new Binary(260);
    Binary      printerNamePtr  = new Binary(260);
    Binary      pcWritten;

    int i;
    int v;

    DLL         winSpool = new DLL('WINSPOOL.DRV');

    DLLFunction openPrinter         = new DLLFunction(winSpool,'OpenPrinterA');
    DLLFunction closePrinter        = new DLLFunction(winSpool,'ClosePrinter');

    DLLFunction startDocPrinter     = new DLLFunction(winSpool,'StartDocPrinterA');
    DLLFunction endDocPrinter       = new DLLFunction(winSpool,'EndDocPrinter');

    DLLFunction startPagePrinter    = new DLLFunction(winSpool,'StartPagePrinter');
    DLLFunction endPagePrinter      = new DLLFunction(winSpool,'EndPagePrinter');

    DLLFunction writePrinter        = new DLLFunction(winSpool,'WritePrinter');


    int             handle;
    str             printerName = "\\\\itdev2\\ZDesigner ZD410-203dpi ZPL";

//    str printerName = "Microsoft XPS Document Writer";

    int ok;
    str txt2print;
    str err;
    ;
    openPrinter.returns(ExtTypes::DWORD);    //  BOOL OpenPrinter(
    openPrinter.arg(ExtTypes::Pointer);      //  LPTSTR pPrinterName,         // printer or server name
    openPrinter.arg(ExtTypes::Pointer);      //  LPHANDLE phPrinter,          // printer or server handle
    openPrinter.arg(ExtTypes::Pointer);      //  LPPRINTER_DEFAULTS pDefault)  // printer defaults

    closePrinter.returns(ExtTypes::DWORD);
    closePrinter.arg(ExtTypes::DWORD); // hPrinter

    startDocPrinter.returns(ExtTypes::DWORD);
    startDocPrinter.arg(ExtTypes::DWORD); // hPrinter
    startDocPrinter.arg(ExtTypes::DWORD); // Level
    startDocPrinter.arg(ExtTypes::Pointer); // LPBYTE pDocInfo

    endDocPrinter.returns(ExtTypes::DWORD);
    endDocPrinter.arg(ExtTypes::DWORD); // hPrinter

    startPagePrinter.returns(ExtTypes::DWORD);
    startPagePrinter.arg(ExtTypes::DWORD); // hPrinter

    endPagePrinter.returns(ExtTypes::DWORD);
    endPagePrinter.arg(ExtTypes::DWORD); // hPrinter

    writePrinter.returns(ExtTypes::DWORD);
    writePrinter.arg(ExtTypes::DWORD);  // _In_  HANDLE  hPrinter,
    writePrinter.arg(ExtTypes::Pointer); //  _In_  LPVOID  pBuf,
    writePrinter.arg(ExtTypes::DWORD);  // _In_ cbBuf,
    writePrinter.arg(ExtTypes::Pointer);  //   _Out_ LPDWORD pcWritten

    DOC_INFO_1 = new Binary(16);

txt2print = @'
N
US
D10
ZT
Q240,24
q456
R08,08
TDdd.mn.y4
A10,0,0,a,1,1,N,"Name Название"
A10,27,0,a,1,1,N,"Name2 Товара"
A10,50,0,b,1,1,N,"TransRefId ссылка"
A200,50,0,b,1,1,N,"Кол-во "
A235,140,0,c,1,1,N,"ManufacId-Тра"
B10,110,0,1,2,4,75,B,"12121212"
P1
'   ;
    printerNamePtr.string(0, printerName);

    ok = openPrinter.call(printerNamePtr, hPrinter, 0);

    if (ok != 1)
    {
        throw error(strFmt("Can not open printer '%1'", printerName));
    }

    handle = hPrinter.dWord(0);

    for (g = 1; g <= 3; g++)
    {
        ok = startDocPrinter.call(handle, 1, DOC_INFO_1);

        if (ok == 0)
        {
            err = WinApi::formatMessage(winapi::getLastError());

            closePrinter.call(handle);

            throw error(err);
        }

        ok = startPagePrinter.call(handle);

        if (ok == 0)
        {
            err = WinApi::formatMessage(winapi::getLastError());

            endDocPrinter.call(handle);
            closePrinter.call(handle);

            throw error(err);
        }

        pcWritten = new Binary(8);

        bytes = new Binary(strLen(txt2print) + 2);
        bytes.string(0, txt2print);

        ok = writePrinter.call(handle, bytes, strLen(txt2print), pcWritten);

        if (ok == 0)
        {
            err = WinApi::formatMessage(winapi::getLastError());

            endPagePrinter.call(handle);
            endDocPrinter.call(handle);
            closePrinter.call(handle);

            throw error(err);
        }

        ok = endPagePrinter.call(handle);
        ok = endDocPrinter.call(Handle);
    }

    closePrinter.call(handle);
}