コード例 #1
0
ファイル: Win32.cs プロジェクト: xiaopohou/windy
            /// <summary>
            /// 获取指定打印机的相关信息
            /// </summary>
            /// <param name="szPrinterName">打印机名称</param>
            /// <param name="stPrinterInfo">打印机信息</param>
            /// <returns>是否执行成功</returns>
            public static bool GetPrinterInfo(string szPrinterName, ref NativeMethods.WinSpool.PRINTER_INFO stPrinterInfo)
            {
                IntPtr hPrinter = IntPtr.Zero;

                NativeMethods.WinSpool.StructPrinterDefaults defaults = new NativeMethods.WinSpool.StructPrinterDefaults();
                if (!NativeMethods.WinSpool.OpenPrinter(szPrinterName, out hPrinter, ref defaults))
                {
                    return(false);
                }

                int cbNeeded = 0;

                NativeMethods.WinSpool.GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
                if (cbNeeded <= 0)
                {
                    NativeMethods.WinSpool.ClosePrinter(hPrinter);
                    return(false);
                }
                try
                {
                    IntPtr pAddr = System.Runtime.InteropServices.Marshal.AllocHGlobal((int)cbNeeded);
                    if (NativeMethods.WinSpool.GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded))
                    {
                        stPrinterInfo = (NativeMethods.WinSpool.PRINTER_INFO)
                                        System.Runtime.InteropServices.Marshal.PtrToStructure(pAddr, typeof(NativeMethods.WinSpool.PRINTER_INFO));
                    }
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(pAddr);
                }
                catch (Exception ex)
                {
                    LogManager.Instance.WriteLog("GlobalMethods.GetPrinterInfo", new string[] { "szPrinterName" }
                                                 , new object[] { szPrinterName }, ex);
                }
                NativeMethods.WinSpool.ClosePrinter(hPrinter);
                return(true);
            }
コード例 #2
0
ファイル: Win32.cs プロジェクト: zuifengke/windy-common
            /// <summary>
            /// ��ȡָ����ӡ���������Ϣ
            /// </summary>
            /// <param name="szPrinterName">��ӡ������</param>
            /// <param name="stPrinterInfo">��ӡ����Ϣ</param>
            /// <returns>�Ƿ�ִ�гɹ�</returns>
            public static bool GetPrinterInfo(string szPrinterName, ref NativeMethods.WinSpool.PRINTER_INFO stPrinterInfo)
            {
                IntPtr hPrinter = IntPtr.Zero;
                NativeMethods.WinSpool.StructPrinterDefaults defaults = new NativeMethods.WinSpool.StructPrinterDefaults();
                if (!NativeMethods.WinSpool.OpenPrinter(szPrinterName, out hPrinter, ref defaults))
                    return false;

                int cbNeeded = 0;
                NativeMethods.WinSpool.GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
                if (cbNeeded <= 0)
                {
                    NativeMethods.WinSpool.ClosePrinter(hPrinter);
                    return false;
                }
                try
                {
                    IntPtr pAddr = System.Runtime.InteropServices.Marshal.AllocHGlobal((int)cbNeeded);
                    if (NativeMethods.WinSpool.GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded))
                    {
                        stPrinterInfo = (NativeMethods.WinSpool.PRINTER_INFO)
                            System.Runtime.InteropServices.Marshal.PtrToStructure(pAddr, typeof(NativeMethods.WinSpool.PRINTER_INFO));
                    }
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(pAddr);
                }
                catch (Exception ex)
                {
                    LogManager.Instance.WriteLog("GlobalMethods.GetPrinterInfo", new string[] { "szPrinterName" }
                        , new object[] { szPrinterName }, ex);
                }
                NativeMethods.WinSpool.ClosePrinter(hPrinter);
                return true;
            }