Пример #1
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;
                }
            }