i want to set paper size on printer preferences UI iam getting error response = -2147024809
C / C++ / MFC
1
Posts
1
Posters
13
Views
-
hi All,
i want to set paper size on printer preferences UI iam getting error response = -2147024809
void CMFCApplication2Dlg::TestFinal()
{
// --- A. Define Printer Name and Paper Size ---
CString strPrinterName = _T("Microsoft Print to PDF");
const WORD nPaperEnumA4 = 9; // DMPAPER_A4CPrintDialogEx dlg; // --- B. Get hDevNames for the Selected Printer using the Helper --- HGLOBAL hDevNames = GetDevNamesForPrinter(strPrinterName); // Assumes this helper is correct if (hDevNames == NULL) { AfxMessageBox(_T("Failed to get hDevNames for the printer.")); return; } // --- C. Get and Modify the DEVMODE for the Printer (A4 Enforcement) --- HGLOBAL hDevMode = NULL; DEVMODE* pDevMode = NULL; // 1. Get required size long lSize = ::DocumentProperties(NULL, NULL, strPrinterName.GetBuffer(), NULL, NULL, 0); if (lSize > 0) { // 2. Allocate and lock DEVMODE hDevMode = ::GlobalAlloc(GHND, lSize); if (hDevMode) { pDevMode = (DEVMODE*)::GlobalLock(hDevMode); // 3. Populate ::DocumentProperties(NULL, NULL, strPrinterName.GetBuffer(), pDevMode, NULL, DM_OUT_BUFFER); // 4. Force A4 pDevMode->dmFields |= DM_PAPERSIZE; pDevMode->dmPaperSize = nPaperEnumA4; // 5. Re-validate ::DocumentProperties(NULL, NULL, strPrinterName.GetBuffer(), pDevMode, pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER); ::GlobalUnlock(hDevMode); } } // --- D. Pass Handles and Show Dialog --- if (hDevMode) { dlg.m_pdex.lStructSize = sizeof(PRINTDLGEX); // *** CRITICAL FIX 1: RESTORE hDevNames ASSIGNMENT *** dlg.m_pdex.hDevMode = hDevMode; dlg.m_pdex.hDevNames = hDevNames; // Set flags to tell the dialog to return a DC, use the handles provided, // and use the copies and collate settings from the DEVMODE. dlg.m_pdex.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE; // *** CRITICAL FIX 2: RESTORE PAGE RANGE (use a non-restrictive range) *** dlg.m_pdex.nMinPage = 1; dlg.m_pdex.nMaxPage = 9999; // --- FIX: Removed the first redundant DoModal() call. --- // dlg.DoModal(); INT_PTR response = dlg.DoModal(); // <-- Single, correct call to show the dialog if (response == S_OK) // S_OK is returned on success for PrintDlgEx { AfxMessageBox(_T("Print Confirmed.")); // If successful, the returned hDevMode and hDevNames in m_pdex // contain the final settings selected by the user. } } // --- E. Cleanup --- // The framework or the subsequent printing code must take responsibility for // freeing the handles returned in dlg.m_pdex.hDevMode and hDevNames. // If DoModal failed, we free the handles we allocated. // NOTE: This cleanup logic needs to be safe against double-free if DoModal succeeded // and transferred ownership. For a test, this simple cleanup is acceptable. if (hDevMode) ::GlobalFree(hDevMode); if (hDevNames) ::GlobalFree(hDevNames);
}
please help me on this i tried different ways but not setting on UI printer preferences dialog its always showing letter.