Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...