GlobalUnlock() private méthode

private GlobalUnlock ( IntPtr hMem ) : bool
hMem IntPtr
Résultat bool
Exemple #1
0
            private PrintTicket AcquirePrintTicket(IntPtr devModeHandle, string printQueueName)
            {
                PrintTicket result = null;

                byte[] array  = null;
                IntPtr intPtr = IntPtr.Zero;

                try
                {
                    intPtr = UnsafeNativeMethods.GlobalLock(devModeHandle);
                    NativeMethods.DEVMODE devmode = (NativeMethods.DEVMODE)Marshal.PtrToStructure(intPtr, typeof(NativeMethods.DEVMODE));
                    array = new byte[(int)(devmode.dmSize + devmode.dmDriverExtra)];
                    Marshal.Copy(intPtr, array, 0, array.Length);
                }
                finally
                {
                    if (intPtr != IntPtr.Zero)
                    {
                        UnsafeNativeMethods.GlobalUnlock(devModeHandle);
                    }
                }
                SystemDrawingHelper.NewDefaultPrintingPermission().Assert();
                try
                {
                    using (PrintTicketConverter printTicketConverter = new PrintTicketConverter(printQueueName, PrintTicketConverter.MaxPrintSchemaVersion))
                    {
                        result = printTicketConverter.ConvertDevModeToPrintTicket(array);
                    }
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }
                return(result);
            }
Exemple #2
0
            AcquirePrintTicket(
                IntPtr devModeHandle,
                string printQueueName
                )
            {
                PrintTicket printTicket = null;

                byte[] devModeData = null;

                //
                // Copy the devmode into a byte array
                //
                IntPtr pDevMode = IntPtr.Zero;

                try
                {
                    pDevMode = UnsafeNativeMethods.GlobalLock(devModeHandle);

                    NativeMethods.DEVMODE devMode = (NativeMethods.DEVMODE)Marshal.PtrToStructure(
                        pDevMode,
                        typeof(NativeMethods.DEVMODE));
                    devModeData = new byte[devMode.dmSize + devMode.dmDriverExtra];
                    Marshal.Copy(pDevMode, devModeData, 0, devModeData.Length);
                }
                finally
                {
                    if (pDevMode != IntPtr.Zero)
                    {
                        UnsafeNativeMethods.GlobalUnlock(devModeHandle);
                    }
                }

                SystemDrawingHelper.NewDefaultPrintingPermission().Assert(); //BlessedAssert
                try
                {
                    //
                    // Convert the devmode data to a PrintTicket object
                    //
                    using (PrintTicketConverter ptConverter = new PrintTicketConverter(
                               printQueueName,
                               PrintTicketConverter.MaxPrintSchemaVersion))
                    {
                        printTicket = ptConverter.ConvertDevModeToPrintTicket(devModeData);
                    }
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }

                return(printTicket);
            }
Exemple #3
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;
            }
Exemple #4
0
            AcquirePrintTicket(
                IntPtr devModeHandle,
                string printQueueName
                )
            {
                PrintTicket printTicket = null;

                byte[] devModeData = null;

                //
                // Copy the devmode into a byte array
                //
                IntPtr pDevMode = IntPtr.Zero;

                try
                {
                    pDevMode = UnsafeNativeMethods.GlobalLock(devModeHandle);

                    NativeMethods.DEVMODE devMode = Marshal.PtrToStructure <NativeMethods.DEVMODE>(pDevMode);
                    devModeData = new byte[devMode.dmSize + devMode.dmDriverExtra];
                    Marshal.Copy(pDevMode, devModeData, 0, devModeData.Length);
                }
                finally
                {
                    if (pDevMode != IntPtr.Zero)
                    {
                        UnsafeNativeMethods.GlobalUnlock(devModeHandle);
                    }
                }

                //
                // Convert the devmode data to a PrintTicket object
                //
                using (PrintTicketConverter ptConverter = new PrintTicketConverter(
                           printQueueName,
                           PrintTicketConverter.MaxPrintSchemaVersion))
                {
                    printTicket = ptConverter.ConvertDevModeToPrintTicket(devModeData);
                }

                return(printTicket);
            }
Exemple #5
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;
                }
            }