Пример #1
0
        private IEnumerable <PRINTER_INFO_2> EnumPrinters(PrinterEnumFlags flags)
        {
            uint cbNeeded  = 0;
            uint cReturned = 0;

            if (EnumPrinters(flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                return(null);
            }
            int lastWin32Error = Marshal.GetLastWin32Error();

            if (lastWin32Error == ERROR_INSUFFICIENT_BUFFER)
            {
                IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                if (EnumPrinters(flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
                {
                    var    printerInfo2 = new PRINTER_INFO_2[cReturned];
                    IntPtr offset       = pAddr;
                    Type   type         = typeof(PRINTER_INFO_2);
                    int    increment    = Marshal.SizeOf(type);
                    for (int i = 0; i < cReturned; i++)
                    {
                        printerInfo2[i] = (PRINTER_INFO_2)Marshal.PtrToStructure(offset, type);
                        offset         += increment;
                    }
                    Marshal.FreeHGlobal(pAddr);
                    return(printerInfo2);
                }
                lastWin32Error = Marshal.GetLastWin32Error();
            }
            throw new Win32Exception(lastWin32Error);
        }
Пример #2
0
        public static List <PRINTER_INFO_2> List()
        {
            uint             cbNeeded  = 0;
            uint             cReturned = 0;
            PrinterEnumFlags flags     = PrinterEnumFlags.PRINTER_ENUM_LOCAL;

            if (EnumPrinters(flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                return(null);
            }
            int lastWin32Error = Marshal.GetLastWin32Error();

            if (lastWin32Error == ERROR_INSUFFICIENT_BUFFER)
            {
                IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                if (EnumPrinters(flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
                {
                    List <PRINTER_INFO_2> PrinterInfos = new List <PRINTER_INFO_2>();
                    int  offset    = pAddr.ToInt32();
                    Type type      = typeof(PRINTER_INFO_2);
                    int  increment = Marshal.SizeOf(type);
                    for (int i = 0; i < cReturned; i++)
                    {
                        PRINTER_INFO_2 pi2 = (PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                        PrinterInfos.Add(pi2);
                        offset += increment;
                    }
                    Marshal.FreeHGlobal(pAddr);
                    return(PrinterInfos);
                }
                lastWin32Error = Marshal.GetLastWin32Error();
            }
            throw new System.ComponentModel.Win32Exception(lastWin32Error);
        }
Пример #3
0
        internal static int GetPrinterStatusInt(string PrinterName)
        {
            int    intRet = 0;
            IntPtr hPrinter;
            structPrinterDefaults defaults = new structPrinterDefaults();

            if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
            {
                int  cbNeeded = 0;
                bool bolRet   = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
                if (cbNeeded > 0)
                {
                    IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                    bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
                    if (bolRet)
                    {
                        PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();

                        Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));

                        intRet = System.Convert.ToInt32(Info2.Status);
                    }
                    Marshal.FreeHGlobal(pAddr);
                }
                ClosePrinter(hPrinter);
            }

            return(intRet);
        }
Пример #4
0
        public static GetCvPort.PRINTER_INFO_2 GetPrinterInfo(string printerName)
        {
            IntPtr hPrinter = new IntPtr();

            if (!GetCvPort.OpenPrinter(printerName, ref hPrinter, (long)IntPtr.Zero))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            IntPtr num = IntPtr.Zero;

            try
            {
                int pcbNeeded1 = new int();
                GetCvPort.GetPrinter(hPrinter, 2, IntPtr.Zero, 0, ref pcbNeeded1);
                if (pcbNeeded1 <= 0)
                {
                    throw new Exception("Fail!!");
                }
                num = Marshal.AllocHGlobal(pcbNeeded1);
                int pcbNeeded2 = new int();
                if (!GetCvPort.GetPrinter(hPrinter, 2, num, pcbNeeded1, ref pcbNeeded2))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                object structure = Marshal.PtrToStructure(num, typeof(GetCvPort.PRINTER_INFO_2));
                GetCvPort.PRINTER_INFO_2 printerInfo2 = new PRINTER_INFO_2();
                return(structure != null ? (GetCvPort.PRINTER_INFO_2)structure : printerInfo2);
            }
            finally
            {
                GetCvPort.ClosePrinter(hPrinter);
                Marshal.FreeHGlobal(num);
            }
        }
Пример #5
0
        public static PRINTER_INFO_2[] enumPrinters(PrinterEnumFlags Flags)
        {
            uint cbNeeded  = 0;
            uint cReturned = 0;

            if (EnumPrinters(Flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                return(null);
            }
            int lastWin32Error = Marshal.GetLastWin32Error();

            if (lastWin32Error == ERROR_INSUFFICIENT_BUFFER)
            {
                IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                if (EnumPrinters(Flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
                {
                    PRINTER_INFO_2[] printerInfo2 = new PRINTER_INFO_2[cReturned];
                    int  offset    = pAddr.ToInt32();
                    Type type      = typeof(PRINTER_INFO_2);
                    int  increment = Marshal.SizeOf(type);
                    for (int i = 0; i < cReturned; i++)
                    {
                        printerInfo2[i] = (PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                        offset         += increment;
                    }
                    Marshal.FreeHGlobal(pAddr);
                    return(printerInfo2);
                }
                lastWin32Error = Marshal.GetLastWin32Error();
            }
            throw new Exception();
        }
Пример #6
0
        public void InstallPrinter(String printerName, String printerPortName, String printerDriverName)
        {
            PRINTER_INFO_2 pInfo = new PRINTER_INFO_2
            {
                pPrinterName    = printerName,
                pPortName       = printerPortName,
                pDriverName     = printerDriverName,
                pPrintProcessor = "WinPrint",
                pShareName      = printerName,
                pDatatype       = "RAW",
                Priority        = 1,
                DefaultPriority = 1,
            };


            IntPtr hPrt = AddPrinter("", 2, ref pInfo);

            if (hPrt == IntPtr.Zero)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }

            if (ClosePrinter(hPrt) == false)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new Win32Exception(errno);
            }
        }
Пример #7
0
        public PrinterInfo(Printer printer)
        {
            tooltip = new ToolTip();
            InitializeComponent();

            info = Main.Manager.GetPrinter(printer);
        }
Пример #8
0
        //使用非托管代码应该设置项目允许unsaft代码

        ///// <summary>
        ///// 遍历打印机
        ///// </summary>
        ///// <param name="Flags"></param>
        ///// <returns></returns>
        //unsafe public static PRINTER_INFO_2[] EnumPrinters(PrinterEnumFlags Flags)
        //{
        //    PRINTER_INFO_2[] Info2;

        //    uint cbNeeded = 0;
        //    uint cReturned = 0;

        //    bool ret = EnumPrinters(Flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);

        //    byte[] bPrinterEnum = new byte[cbNeeded];

        //    fixed (byte* pAddr = bPrinterEnum)
        //    {

        //        ret = EnumPrinters(Flags, null, 2, (IntPtr)pAddr, (uint)bPrinterEnum.Length, ref cbNeeded, ref cReturned);

        //        if (ret)
        //        {
        //            Info2 = new PRINTER_INFO_2[cReturned];

        //            byte* pCurrent = pAddr;
        //            for (int i = 0; i < cReturned; i++)
        //            {
        //                Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pComment = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pLocation = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pDevMode = Marshal.ReadIntPtr((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].pSepFile = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pDatatype = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr((IntPtr)pCurrent));
        //                pCurrent += 4;
        //                Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].Attributes = (uint)Marshal.ReadInt32((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].Priority = (uint)Marshal.ReadInt32((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].DefaultPriority = (uint)Marshal.ReadInt32((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].StartTime = (uint)Marshal.ReadInt32((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].UntilTime = (uint)Marshal.ReadInt32((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].Status = (uint)Marshal.ReadInt32((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].cJobs = (uint)Marshal.ReadInt32((IntPtr)pCurrent);
        //                pCurrent += 4;
        //                Info2[i].AveragePPM = (uint)Marshal.ReadInt32((IntPtr)pCurrent);
        //                pCurrent += 4;
        //            }
        //        }
        //        else
        //        {
        //            Info2 = new PRINTER_INFO_2[0];
        //        }

        //    }
        //    return Info2;
        //}

        #endregion

        #region 托管代码

        /// <summary>
        /// 遍历打印机
        /// </summary>
        /// <param name="Flags"></param>
        /// <returns></returns>
        public static PRINTER_INFO_2[] EnumPrinters(PrinterEnumFlags Flags)
        {
            PRINTER_INFO_2[] Info2 = null;

            uint cbNeeded  = 0;
            uint cReturned = 0;

            bool   ret   = EnumPrinters(Flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);
            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);

            ret = EnumPrinters(Flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);

            if (ret)
            {
                Info2 = new PRINTER_INFO_2[cReturned];
                int offset = pAddr.ToInt32();
                for (int i = 0; i < cReturned; i++)
                {
                    Info2[i] = (PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), typeof(PRINTER_INFO_2));
                    offset  += Marshal.SizeOf(typeof(PRINTER_INFO_2));
                }

                Marshal.FreeHGlobal(pAddr);
            }

            return(Info2);
        }
Пример #9
0
        /// <summary>
        /// Get Printer Info
        /// </summary>
        /// <param name="printer"></param>
        /// <returns></returns>
        public PRINTER_INFO_2 GetPrinter(Printer printer)
        {
            IntPtr pHandle;
            PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();
            PRINTER_DEFAULTS defaults = new PRINTER_DEFAULTS();
            OpenPrinter(printer.Name, out pHandle, defaults);
            uint cbNeeded = 0;

            bool bRet = GetPrinter(pHandle, 2, IntPtr.Zero, 0, ref cbNeeded);
            if (cbNeeded > 0)
            {
                IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                bRet = GetPrinter(pHandle, 2, pAddr, cbNeeded, ref cbNeeded);
                if (bRet)
                {

                    Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
                }
                Marshal.FreeHGlobal(pAddr);

            }

            ClosePrinter(pHandle);
            return Info2;
        }
Пример #10
0
            private DEVMODE GetPrinterSettings(string PrinterName)
            {
                DEVMODE   dm;
                const int PRINTER_ACCESS_ADMINISTER = 0x4;
                const int PRINTER_ACCESS_USE        = 0x8;
                const int PRINTER_ALL_ACCESS        = (STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);

                PrinterValues.pDatatype     = 0;
                PrinterValues.pDevMode      = 0;
                PrinterValues.DesiredAccess = PRINTER_ALL_ACCESS;
                nRet = MyCommon.ToInt(OpenPrinter(PrinterName, out hPrinter, ref PrinterValues));
                if (nRet == 0)
                {
                    lastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out nBytesNeeded);
                if (nBytesNeeded <= 0)
                {
                    throw new System.Exception("Unable to allocate memory");
                }
                else
                {
                    ptrPrinterInfo = Marshal.AllocHGlobal(nBytesNeeded);
                    nRet           = MyCommon.ToInt(GetPrinter(hPrinter, 2, ptrPrinterInfo, nBytesNeeded, out nJunk));
                    if (nRet == 0)
                    {
                        lastError = Marshal.GetLastWin32Error();
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                    pinfo = (PRINTER_INFO_2)Marshal.PtrToStructure(ptrPrinterInfo, typeof(PRINTER_INFO_2));
                    IntPtr Temp = new IntPtr();
                    if (pinfo.pDevMode == IntPtr.Zero)
                    {
                        IntPtr ptrZero = IntPtr.Zero;
                        sizeOfDevMode = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, ptrZero, ref ptrZero, 0);
                        ptrDM         = Marshal.AllocCoTaskMem(sizeOfDevMode);
                        int i;
                        i = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, ptrDM,
                                               ref ptrZero, DM_OUT_BUFFER);
                        if ((i < 0) || (ptrDM == IntPtr.Zero))
                        {
                            throw new System.Exception("Cannot get DEVMODE data");
                        }
                        pinfo.pDevMode = ptrDM;
                    }
                    intError     = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, IntPtr.Zero, ref Temp, 0);
                    yDevModeData = Marshal.AllocHGlobal(intError);
                    intError     = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, yDevModeData, ref Temp, 2);
                    dm           = (DEVMODE)Marshal.PtrToStructure(yDevModeData, typeof(DEVMODE));
                    if ((nRet == 0) || (hPrinter == IntPtr.Zero))
                    {
                        lastError = Marshal.GetLastWin32Error();
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                    return(dm);
                }
            }
Пример #11
0
        /// <summary>
        /// 刷新对象数据
        /// </summary>
        /// <param name="printerName">打印机名称</param>
        public void Refresh()
        {
            myJobs = new PrintJobList();

            int            printer = 0;
            Win32Exception ext     = null;

            int result = OpenPrinterA(strPrinterName, ref printer, 0);

            if (result != 0)
            {
                int bufferSize   = 0;
                int recordNumber = 0;

                GetPrinter(printer, 2, IntPtr.Zero, 0, ref bufferSize);
                if (bufferSize > 0)
                {
                    IntPtr buf = Marshal.AllocHGlobal(bufferSize);
                    if (GetPrinter(printer, 2, buf, bufferSize, ref bufferSize) != 0)
                    {
                        this.myInfo = (PRINTER_INFO_2)Marshal.PtrToStructure(buf, typeof(PRINTER_INFO_2));
                    }
                    Marshal.FreeHGlobal(buf);
                }

                int structureSize = Marshal.SizeOf(typeof(JOB_INFO_1));
                EnumJobsA(printer, 0, 10000, 1, IntPtr.Zero, 0, ref bufferSize, ref recordNumber);
                if (bufferSize > 0)
                {
                    IntPtr p2 = Marshal.AllocHGlobal(bufferSize);
                    if (EnumJobsA(printer, 0, 1000, 1, p2, bufferSize, ref bufferSize, ref recordNumber) != 0)
                    {
                        for (int iCount = 0; iCount < recordNumber; iCount++)
                        {
                            IntPtr   p3  = new IntPtr((long)p2 + iCount * structureSize);
                            PrintJob job = new PrintJob();
                            UpdatePrintJob(job, p3);
                            job.PrinterName = strPrinterName;
                            this.Jobs.Add(job);
                        }
                    }
                    else
                    {
                        Marshal.FreeHGlobal(p2);
                        ClosePrinter(printer);
                        ext = new Win32Exception(Marshal.GetLastWin32Error());
                        throw ext;
                    }
                    Marshal.FreeHGlobal(p2);
                }
                ClosePrinter(printer);
            }
            else
            {
                ext = new Win32Exception(Marshal.GetLastWin32Error());
                throw ext;
            }
        }
Пример #12
0
        private DEVMODE GetPrinterSettings(string i_printerName)
        {
            DEVMODE dm;

            nRet = Convert.ToInt32(OpenPrinter(i_printerName, out hPrinter, ref PrinterValues));
            if (nRet == 0)
            {
                lastError = Marshal.GetLastWin32Error();
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out nBytesNeeded);
            if (nBytesNeeded <= 0)
            {
                throw new System.Exception("Unable to allocate memory by fujie");
            }
            else
            {
                ptrPrinterInfo = Marshal.AllocHGlobal(nBytesNeeded);
                nRet           = Convert.ToInt32(GetPrinter(hPrinter, 2, ptrPrinterInfo, nBytesNeeded, out nJunk));
                if (nRet == 0)
                {
                    lastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                pinfo = (PRINTER_INFO_2)Marshal.PtrToStructure(ptrPrinterInfo, typeof(PRINTER_INFO_2));
                IntPtr Temp = new IntPtr();
                if (pinfo.pDevMode == IntPtr.Zero)
                {
                    IntPtr ptrZero = IntPtr.Zero;
                    sizeOfDevMode = DocumentProperties(IntPtr.Zero, hPrinter, i_printerName, ptrZero, ref ptrZero, 0);
                    ptrDM         = Marshal.AllocCoTaskMem(sizeOfDevMode);
                    int i;
                    i = DocumentProperties(IntPtr.Zero, hPrinter, i_printerName, ptrDM, ref ptrZero, DM_OUT_BUFFER);
                    if ((i < 0) || (ptrDM == IntPtr.Zero))
                    {
                        throw new System.Exception("fujie:Cannot get DEVMODE data");
                    }
                    pinfo.pDevMode = ptrDM;
                }
                intError     = DocumentProperties(IntPtr.Zero, hPrinter, i_printerName, IntPtr.Zero, ref Temp, 0);
                yDevModeData = Marshal.AllocHGlobal(intError);
                intError     = DocumentProperties(IntPtr.Zero, hPrinter, i_printerName, yDevModeData, ref Temp, 2);
                dm           = (DEVMODE)Marshal.PtrToStructure(yDevModeData, typeof(DEVMODE));
                if ((nRet == 0) || (hPrinter == IntPtr.Zero))
                {
                    lastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                return(dm);
            }
        }
Пример #13
0
        internal static IntPtr CreatePrinter(string printerName)
        {
            PRINTER_INFO_2 printerInfo = new PRINTER_INFO_2();

            printerInfo.pPrinterName    = printerName;
            printerInfo.pDriverName     = "Microsoft XPS Document Writer v4";
            printerInfo.pPortName       = "PORTPROMPT:";
            printerInfo.pPrintProcessor = "winprint";
            printerInfo.pDatatype       = "RAW";

            IntPtr hPrinter = AddPrinter("", 2, ref printerInfo);

            return(hPrinter);
        }
Пример #14
0
    private bool GetPrinterStatus(string printerName)
    {
        PRINTER_INFO_2 PrinterInfo = GetPrinterInfo(printerName);

        Debug.Log(PrinterInfo.Status);
        switch (PrinterInfo.Status)
        {
        case 0:
        case 1024:
        case 131072:
            return(true);
        }
        return(false);
    }
Пример #15
0
        public List <String> GetInstalledPrintersUnmanaged()
        {
            //siehe http://msdn.microsoft.com/en-us/library/dd162692(v=vs.85).aspx

            uint cbNeeded  = 0;
            uint cReturned = 0;

            if (EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned))
            {
                //succeeds, but shouldn't, because buffer is zero (too small)!
                throw new Exception("EnumPrinters should fail!");
            }

            int lastWin32Error = Marshal.GetLastWin32Error();

            //ERROR_INSUFFICIENT_BUFFER = 122 expected, if not -> Exception
            if (lastWin32Error != 122)
            {
                throw new Win32Exception(lastWin32Error);
            }

            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);

            if (EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned))
            {
                PRINTER_INFO_2[] printerInfo2 = new PRINTER_INFO_2[cReturned];
                int  offset    = pAddr.ToInt32();
                Type type      = typeof(PRINTER_INFO_2);
                int  increment = Marshal.SizeOf(type);
                for (int i = 0; i < cReturned; i++)
                {
                    printerInfo2[i] = (PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), type);
                    offset         += increment;
                }
                Marshal.FreeHGlobal(pAddr);

                List <String> result = new List <string>();
                for (int i = 0; i < cReturned; i++)
                {
                    result.Add(printerInfo2[i].pPrinterName);
                }
                return(result);
            }

            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
Пример #16
0
        private static bool GetPrinterInfo2(string printerName, out int cJobs, out int status)
        {
            SafePrinterHandle printer = null;

            try
            {
                int needed = 0;
                if (OpenPrinter(printerName, out printer, IntPtr.Zero) &&
                    !GetPrinter(printer, 2, IntPtr.Zero, 0, out needed))
                {
                    int lastWin32Error = Marshal.GetLastWin32Error();
                    if (lastWin32Error == ERROR_INSUFFICIENT_BUFFER)
                    {
                        IntPtr pPrinter = Marshal.AllocHGlobal((int)needed);
                        try
                        {
                            if (GetPrinter(printer, 2, pPrinter, needed, out needed))
                            {
                                PRINTER_INFO_2 printerInfo2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pPrinter, typeof(PRINTER_INFO_2));
                                cJobs  = printerInfo2.cJobs;
                                status = printerInfo2.Status;
                                return(true);
                            }
                        }
                        finally
                        {
                            Marshal.FreeHGlobal(pPrinter);
                        }
                    }
                }
            }
            finally
            {
                if (printer != null)
                {
                    printer.Close();
                }
            }
            cJobs  = 0;
            status = 0;
            return(false);
        }
Пример #17
0
        private void CreatePrinter(string printerName, string portName)
        {
            PRINTER_INFO_2 _pInfo = new PRINTER_INFO_2
            {
                pPrinterName    = printerName,
                pPortName       = portName,
                pDriverName     = _xpsDriver,
                pPrintProcessor = _winProcessor
            };
            IntPtr hPrinter = AddPrinter(null, 2, ref _pInfo);

            if (hPrinter != IntPtr.Zero)
            {
                ClosePrinter(hPrinter);
            }
            else
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Пример #18
0
        private static PRINTER_INFO_2 GetPrinterSettings(string PrinterName)
        {
            const int PRINTER_ACCESS_ADMINISTER = 0x4;
            const int PRINTER_ACCESS_USE        = 0x8;
            const int PRINTER_ALL_ACCESS        = (STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);

            _PrinterValues.pDatatype     = IntPtr.Zero;
            _PrinterValues.pDevMode      = IntPtr.Zero;
            _PrinterValues.DesiredAccess = PRINTER_ALL_ACCESS;
            _NRet = Convert.ToInt32(OpenPrinter(PrinterName, out _hPrinter, ref _PrinterValues));
            if (_NRet == 0)
            {
                _LastError = Marshal.GetLastWin32Error();
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            GetPrinter(_hPrinter, 2, IntPtr.Zero, 0, out _NBytesNeeded);
            if (_NBytesNeeded <= 0)
            {
                throw new System.Exception("Unable to allocate memory");
            }
            else
            {
                _PtrPrinterInfo = Marshal.AllocHGlobal(_NBytesNeeded);

                _NRet = Convert.ToInt32(GetPrinter(_hPrinter, 2, _PtrPrinterInfo, _NBytesNeeded, out _NJunk));

                if (_NRet == 0)
                {
                    _LastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                _pinfo = (PRINTER_INFO_2)Marshal.PtrToStructure(_PtrPrinterInfo, typeof(PRINTER_INFO_2));

                if ((_NRet == 0) || (_hPrinter == IntPtr.Zero))
                {
                    _LastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                return(_pinfo);
            }
        }
Пример #19
0
    public static PRINTER_INFO_2 GetPrinterInfo(string printerName)
    {
        IntPtr hPrinter;

        if (!OpenPrinter(printerName, out hPrinter, IntPtr.Zero))
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        IntPtr pPrinterInfo = IntPtr.Zero;

        try
        {
            int needed;
            GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out needed);
            if (needed <= 0)
            {
                throw new Exception("失敗しました。");
            }

            pPrinterInfo = Marshal.AllocHGlobal(needed);

            int temp;
            if (!GetPrinter(hPrinter, 2, pPrinterInfo, needed, out temp))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            PRINTER_INFO_2 printerInfo = (PRINTER_INFO_2)Marshal.PtrToStructure(pPrinterInfo, typeof(PRINTER_INFO_2));

            return(printerInfo);
        }
        finally
        {
            //後始末をする
            ClosePrinter(hPrinter);
            Marshal.FreeHGlobal(pPrinterInfo);
        }
    }
Пример #20
0
        public static bool SharePrinter(string printerName, bool share)
        {
            try
            {
                _pinfo = GetPrinterSettings(printerName);
                _pinfo.pSecurityDescriptor = IntPtr.Zero;


                if (share)
                {
                    _pinfo.pShareName  = printerName;
                    _pinfo.Attributes |= 0x8;
                }
                else
                {
                    _pinfo.Attributes = _pinfo.Attributes & (~0x8);
                }

                Marshal.StructureToPtr(_pinfo, _PtrPrinterInfo, false);
                _LastError = Marshal.GetLastWin32Error();

                _NRet = Convert.ToInt16(SetPrinter(_hPrinter, 2, _PtrPrinterInfo, 0));

                if (_NRet == 0)
                {
                    _LastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                if (_hPrinter != IntPtr.Zero)
                {
                    ClosePrinter(_hPrinter);
                }
            }
            Marshal.FreeHGlobal(_PtrPrinterInfo);
            return(Convert.ToBoolean(_NRet));
        }
Пример #21
0
        public GenericResult AddPrinter(string printerName, string portName, string driverName)
        {
            GenericResult  retVal = new GenericResult("AddPrinter");
            PRINTER_INFO_2 pi     = new PRINTER_INFO_2();

            pi.pServerName         = null;
            pi.pPrinterName        = printerName;
            pi.pShareName          = "";
            pi.pPortName           = portName;
            pi.pDriverName         = driverName; // "Apple Color LW 12/660 PS";
            pi.pComment            = "Dokuflex";
            pi.pLocation           = "";
            pi.pDevMode            = new IntPtr(0);
            pi.pSepFile            = "";
            pi.pPrintProcessor     = "WinPrint";
            pi.pDatatype           = "RAW";
            pi.pParameters         = "";
            pi.pSecurityDescriptor = new IntPtr(0);

            try
            {
                if (AddPrinter(null, 2, ref pi) == 0)
                {
                    retVal.Exception = new Win32Exception(Marshal.GetLastWin32Error());
                    retVal.Message   = retVal.Exception.Message;
                }
            }
            catch (Exception ex)
            {
                retVal.Exception = ex;
                retVal.Message   = retVal.Exception.Message;
            }
            if (string.IsNullOrWhiteSpace(retVal.Message))
            {
                retVal.Success = true;
            }
            return(retVal);
        }
Пример #22
0
        public void AddPrinter(string printerName, string portName, string driverName, string comment)
        {
            var pi = new PRINTER_INFO_2
            {
                pServerName         = null,
                pPrinterName        = printerName,
                pShareName          = "",
                pPortName           = portName,
                pDriverName         = driverName,         // "Apple Color LW 12/660 PS";
                pComment            = comment,
                pLocation           = "",
                pDevMode            = new IntPtr(0),
                pSepFile            = "",
                pPrintProcessor     = "WinPrint",
                pDatatype           = "RAW",       // NULL?
                pParameters         = "",
                pSecurityDescriptor = new IntPtr(0)
            };

            if (AddPrinter(null, 2, ref pi) == 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Пример #23
0
 internal static void PrintInfo(PRINTER_INFO_2 info2)
 {
     cw("Attributes: ".PadRight(25) + info2.Attributes);
     cw("AveragePPM: ".PadRight(25) + info2.AveragePPM);
     cw("cJobs: ".PadRight(25) + info2.cJobs);
     cw("DefaultPriority: ".PadRight(25) + info2.DefaultPriority);
     cw("pComment: ".PadRight(25) + info2.pComment);
     cw("pDatatype: ".PadRight(25) + info2.pDatatype);
     cw("pDevMode: ".PadRight(25) + info2.pDevMode);
     cw("pDriverName: ".PadRight(25) + info2.pDriverName);
     cw("pLocation: ".PadRight(25) + info2.pLocation);
     cw("pParameters: ".PadRight(25) + info2.pParameters);
     cw("pPortName: ".PadRight(25) + info2.pPortName);
     cw("pPrinterName: ".PadRight(25) + info2.pPrinterName);
     cw("pPrintProcessor: ".PadRight(25) + info2.pPrintProcessor);
     cw("Priority: ".PadRight(25) + info2.Priority);
     cw("pSecurityDescriptor: ".PadRight(25) + info2.pSecurityDescriptor);
     cw("pSepFile: ".PadRight(25) + info2.pSepFile);
     cw("pServerName: ".PadRight(25) + info2.pServerName);
     cw("pShareName: ".PadRight(25) + info2.pShareName);
     cw("StartTime: ".PadRight(25) + info2.StartTime);
     cw("Status: ".PadRight(25) + info2.Status);
     cw("UntilTime: ".PadRight(25) + info2.UntilTime);
 }
 public static extern IntPtr AddPrinter(string pName, uint Level, [In] ref PRINTER_INFO_2 pPrinter);
Пример #25
0
        public void AddPrinter(string printerName, string portName, string driverName)
        {
            PRINTER_INFO_2 pi = new PRINTER_INFO_2();

            pi.pServerName = null;
            pi.pPrinterName = printerName;
            pi.pShareName = "";
            pi.pPortName = portName;
            pi.pDriverName = driverName;    // "Apple Color LW 12/660 PS";
            pi.pComment = "PrintToEVO";
            pi.pLocation = "";
            pi.pDevMode = new IntPtr(0);
            pi.pSepFile = "";
            pi.pPrintProcessor = "WinPrint";
            pi.pDatatype = "RAW";
            pi.pParameters = "";
            pi.pSecurityDescriptor = new IntPtr(0);

            var hPrt = AddPrinter(null, 2, ref pi);
            if (hPrt == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            ClosePrinter(hPrt);
        }
Пример #26
0
        /// <summary>
        /// Fetches data about the printers available on the designated server
        /// </summary>
        /// <param name="sServer"></param>
        /// <param name="dt">The DataTable into which the data should be placed. dt MUST have been created properly with CreateDataTable</param>
        public static Dictionary <int, string[]> FetchPrinterData(CredentialEntry ce, string sServer)
        {
            int    dwFlags      = (int)PEFlags.PRINTER_ENUM_NAME;
            string Name         = Canon(sServer);
            int    Level        = 2;
            IntPtr pPrinterEnum = IntPtr.Zero;
            int    cBufNeeded   = 0;
            int    cReturned    = 0;

            if (!Session.EnsureNullSession(sServer, ce))
            {
                return(null);
            }

            Dictionary <int, string[]> printInfoList = new Dictionary <int, string[]>();

            ///
            /// TODO: revise. Samba 3.0.13 fails if we pass in a null buffer. To avoid this
            /// pass in a pointer to a zero length buffer!
            ///

        #if SAMBAFIXED
            if (EnumPrinters(dwFlags, Name, Level, pPrinterEnum, 0, ref cBufNeeded, ref cReturned))
            {
                // Can only happen if no printers available!
                return;
            }
        #else
            {
            }
            pPrinterEnum = Marshal.AllocHGlobal(0);
            if (EnumPrinters(dwFlags, Name, Level, pPrinterEnum, 0, ref cBufNeeded, ref cReturned))
            {
                // free the mem
                Marshal.FreeHGlobal(pPrinterEnum);

                // Can only happen if no printers available!
                return(null);
            }
            // free the mem
            Marshal.FreeHGlobal(pPrinterEnum);
        #endif

            // Normally, we fail because we need a bigger buffer. However, if we fail
            // with RPC_S_CALL_FAILED, this means that we "succeeded" but that the remote
            // machine has nothing to tell us
            int nError = Marshal.GetLastWin32Error();

            if (nError == nErrorRPC_S_CALL_FAILED)
            {
                return(null);
            }

            if (nError != ERROR_INSUFFICIENT_BUFFER)
            {
                // Any other error results in an exception
                string sErr;
                if (nError == nErrorBadRPC)
                {
                    sErr = sBadServerName;
                }
                else if (nError == nErrorAccessDenied)
                {
                    throw new AuthException(new Win32Exception(nError));
                }
                else
                {
                    sErr = string.Format(sErrorMsg, sServer, nError);
                }
                throw new Win32Exception(sErr);
            }

            // allocate a big enough buffer
            pPrinterEnum = Marshal.AllocHGlobal(cBufNeeded);

            // retry
            if (!EnumPrinters(dwFlags, Name, Level, pPrinterEnum, cBufNeeded, ref cBufNeeded, ref cReturned))
            {
                nError = Marshal.GetLastWin32Error();
                Win32Exception we = new Win32Exception(nError);
                if (nError == nErrorAccessDenied)
                {
                    throw new AuthException(we);
                }
                else
                {
                    throw we;
                }
            }

            // iterate through the retrieved entries, copying printer entries
            // to the output table
            IntPtr pCur = pPrinterEnum;
            for (int i = 0; i < cReturned; i++)
            {
                // marshal the entry into
                PRINTER_INFO_2 pi2 = new PRINTER_INFO_2();
                Marshal.PtrToStructure(pCur, pi2);

                // trim off server name
                string sName = Marshal.PtrToStringUni(pi2.pPrinterName);
                if (sName.StartsWith(@"\\"))
                {
                    // yep, trim off prefix
                    int ibackwhack = sName.LastIndexOf(@"\");
                    sName = sName.Substring(ibackwhack + 1);
                }

                string[] printInfo = { Marshal.PtrToStringUni(pi2.pShareName), Marshal.PtrToStringUni(pi2.pDriverName), pi2.cJobs.ToString(), Marshal.PtrToStringUni(pi2.pComment) };

                // advance to the next entry
                pCur = (IntPtr)((int)pCur + Marshal.SizeOf(pi2));

                printInfoList.Add(i, printInfo);
            }

            // free the mem
            Marshal.FreeHGlobal(pPrinterEnum);

            return(printInfoList);
        }
 public static extern IntPtr AddPrinter(String pName, uint level, ref PRINTER_INFO_2 pPrinter);
Пример #28
0
 static extern IntPtr AddPrinter(string pPrinterName, int level, ref PRINTER_INFO_2 printerInfo);
Пример #29
0
 private static extern bool SetPrinter(IntPtr hPrinter, int Level, PRINTER_INFO_2
                                       pPrinter, int Command);
        internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
        {
            uint cbNeeded  = 0;
            uint cReturned = 0;
            bool ret       = EnumPrinters(Flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);

            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);

            ret = EnumPrinters(Flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);

            if (ret)
            {
                PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];

                int offset = pAddr.ToInt32();
                Console.WriteLine("offset:{0}", offset);

                for (int i = 0; i < cReturned; i++)
                {
                    Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    try
                    {
                        Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Exception:{0}", e.Message);
                    }
                    offset += 4;
                    Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    try
                    {
                        //Info2[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    }
                    catch (System.AccessViolationException e)
                    {
                        Console.WriteLine("Exception:{0}", e.Message);
                    }
                    offset += 4;
                    Info2[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                  += 4;
                    Info2[i].pComment        = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                  += 4;
                    Info2[i].pLocation       = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                  += 4;
                    Info2[i].pDevMode        = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset                  += 4;
                    Info2[i].pSepFile        = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                  += 4;
                    Info2[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                  += 4;
                    Info2[i].pDatatype       = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                  += 4;
                    //Info2[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset += 4;
                    Info2[i].Attributes = (uint)Marshal.ReadIntPtr(new IntPtr(offset));
                    offset                  += 4;
                    Info2[i].Priority        = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                  += 4;
                    Info2[i].DefaultPriority = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                  += 4;
                    Info2[i].StartTime       = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                  += 4;
                    Info2[i].UntilTime       = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                  += 4;
                    Info2[i].Status          = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                  += 4;
                    Info2[i].cJobs           = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                  += 4;
                    Info2[i].AveragePPM      = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                  += 4;
                }



                Marshal.FreeHGlobal(pAddr);

                return(Info2);
            }
            else
            {
                return(new PRINTER_INFO_2[0]);
            }
        }
Пример #31
0
        public static bool NewPrinter(string printerName, string portName, string driverName)
        {
            IntPtr hMonitor;
            IntPtr hPrinter;
            bool   bRes;
            int    iRes;
            uint   size, needed, xcvResult;

            PRINTER_DEFAULTS printerDefaults = new PRINTER_DEFAULTS();

            printerDefaults.pDatatype     = null;
            printerDefaults.pDevMode      = null;
            printerDefaults.DesiredAccess = SERVER_ACCESS_ADMINISTER;

            hMonitor = IntPtr.Zero;
            iRes     = OpenPrinter(",XcvMonitor Local Port", out hMonitor, printerDefaults);
            if (iRes == 0)
            {
                Console.WriteLine("[-] Error opening printer handle: {0}\n", Marshal.GetLastWin32Error());
                return(false);
            }

            if (!portName.EndsWith("\0"))
            {
                portName += "\0";
            }
            size = (uint)(portName.Length * 2);
            IntPtr portPtr = Marshal.AllocHGlobal((int)size);

            Marshal.Copy(portName.ToCharArray(), 0, portPtr, portName.Length);

            bRes = XcvData(hMonitor, "AddPort", portPtr, size, IntPtr.Zero, 0, out needed, out xcvResult);
            if (bRes == false)
            {
                Console.WriteLine("[-] Failed to add port: {0}\n", Marshal.GetLastWin32Error());
                Marshal.FreeHGlobal(portPtr);
                ClosePrinter(hMonitor);
                return(false);
            }

            iRes = InstallPrinterDriverFromPackage(null, null, driverName, null, 0);
            if (iRes != 0)
            {
                Console.WriteLine("[-] Failed to install print driver: {0}\n", Marshal.GetLastWin32Error());
                Marshal.FreeHGlobal(portPtr);
                ClosePrinter(hMonitor);
                return(false);
            }

            PRINTER_INFO_2 printerinfo2 = new PRINTER_INFO_2();

            printerinfo2.pPortName       = portName;
            printerinfo2.pDriverName     = driverName;
            printerinfo2.pPrinterName    = printerName;
            printerinfo2.pPrintProcessor = "WinPrint";
            printerinfo2.pDatatype       = "RAW";
            printerinfo2.pComment        = "I'd be careful with this one...";
            printerinfo2.pLocation       = "Inside of an exploit";
            printerinfo2.Attributes      = PRINTER_ATTRIBUTE_RAW_ONLY | PRINTER_ATTRIBUTE_HIDDEN;
            printerinfo2.AveragePPM      = 9001;

            hPrinter = IntPtr.Zero;
            hPrinter = AddPrinter(null, 2, printerinfo2);
            if (hPrinter == IntPtr.Zero)
            {
                Console.WriteLine("[-] Failed to create printer: {0}\n", Marshal.GetLastWin32Error());
                Marshal.FreeHGlobal(portPtr);
                ClosePrinter(hMonitor);
                return(false);
            }

            Marshal.FreeHGlobal(portPtr);
            ClosePrinter(hMonitor);
            ClosePrinter(hPrinter);

            return(true);
        }
Пример #32
0
        public static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
        {
            uint cbNeeded  = 0;
            uint cReturned = 0;
            bool ret       = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_CONNECTIONS | PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);

            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);

            ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_CONNECTIONS | PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);

            if (ret)
            {
                PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];

                int offset = pAddr.ToInt32();

                for (int i = 0; i < cReturned; i++)
                {
                    Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pPortName           = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pDriverName         = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pComment            = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pLocation           = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pDevMode            = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].pSepFile            = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pPrintProcessor     = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pDatatype           = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pParameters         = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset                      += 4;
                    Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].Attributes          = (uint )Marshal.ReadIntPtr(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].Priority            = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].DefaultPriority     = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].StartTime           = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].UntilTime           = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].Status              = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].cJobs               = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                    Info2[i].AveragePPM          = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset                      += 4;
                }

                Marshal.FreeHGlobal(pAddr);

                return(Info2);
            }
            else
            {
                return(new PRINTER_INFO_2[0]);
            }
        }
        internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
        {
            uint cbNeeded = 0;
            uint cReturned = 0;
            bool ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);

            IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
            ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);

            if (ret)
            {
                PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];

                int offset = pAddr.ToInt32();

                for (int i = 0; i < cReturned; i++)
                {
                    Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pComment = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pLocation = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pDevMode = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset += 4;
                    Info2[i].pSepFile = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pDatatype = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset += 4;
                    Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
                    offset += 4;
                    Info2[i].Attributes = (uint)Marshal.ReadIntPtr(new IntPtr(offset));
                    offset += 4;
                    Info2[i].Priority = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].DefaultPriority = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].StartTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].UntilTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].Status = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].cJobs = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;
                    Info2[i].AveragePPM = (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset += 4;

                }

                Marshal.FreeHGlobal(pAddr);

                return Info2;

            }
            else
            {
                return new PRINTER_INFO_2[0];
            }
        }
