Microsoft's simple printer code won't work on Vista Home Premium,
where do I go now?
I am writing an MFC-based app that uses an Epson TM-T88IV receipt
printer. Printing to the printer works fine on XP and Vista Home
Basic, but doesn't work on Vista Home Premium. I've now compiled the
Microsoft "Hello World" print code below with the same results -
prints on XP and VHB, doesn't print on VHP. The printer window shows
the print job spooling but not printing.
Epson says it's not their fault because the driver "Print Test Page"
works on VHP, so now I'm stuck. BTW, other apps do print to the
printer in VHP, e.g. Word, Excel. Just not either program compiled
under MSVC++ (tried both debug and release configs). Any ideas,
reactions, similar experiences?
Here's the bare-minimum code that doesn't work, taken from the
CDC::StartDoc doc page:
// get the default printer
CPrintDialog dlg(FALSE);
dlg.GetDefaults();
// is a default printer set up?
HDC hdcPrinter = dlg.GetPrinterDC();
if (hdcPrinter == NULL)
{
MessageBox(_T("Buy a printer!"));
}
else
{
// create a CDC and attach it to the default printer
CDC dcPrinter;
dcPrinter.Attach(hdcPrinter);
// call StartDoc() to begin printing
DOCINFO docinfo;
memset(&docinfo, 0, sizeof(docinfo));
docinfo.cbSize = sizeof(docinfo);
docinfo.lpszDocName = _T("CDC::StartDoc() Code Fragment");
// if it fails, complain and exit gracefully
if (dcPrinter.StartDoc(&docinfo) < 0)
{
MessageBox(_T("Printer wouldn't initalize"));
}
else
{
// start a page
if (dcPrinter.StartPage() < 0)
{
MessageBox(_T("Could not start page"));
dcPrinter.AbortDoc();
}
else
{
// actually do some printing
CGdiObject* pOldFont =
dcPrinter.SelectStockObject(SYSTEM_FONT);
dcPrinter.TextOut(50, 50, _T("Hello World!"), 12);
dcPrinter.EndPage();
dcPrinter.EndDoc();
dcPrinter.SelectObject(pOldFont);
}
}
}