Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
864 views
in Technique[技术] by (71.8m points)

visual c++ - Send Postscript Document to Printer using VC++

I have a postscript file. How can I send it to a printer using Visual C++? This seems like it should be simple.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the printer supports PostScript directly you can spool a raw print jobs like this:

HANDLE ph;

OpenPrinter(&ph, "Printer Name", NULL);

di1.pDatatype = IsV4Driver("Printer Name") ? "XPS_PASS" : "RAW"; 
di1.pDocName = "Raw print document";
di1.pOutputFile = NULL;

StartDocPrinter(ph, 1, (LPBYTE)&di1);

StartPagePrinter(ph);

WritePrinter(ph, buffer, dwRead, &dwWritten);

EndPagePrinter(ph);

EndDocPrinter(ph)

Repeat the WritePrinter until you have spooled the whole file.

IsV4Driver() Checks for version 4 drivers, this is necessary in Windows 8 and Server 2012:

bool IsV4Driver(wchar_t* printerName)
{
    HANDLE handle;
    PRINTER_DEFAULTS defaults;

    defaults.DesiredAccess = PRINTER_ACCESS_USE;
    defaults.pDatatype = L"RAW";
    defaults.pDevMode = NULL;

    if (::OpenPrinter(printerName, &handle, &defaults) == 0)
    {
        return false;
    }

    DWORD version = GetVersion(handle);

    ClosePrinter(handle);

    return version == 4;
}

DWORD GetVersion(HANDLE handle)
{
    DWORD needed;

    GetPrinterDriver(handle, NULL, 2, NULL, 0, &needed);

    std::vector<char> buffer(needed);

    return ((DRIVER_INFO_2*) &buffer[0])->cVersion;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...