Click to See Complete Forum and Search --> : CreateDC for printer fails...


GNiewerth
December 1st, 2006, 11:21 AM
Hi,

there are two printers installed on my system, a physically attached HP Deskjet 970c and a virtual PDF printer (FreePDF XP). I want to create a HDC for the standard printer to retrieve its configuration.
I´ve got a very strange behaviour with my code: I fail in creating a HDC for a printer and GetLastError() returns error code 122 (buffer too small). What the heck can that be? For another printer, however, my code succeeds...

Here´s my code:

void CPrinterPage::Init()
HDC hPrinterDC;
DEVMODE *pDevMode;
DEVNAMES *pDevNames;
PRINTDLG PrintDlg;

::ZeroMemory( &PrintDlg, sizeof( PRINTDLG ) );
PrintDlg.lStructSize = sizeof( PRINTDLG );
PrintDlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;

if( false == ::PrintDlg( &PrintDlg ) )
{
// throw Exception
throw Exception( "TPrinterPage::GetPrinterCapabilities: Unable find default printer" );
}
// Pointer fixieren
pDevMode = (DEVMODE *) ::GlobalLock( PrintDlg.hDevMode );
pDevNames = (DEVNAMES *) ::GlobalLock( PrintDlg.hDevNames );

// Auflösungen bestimmen
m_dXResolution = pDevMode->dmPrintQuality;
m_dYResolution = pDevMode->dmYResolution;

// Drucker- und Treibernamen merken
m_strDriverName = ((char *) pDevNames) + pDevNames->wDriverOffset;
m_strDeviceName = ((char *) pDevNames) + pDevNames->wDeviceOffset;

// Pointer freigeben
::GlobalUnlock( PrintDlg.hDevMode );
::GlobalUnlock( PrintDlg.hDevNames );
::GlobalFree( PrintDlg.hDevMode );
::GlobalFree( PrintDlg.hDevNames );

// ALWAYS fails for HP Deskjet
hPrinterDC = ::CreateDC( m_strDriverName.c_str(),
m_strDeviceName.c_str(),
NULL,
NULL );
}


Both PrintDlg(...) and CreateDC(...) fail to yield a valid device context, the PrintDlg hDC member is NULL though I specified PD_RETURNDC (also tried PD_RETURNIC). CreateDC returns NULL and a subsequent call of GetLastError returns 122.

These are the device/driver names I use to CreateDC:


// successful call
::CreateDC( "winspool", "FreePDF XP", NULL, NULL );

// failing call
::CreateDC( "winspool", "HP Deskjet", NULL, NULL );


Any help is aprreciated,
Guido

GNiewerth
December 2nd, 2006, 12:27 PM
Hello again,

I reinstalled the printer drivers and now everything´s fine o.O

Guido