Exemplo n.º 1
0
            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;
            }
Exemplo n.º 2
0
            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;
                }
            }