Пример #34
0
 static extern IntPtr AddPrinter(string printerName, uint Level, PRINTER_INFO_2 printerinfo);
Пример #35
0
 /// <summary>
 /// Fetches data about the printers available on the designated server
 /// </summary>
 /// <param name="sServer"></param>
 /// <param name="dt">The DataTable into which the data should be placed. dt MUST have been created properly with CreateDataTable</param>
 public static Dictionary<int, string[]> FetchPrinterData(CredentialEntry ce, string sServer)
 {
     int dwFlags = (int)PEFlags.PRINTER_ENUM_NAME;
     string Name = Canon(sServer);
     int Level = 2;
     IntPtr pPrinterEnum = IntPtr.Zero;
     int cBufNeeded = 0;
     int cReturned = 0;
     
     if (!Session.EnsureNullSession(sServer, ce))
     {
         return null;
     }
     
     Dictionary<int, string[]> printInfoList = new Dictionary<int, string[]>();
     
     ///
     /// TODO: revise. Samba 3.0.13 fails if we pass in a null buffer. To avoid this
     /// pass in a pointer to a zero length buffer!
     ///
     
     #if SAMBAFIXED
     if (EnumPrinters( dwFlags, Name, Level, pPrinterEnum, 0, ref cBufNeeded, ref cReturned))
     {
         // Can only happen if no printers available!
         return;
     }
     #else
     {
         
     }
     pPrinterEnum = Marshal.AllocHGlobal(0);
     if (EnumPrinters(dwFlags, Name, Level, pPrinterEnum, 0, ref cBufNeeded, ref cReturned))
     {
         // free the mem
         Marshal.FreeHGlobal(pPrinterEnum);
         
         // Can only happen if no printers available!
         return null;
     }
     // free the mem
     Marshal.FreeHGlobal(pPrinterEnum);
     #endif
     
     // Normally, we fail because we need a bigger buffer. However, if we fail
     // with RPC_S_CALL_FAILED, this means that we "succeeded" but that the remote
     // machine has nothing to tell us
     int nError = Marshal.GetLastWin32Error();
     
     if (nError == nErrorRPC_S_CALL_FAILED)
     {
         return null;
     }
     
     if (nError != ERROR_INSUFFICIENT_BUFFER)
     {
         // Any other error results in an exception
         string sErr;
         if (nError == nErrorBadRPC)
         {
             sErr = sBadServerName;
         }
         else if (nError == nErrorAccessDenied)
         {
             throw new AuthException(new Win32Exception(nError));
         }
         else
         {
             sErr = string.Format(sErrorMsg, sServer, nError);
         }
         throw new Win32Exception(sErr);
     }
     
     // allocate a big enough buffer
     pPrinterEnum = Marshal.AllocHGlobal(cBufNeeded);
     
     // retry
     if (!EnumPrinters(dwFlags, Name, Level, pPrinterEnum, cBufNeeded, ref cBufNeeded, ref cReturned))
     {
         nError = Marshal.GetLastWin32Error();
         Win32Exception we = new Win32Exception(nError);
         if (nError == nErrorAccessDenied)
         {
             throw new AuthException(we);
         }
         else
         {
             throw we;
         }
     }
     
     // iterate through the retrieved entries, copying printer entries
     // to the output table
     IntPtr pCur = pPrinterEnum;
     for (int i = 0; i < cReturned; i++)
     {
         // marshal the entry into
         PRINTER_INFO_2 pi2 = new PRINTER_INFO_2();
         Marshal.PtrToStructure(pCur, pi2);
         
         // trim off server name
         string sName = Marshal.PtrToStringUni(pi2.pPrinterName);
         if (sName.StartsWith(@"\\"))
         {
             // yep, trim off prefix
             int ibackwhack = sName.LastIndexOf(@"\");
             sName = sName.Substring(ibackwhack + 1);
         }
         
         string[] printInfo ={ Marshal.PtrToStringUni(pi2.pShareName), Marshal.PtrToStringUni(pi2.pDriverName), pi2.cJobs.ToString(), Marshal.PtrToStringUni(pi2.pComment) };
         
         // advance to the next entry
         pCur = (IntPtr)((int)pCur + Marshal.SizeOf(pi2));
         
         printInfoList.Add(i, printInfo);
     }
     
     // free the mem
     Marshal.FreeHGlobal(pPrinterEnum);
     
     return printInfoList;
 }
Пример #36
0
 private static extern int AddPrinter(string pName, uint Level, [In] ref PRINTER_INFO_2 pPrinter);
Пример #37
0
        internal static int GetPrinterStatusInt(string PrinterName)
        {
            int intRet = 0;
            IntPtr hPrinter;
            structPrinterDefaults defaults = new structPrinterDefaults();

            if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
            {
                int cbNeeded = 0;
                bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
                if (cbNeeded > 0)
                {
                    IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                    bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
                    if (bolRet)
                    {
                        PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();

                        Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));

                        intRet = System.Convert.ToInt32(Info2.Status);
                    }
                    Marshal.FreeHGlobal(pAddr);
                }
                ClosePrinter(hPrinter);
            }

            return intRet;
        }