void CBCGPRibbonBackstagePagePrint::OnSelectPrinter() 
{
	CWinApp* pApp = AfxGetApp();
	if (pApp == NULL)
	{
		ASSERT(FALSE);
		return;
	}

	int nSelection = GetPrinterSelection ();
	if (nSelection < 0)
	{
		ASSERT(FALSE);
		return;
	}

	PRINTDLG* dlgPrint = GetPrintDlg();
	ASSERT(dlgPrint != NULL);

	LPDEVNAMES lpDevNames = NULL;
	if (dlgPrint->hDevNames != NULL)
	{
		lpDevNames = (LPDEVNAMES)::GlobalLock(dlgPrint->hDevNames);
		ASSERT(lpDevNames != NULL);
	}

	XPrinterInfo& info = m_Printers[nSelection];

 	if (lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset, info.strDriverName) != 0 ||
 		lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset, info.strPrinterName) != 0 ||
 		lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wOutputOffset, info.strPortName) != 0)
	{
		::GlobalUnlock(dlgPrint->hDevNames);

		// Compute size of DEVNAMES structure from PRINTER_INFO_2's data
		int drvNameLen = info.strDriverName.GetLength () + 1;  // driver name
		int ptrNameLen = info.strPrinterName.GetLength () + 1; // printer name
		int porNameLen = info.strPortName.GetLength () + 1;    // port name

		// Allocate a global handle big enough to hold DEVNAMES.
		HGLOBAL hDevNames = GlobalAlloc(GHND, sizeof(DEVNAMES) + (drvNameLen + ptrNameLen + porNameLen) * sizeof(TCHAR));
		DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames);
		ASSERT(pDevNames != NULL);

		// Copy the DEVNAMES information from PRINTER_INFO_2
		// tcOffset = TCHAR Offset into structure
		int tcOffset = sizeof(DEVNAMES) / sizeof(TCHAR);
		ASSERT(sizeof(DEVNAMES) == tcOffset * sizeof(TCHAR));

		pDevNames->wDriverOffset = (WORD)tcOffset;
		memcpy((LPTSTR)pDevNames + tcOffset, (LPCTSTR)info.strDriverName, drvNameLen * sizeof(TCHAR));
		tcOffset += drvNameLen;

		pDevNames->wDeviceOffset = (WORD)tcOffset;
		memcpy((LPTSTR)pDevNames + tcOffset, (LPCTSTR)info.strPrinterName, ptrNameLen * sizeof(TCHAR));
		tcOffset += ptrNameLen;

		pDevNames->wOutputOffset = (WORD)tcOffset;
		memcpy((LPTSTR)pDevNames + tcOffset, (LPCTSTR)info.strPortName, porNameLen * sizeof(TCHAR));
		pDevNames->wDefault = 0;

		::GlobalUnlock(hDevNames);

		dlgPrint->hDevNames = hDevNames;

		pApp->SelectPrinter(dlgPrint->hDevNames, NULL, TRUE);
		pApp->DevModeChange((LPTSTR)(LPCTSTR)info.strPrinterName);
		pApp->GetPrinterDeviceDefaults (dlgPrint);

		short dmPaperSize = 0;
		if (dlgPrint->hDevMode != NULL)
		{
			LPDEVMODE lpDevMode = (LPDEVMODE)::GlobalLock (dlgPrint->hDevMode);
			dmPaperSize = lpDevMode->dmPaperSize;
			::GlobalUnlock (dlgPrint->hDevMode);
		}

		BOOL bNotify = m_wndPaper.GetCount () > 0;

		UpdatePrinterProperties(TRUE);
		UpdatePapers (dmPaperSize);
		UpdatePrinterProperties(FALSE, bNotify);
	}
}