AcquireResultFromPrintDlgExStruct( IntPtr unmanagedBuffer ) { UInt32 result = 0; // // Extract the devmode and devnames handles from the appropriate PRINTDLGEX structure // if (!Is64Bit()) { NativeMethods.PRINTDLGEX32 pdex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure( unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX32)); result = pdex.dwResultAction; } else { NativeMethods.PRINTDLGEX64 pdex = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure( unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX64)); result = pdex.dwResultAction; } return(result); }
FreeUnmanagedPrintDlgExStruct( IntPtr unmanagedBuffer ) { if (unmanagedBuffer == IntPtr.Zero) { return; } IntPtr devModeHandle = IntPtr.Zero; IntPtr devNamesHandle = IntPtr.Zero; IntPtr pageRangePtr = IntPtr.Zero; // // Extract the devmode and devnames handles from the appropriate PRINTDLGEX structure // if (!Is64Bit()) { NativeMethods.PRINTDLGEX32 pdex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure( unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX32)); devModeHandle = pdex.hDevMode; devNamesHandle = pdex.hDevNames; pageRangePtr = pdex.lpPageRanges; } else { NativeMethods.PRINTDLGEX64 pdex = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure( unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX64)); devModeHandle = pdex.hDevMode; devNamesHandle = pdex.hDevNames; pageRangePtr = pdex.lpPageRanges; } if (devModeHandle != IntPtr.Zero) { UnsafeNativeMethods.GlobalFree(devModeHandle); } if (devNamesHandle != IntPtr.Zero) { UnsafeNativeMethods.GlobalFree(devNamesHandle); } if (pageRangePtr != IntPtr.Zero) { UnsafeNativeMethods.GlobalFree(pageRangePtr); } Marshal.FreeHGlobal(unmanagedBuffer); }
private void ExtractPrintDataAndDevMode(IntPtr unmanagedBuffer, out string printerName, out uint flags, out PageRange pageRange, out IntPtr devModeHandle) { IntPtr intPtr = IntPtr.Zero; IntPtr intPtr2 = IntPtr.Zero; if (!this.Is64Bit()) { NativeMethods.PRINTDLGEX32 printdlgex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX32)); devModeHandle = printdlgex.hDevMode; intPtr = printdlgex.hDevNames; flags = printdlgex.Flags; intPtr2 = printdlgex.lpPageRanges; } else { NativeMethods.PRINTDLGEX64 printdlgex2 = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX64)); devModeHandle = printdlgex2.hDevMode; intPtr = printdlgex2.hDevNames; flags = printdlgex2.Flags; intPtr2 = printdlgex2.lpPageRanges; } if ((flags & 2U) == 2U && intPtr2 != IntPtr.Zero) { NativeMethods.PRINTPAGERANGE printpagerange = (NativeMethods.PRINTPAGERANGE)Marshal.PtrToStructure(intPtr2, typeof(NativeMethods.PRINTPAGERANGE)); pageRange = new PageRange((int)printpagerange.nFromPage, (int)printpagerange.nToPage); } else { pageRange = new PageRange(1); } if (intPtr != IntPtr.Zero) { IntPtr intPtr3 = IntPtr.Zero; try { intPtr3 = UnsafeNativeMethods.GlobalLock(intPtr); NativeMethods.DEVNAMES devnames = (NativeMethods.DEVNAMES)Marshal.PtrToStructure(intPtr3, typeof(NativeMethods.DEVNAMES)); int offset = checked ((int)devnames.wDeviceOffset * Marshal.SystemDefaultCharSize); printerName = Marshal.PtrToStringAuto(intPtr3 + offset); return; } finally { if (intPtr3 != IntPtr.Zero) { UnsafeNativeMethods.GlobalUnlock(intPtr); } } } printerName = string.Empty; }
private uint AcquireResultFromPrintDlgExStruct(IntPtr unmanagedBuffer) { uint dwResultAction; if (!this.Is64Bit()) { NativeMethods.PRINTDLGEX32 printdlgex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX32)); dwResultAction = printdlgex.dwResultAction; } else { NativeMethods.PRINTDLGEX64 printdlgex2 = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX64)); dwResultAction = printdlgex2.dwResultAction; } return(dwResultAction); }
private void FreeUnmanagedPrintDlgExStruct(IntPtr unmanagedBuffer) { if (unmanagedBuffer == IntPtr.Zero) { return; } IntPtr intPtr = IntPtr.Zero; IntPtr intPtr2 = IntPtr.Zero; IntPtr intPtr3 = IntPtr.Zero; if (!this.Is64Bit()) { NativeMethods.PRINTDLGEX32 printdlgex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX32)); intPtr = printdlgex.hDevMode; intPtr2 = printdlgex.hDevNames; intPtr3 = printdlgex.lpPageRanges; } else { NativeMethods.PRINTDLGEX64 printdlgex2 = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX64)); intPtr = printdlgex2.hDevMode; intPtr2 = printdlgex2.hDevNames; intPtr3 = printdlgex2.lpPageRanges; } if (intPtr != IntPtr.Zero) { UnsafeNativeMethods.GlobalFree(intPtr); } if (intPtr2 != IntPtr.Zero) { UnsafeNativeMethods.GlobalFree(intPtr2); } if (intPtr3 != IntPtr.Zero) { UnsafeNativeMethods.GlobalFree(intPtr3); } Marshal.FreeHGlobal(unmanagedBuffer); }
AllocateUnmanagedPrintDlgExStruct() { IntPtr unmanagedBuffer = IntPtr.Zero; NativeMethods.PRINTPAGERANGE range; range.nToPage = (uint)_dialog.PageRange.PageTo; range.nFromPage = (uint)_dialog.PageRange.PageFrom; uint defaultFlags = NativeMethods.PD_ALLPAGES | NativeMethods.PD_USEDEVMODECOPIESANDCOLLATE | NativeMethods.PD_DISABLEPRINTTOFILE | NativeMethods.PD_HIDEPRINTTOFILE; try { if (!Is64Bit()) { NativeMethods.PRINTDLGEX32 pdex = new NativeMethods.PRINTDLGEX32(); pdex.hwndOwner = _ownerHandle; pdex.nMinPage = _dialog.MinPage; pdex.nMaxPage = _dialog.MaxPage; pdex.Flags = defaultFlags; if (_dialog.SelectedPagesEnabled) { if (_dialog.PageRangeSelection == PageRangeSelection.SelectedPages) { pdex.Flags |= NativeMethods.PD_SELECTION; } } else { pdex.Flags |= NativeMethods.PD_NOSELECTION; } if (_dialog.CurrentPageEnabled) { if (_dialog.PageRangeSelection == PageRangeSelection.CurrentPage) { pdex.Flags |= NativeMethods.PD_CURRENTPAGE; } } else { pdex.Flags |= NativeMethods.PD_NOCURRENTPAGE; } if (_dialog.PageRangeEnabled) { pdex.lpPageRanges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE))); pdex.nMaxPageRanges = 1; if (_dialog.PageRangeSelection == PageRangeSelection.UserPages) { pdex.nPageRanges = 1; Marshal.StructureToPtr(range, pdex.lpPageRanges, false); pdex.Flags |= NativeMethods.PD_PAGENUMS; } else { pdex.nPageRanges = 0; } } else { pdex.lpPageRanges = IntPtr.Zero; pdex.nMaxPageRanges = 0; pdex.Flags |= NativeMethods.PD_NOPAGENUMS; } // // If we know a print queue and print ticket, then we need to try to select // the printer in the dialog and use the print ticket. We allocate and setup // a DEVNAMES structure for this as well as convert the PrintTicket to a // DEVMODE. // if (_dialog.PrintQueue != null) { pdex.hDevNames = AllocateAndInitializeDevNames(_dialog.PrintQueue.FullName); if (_dialog.PrintTicket != null) { pdex.hDevMode = AllocateAndInitializeDevMode( _dialog.PrintQueue.FullName, _dialog.PrintTicket); } } int cbBufferSize = Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX32)); unmanagedBuffer = Marshal.AllocHGlobal(cbBufferSize); Marshal.StructureToPtr(pdex, unmanagedBuffer, false); } else { NativeMethods.PRINTDLGEX64 pdex = new NativeMethods.PRINTDLGEX64(); pdex.hwndOwner = _ownerHandle; pdex.nMinPage = _dialog.MinPage; pdex.nMaxPage = _dialog.MaxPage; pdex.Flags = defaultFlags; if (_dialog.SelectedPagesEnabled) { if (_dialog.PageRangeSelection == PageRangeSelection.SelectedPages) { pdex.Flags |= NativeMethods.PD_SELECTION; } } else { pdex.Flags |= NativeMethods.PD_NOSELECTION; } if (_dialog.CurrentPageEnabled) { if (_dialog.PageRangeSelection == PageRangeSelection.CurrentPage) { pdex.Flags |= NativeMethods.PD_CURRENTPAGE; } } else { pdex.Flags |= NativeMethods.PD_NOCURRENTPAGE; } if (_dialog.PageRangeEnabled) { pdex.lpPageRanges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE))); pdex.nMaxPageRanges = 1; if (_dialog.PageRangeSelection == PageRangeSelection.UserPages) { pdex.nPageRanges = 1; Marshal.StructureToPtr(range, pdex.lpPageRanges, false); pdex.Flags |= NativeMethods.PD_PAGENUMS; } else { pdex.nPageRanges = 0; } } else { pdex.lpPageRanges = IntPtr.Zero; pdex.nMaxPageRanges = 0; pdex.Flags |= NativeMethods.PD_NOPAGENUMS; } // // If we know a print queue and print ticket, then we need to try to select // the printer in the dialog and use the print ticket. We allocate and setup // a DEVNAMES structure for this as well as convert the PrintTicket to a // DEVMODE. // if (_dialog.PrintQueue != null) { pdex.hDevNames = AllocateAndInitializeDevNames(_dialog.PrintQueue.FullName); if (_dialog.PrintTicket != null) { pdex.hDevMode = AllocateAndInitializeDevMode( _dialog.PrintQueue.FullName, _dialog.PrintTicket); } } int cbBufferSize = Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX64)); unmanagedBuffer = Marshal.AllocHGlobal(cbBufferSize); Marshal.StructureToPtr(pdex, unmanagedBuffer, false); } } catch (Exception) { if (unmanagedBuffer != null) { FreeUnmanagedPrintDlgExStruct(unmanagedBuffer); unmanagedBuffer = IntPtr.Zero; } throw; } return(unmanagedBuffer); }
ExtractPrintDataAndDevMode( IntPtr unmanagedBuffer, out string printerName, out UInt32 flags, out PageRange pageRange, out IntPtr devModeHandle ) { IntPtr devNamesHandle = IntPtr.Zero; IntPtr pageRangePtr = IntPtr.Zero; // // Extract the devmode and devnames handles from the appropriate PRINTDLGEX structure // if (!Is64Bit()) { NativeMethods.PRINTDLGEX32 pdex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure( unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX32)); devModeHandle = pdex.hDevMode; devNamesHandle = pdex.hDevNames; flags = pdex.Flags; pageRangePtr = pdex.lpPageRanges; } else { NativeMethods.PRINTDLGEX64 pdex = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure( unmanagedBuffer, typeof(NativeMethods.PRINTDLGEX64)); devModeHandle = pdex.hDevMode; devNamesHandle = pdex.hDevNames; flags = pdex.Flags; pageRangePtr = pdex.lpPageRanges; } // // Get a managed copy of the page ranges. This only matters if the PD_PAGENUMS bit is // set in the flags. // if (((flags & NativeMethods.PD_PAGENUMS) == NativeMethods.PD_PAGENUMS) && (pageRangePtr != IntPtr.Zero)) { NativeMethods.PRINTPAGERANGE pageRangeStruct = (NativeMethods.PRINTPAGERANGE)Marshal.PtrToStructure( pageRangePtr, typeof(NativeMethods.PRINTPAGERANGE)); pageRange = new PageRange((int)pageRangeStruct.nFromPage, (int)pageRangeStruct.nToPage); } else { pageRange = new PageRange(1); } // // Get a managed copy of the device name // if (devNamesHandle != IntPtr.Zero) { IntPtr pDevNames = IntPtr.Zero; try { pDevNames = UnsafeNativeMethods.GlobalLock(devNamesHandle); NativeMethods.DEVNAMES devNames = (NativeMethods.DEVNAMES)Marshal.PtrToStructure( pDevNames, typeof(NativeMethods.DEVNAMES)); int devNamesOffset = checked (devNames.wDeviceOffset * Marshal.SystemDefaultCharSize); printerName = Marshal.PtrToStringAuto(pDevNames + devNamesOffset); } finally { if (pDevNames != IntPtr.Zero) { UnsafeNativeMethods.GlobalUnlock(devNamesHandle); } } } else { printerName = string.Empty; } }
AllocateUnmanagedPrintDlgExStruct() { IntPtr unmanagedBuffer = IntPtr.Zero; NativeMethods.PRINTPAGERANGE range; range.nToPage = (uint)_dialog.PageRange.PageTo; range.nFromPage = (uint)_dialog.PageRange.PageFrom; try { if (!Is64Bit()) { NativeMethods.PRINTDLGEX32 pdex = new NativeMethods.PRINTDLGEX32(); pdex.hwndOwner = _ownerHandle; pdex.nMinPage = _dialog.MinPage; pdex.nMaxPage = _dialog.MaxPage; pdex.Flags = NativeMethods.PD_ALLPAGES | NativeMethods.PD_NOCURRENTPAGE | NativeMethods.PD_NOSELECTION | NativeMethods.PD_USEDEVMODECOPIESANDCOLLATE | NativeMethods.PD_DISABLEPRINTTOFILE | NativeMethods.PD_HIDEPRINTTOFILE; if (_dialog.PageRangeEnabled) { pdex.lpPageRanges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE))); pdex.nMaxPageRanges = 1; if (_dialog.PageRangeSelection == PageRangeSelection.UserPages) { pdex.nPageRanges = 1; Marshal.StructureToPtr(range, pdex.lpPageRanges, false); pdex.Flags |= NativeMethods.PD_PAGENUMS; } else { pdex.nPageRanges = 0; } } else { pdex.lpPageRanges = IntPtr.Zero; pdex.nMaxPageRanges = 0; pdex.Flags |= NativeMethods.PD_NOPAGENUMS; } // // If we know a print queue and print ticket, then we need to try to select // the printer in the dialog and use the print ticket. We allocate and setup // a DEVNAMES structure for this as well as convert the PrintTicket to a // DEVMODE. // if (_dialog.PrintQueue != null) { pdex.hDevNames = AllocateAndInitializeDevNames(_dialog.PrintQueue.FullName); if (_dialog.PrintTicket != null) { pdex.hDevMode = AllocateAndInitializeDevMode( _dialog.PrintQueue.FullName, _dialog.PrintTicket); } } int cbBufferSize = Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX32)); unmanagedBuffer = Marshal.AllocHGlobal(cbBufferSize); Marshal.StructureToPtr(pdex, unmanagedBuffer, false); } else { NativeMethods.PRINTDLGEX64 pdex = new NativeMethods.PRINTDLGEX64(); pdex.hwndOwner = _ownerHandle; pdex.nMinPage = _dialog.MinPage; pdex.nMaxPage = _dialog.MaxPage; pdex.Flags = NativeMethods.PD_ALLPAGES | NativeMethods.PD_NOCURRENTPAGE | NativeMethods.PD_NOSELECTION | NativeMethods.PD_USEDEVMODECOPIESANDCOLLATE | NativeMethods.PD_DISABLEPRINTTOFILE | NativeMethods.PD_HIDEPRINTTOFILE; if (_dialog.PageRangeEnabled) { pdex.lpPageRanges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE))); pdex.nMaxPageRanges = 1; if (_dialog.PageRangeSelection == PageRangeSelection.UserPages) { pdex.nPageRanges = 1; Marshal.StructureToPtr(range, pdex.lpPageRanges, false); pdex.Flags |= NativeMethods.PD_PAGENUMS; } else { pdex.nPageRanges = 0; } } else { pdex.lpPageRanges = IntPtr.Zero; pdex.nMaxPageRanges = 0; pdex.Flags |= NativeMethods.PD_NOPAGENUMS; } // // If we know a print queue and print ticket, then we need to try to select // the printer in the dialog and use the print ticket. We allocate and setup // a DEVNAMES structure for this as well as convert the PrintTicket to a // DEVMODE. // if (_dialog.PrintQueue != null) { pdex.hDevNames = AllocateAndInitializeDevNames(_dialog.PrintQueue.FullName); if (_dialog.PrintTicket != null) { pdex.hDevMode = AllocateAndInitializeDevMode( _dialog.PrintQueue.FullName, _dialog.PrintTicket); } } int cbBufferSize = Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX64)); unmanagedBuffer = Marshal.AllocHGlobal(cbBufferSize); Marshal.StructureToPtr(pdex, unmanagedBuffer, false); } } catch (Exception) { if (unmanagedBuffer != null) { FreeUnmanagedPrintDlgExStruct(unmanagedBuffer); unmanagedBuffer = IntPtr.Zero; } throw; } return unmanagedBuffer; }
private IntPtr AllocateUnmanagedPrintDlgExStruct() { IntPtr intPtr = IntPtr.Zero; NativeMethods.PRINTPAGERANGE printpagerange; printpagerange.nToPage = (uint)this._dialog.PageRange.PageTo; printpagerange.nFromPage = (uint)this._dialog.PageRange.PageFrom; uint flags = 1835008U; try { if (!this.Is64Bit()) { NativeMethods.PRINTDLGEX32 printdlgex = new NativeMethods.PRINTDLGEX32(); printdlgex.hwndOwner = this._ownerHandle; printdlgex.nMinPage = this._dialog.MinPage; printdlgex.nMaxPage = this._dialog.MaxPage; printdlgex.Flags = flags; if (this._dialog.SelectedPagesEnabled) { if (this._dialog.PageRangeSelection == PageRangeSelection.SelectedPages) { printdlgex.Flags |= 1U; } } else { printdlgex.Flags |= 4U; } if (this._dialog.CurrentPageEnabled) { if (this._dialog.PageRangeSelection == PageRangeSelection.CurrentPage) { printdlgex.Flags |= 4194304U; } } else { printdlgex.Flags |= 8388608U; } if (this._dialog.PageRangeEnabled) { printdlgex.lpPageRanges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE))); printdlgex.nMaxPageRanges = 1U; if (this._dialog.PageRangeSelection == PageRangeSelection.UserPages) { printdlgex.nPageRanges = 1U; Marshal.StructureToPtr(printpagerange, printdlgex.lpPageRanges, false); printdlgex.Flags |= 2U; } else { printdlgex.nPageRanges = 0U; } } else { printdlgex.lpPageRanges = IntPtr.Zero; printdlgex.nMaxPageRanges = 0U; printdlgex.Flags |= 8U; } if (this._dialog.PrintQueue != null) { printdlgex.hDevNames = this.AllocateAndInitializeDevNames(this._dialog.PrintQueue.FullName); if (this._dialog.PrintTicket != null) { printdlgex.hDevMode = this.AllocateAndInitializeDevMode(this._dialog.PrintQueue.FullName, this._dialog.PrintTicket); } } int cb = Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX32)); intPtr = Marshal.AllocHGlobal(cb); Marshal.StructureToPtr(printdlgex, intPtr, false); } else { NativeMethods.PRINTDLGEX64 printdlgex2 = new NativeMethods.PRINTDLGEX64(); printdlgex2.hwndOwner = this._ownerHandle; printdlgex2.nMinPage = this._dialog.MinPage; printdlgex2.nMaxPage = this._dialog.MaxPage; printdlgex2.Flags = flags; if (this._dialog.SelectedPagesEnabled) { if (this._dialog.PageRangeSelection == PageRangeSelection.SelectedPages) { printdlgex2.Flags |= 1U; } } else { printdlgex2.Flags |= 4U; } if (this._dialog.CurrentPageEnabled) { if (this._dialog.PageRangeSelection == PageRangeSelection.CurrentPage) { printdlgex2.Flags |= 4194304U; } } else { printdlgex2.Flags |= 8388608U; } if (this._dialog.PageRangeEnabled) { printdlgex2.lpPageRanges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE))); printdlgex2.nMaxPageRanges = 1U; if (this._dialog.PageRangeSelection == PageRangeSelection.UserPages) { printdlgex2.nPageRanges = 1U; Marshal.StructureToPtr(printpagerange, printdlgex2.lpPageRanges, false); printdlgex2.Flags |= 2U; } else { printdlgex2.nPageRanges = 0U; } } else { printdlgex2.lpPageRanges = IntPtr.Zero; printdlgex2.nMaxPageRanges = 0U; printdlgex2.Flags |= 8U; } if (this._dialog.PrintQueue != null) { printdlgex2.hDevNames = this.AllocateAndInitializeDevNames(this._dialog.PrintQueue.FullName); if (this._dialog.PrintTicket != null) { printdlgex2.hDevMode = this.AllocateAndInitializeDevMode(this._dialog.PrintQueue.FullName, this._dialog.PrintTicket); } } int cb2 = Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX64)); intPtr = Marshal.AllocHGlobal(cb2); Marshal.StructureToPtr(printdlgex2, intPtr, false); } } catch (Exception) { this.FreeUnmanagedPrintDlgExStruct(intPtr); intPtr = IntPtr.Zero; throw; } return(intPtr); }