Пример #1
0
            /// <summary>
            /// Get various settings
            /// </summary>
            /// <param name="nativeBuffer"></param>
            /// <param name="printerName"></param>
            /// <param name="flags"></param>
            /// <param name="pageRange"></param>
            /// <param name="dModeHnd"></param>
            void GetSettings(IntPtr nativeBuffer, out string printerName, out uint flags, out PageRange pageRange, out IntPtr dModeHnd)
            {
                IntPtr dNames  = IntPtr.Zero;
                IntPtr pRanges = IntPtr.Zero;

                if (Is64Bits())
                {
                    NativeMethods.PRINTDLGEX64 dlg = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(nativeBuffer, typeof(NativeMethods.PRINTDLGEX64));
                    dModeHnd = dlg.hDevMode;
                    dNames   = dlg.hDevNames;
                    flags    = dlg.Flags;
                    pRanges  = dlg.lpPageRanges;
                }
                else
                {
                    NativeMethods.PRINTDLGEX32 dlg = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(nativeBuffer, typeof(NativeMethods.PRINTDLGEX32));
                    dModeHnd = dlg.hDevMode;
                    dNames   = dlg.hDevNames;
                    flags    = dlg.Flags;
                    pRanges  = dlg.lpPageRanges;
                }

                if (((flags & 2) == 2) && (pRanges != IntPtr.Zero))
                {
                    NativeMethods.PRINTPAGERANGE printRange = (NativeMethods.PRINTPAGERANGE)Marshal.PtrToStructure(pRanges, typeof(NativeMethods.PRINTPAGERANGE));
                    pageRange = new PageRange((int)printRange.nFromPage, (int)printRange.nToPage);
                }
                else
                {
                    pageRange = new PageRange(1);
                }

                if (dNames != IntPtr.Zero)
                {
                    IntPtr ptrDevNames = IntPtr.Zero;
                    try
                    {
                        ptrDevNames = NativeMethods.GlobalLock(dNames);
                        NativeMethods.DEVNAMES devnames = (NativeMethods.DEVNAMES)Marshal.PtrToStructure(ptrDevNames, typeof(NativeMethods.DEVNAMES));
                        printerName = Marshal.PtrToStringAuto(
                            (IntPtr)(((long)ptrDevNames) + (devnames.wDeviceOffset * Marshal.SystemDefaultCharSize))
                            );
                    }
                    finally
                    {
                        if (ptrDevNames != IntPtr.Zero)
                        {
                            NativeMethods.GlobalUnlock(dNames);
                        }
                    }
                }
                else
                {
                    printerName = string.Empty;
                }
            }
Пример #2
0
 /// <summary>
 /// Get dwResultAction from Dlg
 /// </summary>
 /// <param name="ptrPrintDlg"></param>
 /// <returns></returns>
 uint GetResultPrintDlgExHnd(IntPtr ptrPrintDlg)
 {
     if (Is64Bits())
     {
         NativeMethods.PRINTDLGEX64 dlg = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(ptrPrintDlg, typeof(NativeMethods.PRINTDLGEX64));
         return(dlg.dwResultAction);
     }
     else
     {
         NativeMethods.PRINTDLGEX32 dlg = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(ptrPrintDlg, typeof(NativeMethods.PRINTDLGEX32));
         return(dlg.dwResultAction);
     }
 }
Пример #3
0
            /// <summary>
            /// Frees memory associated with PRINTDLGEX32 or PRINTDLGEX64 structures
            /// </summary>
            /// <param name="ptr"></param>
            void DeallocatePrintDlgExStruct(IntPtr ptr)
            {
                if (ptr != IntPtr.Zero)
                {
                    IntPtr hMode       = IntPtr.Zero;
                    IntPtr hNames      = IntPtr.Zero;
                    IntPtr hPageRanges = IntPtr.Zero;

                    if (Is64Bits())
                    {
                        NativeMethods.PRINTDLGEX64 pDlg = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(ptr, typeof(NativeMethods.PRINTDLGEX64));
                        hMode       = pDlg.hDevMode;
                        hNames      = pDlg.hDevNames;
                        hPageRanges = pDlg.lpPageRanges;
                    }
                    else
                    {
                        NativeMethods.PRINTDLGEX32 pDlg = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(ptr, typeof(NativeMethods.PRINTDLGEX32));
                        hMode       = pDlg.hDevMode;
                        hNames      = pDlg.hDevNames;
                        hPageRanges = pDlg.lpPageRanges;
                    }

                    if (hMode != IntPtr.Zero)
                    {
                        NativeMethods.GlobalFree(hMode);
                    }

                    if (hNames != IntPtr.Zero)
                    {
                        NativeMethods.GlobalFree(hNames);
                    }

                    if (hPageRanges != IntPtr.Zero)
                    {
                        NativeMethods.GlobalFree(hPageRanges);
                    }

                    Marshal.FreeHGlobal(ptr);
                }
            }
