Some Printer Drivers can cause your App to crash

Louis Solomon / SteelBytes
14/Feb/05

Calling CreateDC()/DeleteDC() or PrintDlg() can upset and even crash an application that relies on the floating point control word, to not spontaeneously change!
Wrapping calls to these functions in calls to _control87() to save and restore the FPU control word, cures this.

{
	PRINTDLGW pd;
	...
	unsigned fpstate = _control87(0,0);
	BOOL res = PrintDlgW(&pd);
	if (_control87(0,0)!=fpstate) { _control87(fpstate,0xffffffff); TRACE(L"bad printer driver?\n"); }
}
{
	DEVNAMES *devNamesW;
	DEVMODEW *devMode;
	...
	unsigned fpstate = _control87(0,0);
	HDC pdc = CreateDCW(
		(WCHAR *)((BYTE*)devNamesW + devNamesW->wDriverOffset*sizeof(WCHAR))
		,(WCHAR *)((BYTE*)devNamesW + devNamesW->wDeviceOffset*sizeof(WCHAR))
		,(WCHAR *)((BYTE*)devNamesW + devNamesW->wOutputOffset*sizeof(WCHAR))
		,devMode);
	if (_control87(0,0)!=fpstate) { _control87(fpstate,0xffffffff); TRACE(L"bad printer driver?\n"); }
	if (pdc!=NULL)
	{
		...
		DeleteDC(pdc);
		if (_control87(0,0)!=fpstate) { _control87(fpstate,0xffffffff); TRACE(L"bad printer driver?\n"); }
	}
}