Пример #4
0
            /// <summary>
            /// Allocate memory associated with PRINTDLGEX32 or PRINTDLGEX64 structures
            /// </summary>
            IntPtr AllocatePrintDlgExStruct()
            {
                NativeMethods.PRINTPAGERANGE pageRange;
                IntPtr ptr = IntPtr.Zero;

                pageRange.nToPage   = (uint)mDialogOwner.PageRange.PageTo;
                pageRange.nFromPage = (uint)mDialogOwner.PageRange.PageFrom;

                try
                {
                    //Handle 32 bit case first
                    if (!Is64Bits())
                    {
                        NativeMethods.PRINTDLGEX32 pDlg = new NativeMethods.PRINTDLGEX32();
                        pDlg.hwndOwner = mWinHandle;
                        pDlg.nMinPage  = mDialogOwner.MinPage;
                        pDlg.nMaxPage  = mDialogOwner.MaxPage;
                        pDlg.Flags     = 0x9c0004;

                        if (mDialogOwner.PageRangeEnabled)
                        {
                            pDlg.lpPageRanges   = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE)));
                            pDlg.nMaxPageRanges = 1;

                            if (mDialogOwner.PageRangeSelection == PageRangeSelection.UserPages)
                            {
                                pDlg.nPageRanges = 1;
                                Marshal.StructureToPtr(pageRange, pDlg.lpPageRanges, false);
                                pDlg.Flags |= 2;
                            }
                            else
                            {
                                pDlg.nPageRanges = 0;
                            }
                        }
                        else
                        {
                            pDlg.lpPageRanges   = IntPtr.Zero;
                            pDlg.nMaxPageRanges = 0;
                            pDlg.Flags         |= 8;
                        }

                        if (mDialogOwner.PrintQueue != null)
                        {
                            pDlg.hDevNames = InitializeDevNames(mDialogOwner.PrintQueue.FullName);
                            if (mDialogOwner.PrintTicket != null)
                            {
                                pDlg.hDevMode = InitializeDevMode(mDialogOwner.PrintQueue.FullName, mDialogOwner.PrintTicket);
                            }
                        }

                        ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX32)));
                        Marshal.StructureToPtr(pDlg, ptr, false);
                        return(ptr);
                    }

                    //Go with 64 bit structure
                    NativeMethods.PRINTDLGEX64 pDlg64 = new NativeMethods.PRINTDLGEX64();
                    pDlg64.hwndOwner = mWinHandle;
                    pDlg64.nMinPage  = mDialogOwner.MinPage;
                    pDlg64.nMaxPage  = mDialogOwner.MaxPage;
                    pDlg64.Flags     = 0x9c0004;

                    if (mDialogOwner.PageRangeEnabled)
                    {
                        pDlg64.lpPageRanges   = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE)));
                        pDlg64.nMaxPageRanges = 1;

                        if (mDialogOwner.PageRangeSelection == PageRangeSelection.UserPages)
                        {
                            pDlg64.nPageRanges = 1;
                            Marshal.StructureToPtr(pageRange, pDlg64.lpPageRanges, false);
                            pDlg64.Flags |= 2;
                        }
                        else
                        {
                            pDlg64.nPageRanges = 0;
                        }
                    }
                    else
                    {
                        pDlg64.lpPageRanges   = IntPtr.Zero;
                        pDlg64.nMaxPageRanges = 0;
                        pDlg64.Flags         |= 8;
                    }

                    if (mDialogOwner.PrintQueue != null)
                    {
                        pDlg64.hDevNames = InitializeDevNames(mDialogOwner.PrintQueue.FullName);

                        if (mDialogOwner.PrintTicket != null)
                        {
                            pDlg64.hDevMode = InitializeDevMode(mDialogOwner.PrintQueue.FullName, mDialogOwner.PrintTicket);
                        }
                    }

                    ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX64)));
                    Marshal.StructureToPtr(pDlg64, ptr, false);
                    return(ptr);
                }
                catch (Exception)
                {                   //Free buffer on error
                    DeallocatePrintDlgExStruct(ptr);
                    throw;
                }
            }
Пример #5
0
			/// <summary>
			/// Allocate memory associated with PRINTDLGEX32 or PRINTDLGEX64 structures
			/// </summary>
			IntPtr AllocatePrintDlgExStruct()
			{
				NativeMethods.PRINTPAGERANGE pageRange;
				IntPtr ptr = IntPtr.Zero;
				pageRange.nToPage = (uint)mDialogOwner.PageRange.PageTo;
				pageRange.nFromPage = (uint)mDialogOwner.PageRange.PageFrom;

				try
				{
					//Handle 32 bit case first
					if (!Is64Bits())
					{
						NativeMethods.PRINTDLGEX32 pDlg = new NativeMethods.PRINTDLGEX32();
						pDlg.hwndOwner = mWinHandle;
						pDlg.nMinPage = mDialogOwner.MinPage;
						pDlg.nMaxPage = mDialogOwner.MaxPage;
						pDlg.Flags = 0x9c0004;

						if (mDialogOwner.PageRangeEnabled)
						{
							pDlg.lpPageRanges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE)));
							pDlg.nMaxPageRanges = 1;

							if (mDialogOwner.PageRangeSelection == PageRangeSelection.UserPages)
							{
								pDlg.nPageRanges = 1;
								Marshal.StructureToPtr(pageRange, pDlg.lpPageRanges, false);
								pDlg.Flags |= 2;
							}
							else
							{
								pDlg.nPageRanges = 0;
							}
						}
						else
						{
							pDlg.lpPageRanges = IntPtr.Zero;
							pDlg.nMaxPageRanges = 0;
							pDlg.Flags |= 8;
						}

						if (mDialogOwner.PrintQueue != null)
						{
							pDlg.hDevNames = InitializeDevNames(mDialogOwner.PrintQueue.FullName);
							if (mDialogOwner.PrintTicket != null)
								pDlg.hDevMode = InitializeDevMode(mDialogOwner.PrintQueue.FullName, mDialogOwner.PrintTicket);
						}

						ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX32)));
						Marshal.StructureToPtr(pDlg, ptr, false);
						return ptr;
					}

					//Go with 64 bit structure
					NativeMethods.PRINTDLGEX64 pDlg64 = new NativeMethods.PRINTDLGEX64();
					pDlg64.hwndOwner = mWinHandle;
					pDlg64.nMinPage = mDialogOwner.MinPage;
					pDlg64.nMaxPage = mDialogOwner.MaxPage;
					pDlg64.Flags = 0x9c0004;

					if (mDialogOwner.PageRangeEnabled)
					{
						pDlg64.lpPageRanges = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE)));
						pDlg64.nMaxPageRanges = 1;

						if (mDialogOwner.PageRangeSelection == PageRangeSelection.UserPages)
						{
							pDlg64.nPageRanges = 1;
							Marshal.StructureToPtr(pageRange, pDlg64.lpPageRanges, false);
							pDlg64.Flags |= 2;
						}
						else
						{
							pDlg64.nPageRanges = 0;
						}
					}
					else
					{
						pDlg64.lpPageRanges = IntPtr.Zero;
						pDlg64.nMaxPageRanges = 0;
						pDlg64.Flags |= 8;
					}

					if (mDialogOwner.PrintQueue != null)
					{
						pDlg64.hDevNames = InitializeDevNames(mDialogOwner.PrintQueue.FullName);

						if (mDialogOwner.PrintTicket != null)
							pDlg64.hDevMode = InitializeDevMode(mDialogOwner.PrintQueue.FullName, mDialogOwner.PrintTicket);
					}

					ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.PRINTDLGEX64)));
					Marshal.StructureToPtr(pDlg64, ptr, false);
					return ptr;
				}
				catch (Exception)
				{   //Free buffer on error
					DeallocatePrintDlgExStruct(ptr);
					throw;
				}
			}