知识大全 枚举网打印机

Posted

篇首语:临文乍了了,彻卷兀若无。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 枚举网打印机相关的知识,希望对你有一定的参考价值。

Delphi必学技巧:枚举网打印机  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

在某些情况下 我们需要枚举网内的打印机 试试以下的代码 很有可能帮得了你       using System;    using System Runtime InteropServices;    using System Runtime Serialization;    namespace Sample GetPrinter        class App        static void Main(string[] args)        Console WriteLine( 请输入你想搜索类型的序号: );    Console WriteLine( Default printer(only Win Win WinME)\\n Enumerates the locally installed printers;\\n Enumerates the list of printers to which the user has made previous connections;\\n Enumerates the printer identified by Name ;\\n Enumerates neork printers and print servers in the puter s domain;\\n Enumerates printers that have the shared attribute;\\n Enumerates neork printers in the puter s domain;\\n========================== );        int pt = ;    try        pt=Int Parse(Console ReadLine());        catch(Exception e)        Console WriteLine( 错误信息: e Message);    return;          PRINTER_ENUM printerKind = GetPrinterConType(pt);      PrinterSearcher p = new PrinterSearcher();      PrinterSearcher PrinterInfo[] printers = p Search(printerKind);      foreach(PrinterSearcher PrinterInfo pi in printers)        Console WriteLine( =====================================\\n打印机名: \\n描叙:: \\n注释: \\n=====================================\\n     pi Name pi Description pi Comment);              static PRINTER_ENUM GetPrinterConType(int ins)        switch(ins)        case :    return PRINTER_ENUM DEFAULT ;    case :    return PRINTER_ENUM LOCAL;    case :    return PRINTER_ENUM CONNECTIONS;    case :    return PRINTER_ENUM NAME;    case :    return PRINTER_ENUM REMOTE;      case :    return PRINTER_ENUM SHARED;    case :    return PRINTER_ENUM NEORK;    default:    return PRINTER_ENUM LOCAL ;                  #region 打印机位置状态枚举 PRINTER_ENUM    public enum PRINTER_ENUM        DEFAULT = x     LOCAL = x     CONNECTIONS = x     NAME = x     REMOTE = x     SHARED = x     NEORK = x         #endregion      #region 异常派生 EnumPrinterException    [Serializable]    public class EnumPrinterException: ApplicationException        public EnumPrinterException()     public EnumPrinterException (string message): base(message)     public EnumPrinterException (string message Exception inner):    base(message inner)     protected EnumPrinterException (SerializationInfo info     StreamingContext context) : base(info context)             #endregion      //加上这个属性可以按导出到非托管对像的顺序排序    [StructLayout(LayoutKind Sequential CharSet = CharSet Auto)]    public class PRINTER_INFO_         public int flags;    public IntPtr pDescription;    public IntPtr pName;    public IntPtr pComment;          public class PrinterSearcher        #region Search    public PrinterInfo[] Search(PRINTER_ENUM printerKind)        PrinterInfo[] pInfo = new PrinterInfo[ ];      uint iNeeded = iReturned = iSize = ;    IntPtr printers = IntPtr Zero;      if (!EnumPrinters(printerKind String Empty    printers   ref iNeeded ref iReturned) )          //返回由上一个非托管函数返回的错误代码 该函数是使用设置了     //DllImport属性中SetLastError=true 标志的平台调用来调用的    int err = Marshal GetLastWin Error();    if (err != Win Error ERROR_INSUFFICIENT_BUFFER)    ThrowEnumPrinterException();          iSize = iNeeded;      if (iNeeded != )        try        //使用AllocHGlobal分配非托管内块    printers = Marshal AllocHGlobal(new IntPtr(iSize));        //如果调用不成功抛出异常    if (!EnumPrinters(printerKind String Empty printers iSize ref iNeeded ref iReturned) )        ThrowEnumPrinterException();        else        pInfo = GetPrinterInfoFromMemory(printers iReturned);            finally        //释放分配的内存块    if (printers != IntPtr Zero)    Marshal FreeHGlobal(printers);            return pInfo;        #endregion      #region PrinterInfo    public struct PrinterInfo        public string Name;    public string Description;    public string Comment;              public sealed class Win Error        private Win Error()     public const int ERROR_INSUFFICIENT_BUFFER = ;        #endregion      #region EnumPrinters    [DllImport( winspool drv SetLastError = true CharSet = CharSet Auto)]    [return: MarshalAs(UnmanagedType Bool)]      private static extern bool EnumPrinters ([MarshalAs(UnmanagedType U )] PRINTER_ENUM flags     [MarshalAs(UnmanagedType LPStr)] string sName     uint iLevel     IntPtr pPrinterDesc     uint iSize     [MarshalAs(UnmanagedType U )] ref uint iNeeded     [MarshalAs(UnmanagedType U )] ref uint iReturned    );    #endregion    #region GetPrinterInfoFromMemory    private PrinterInfo[] GetPrinterInfoFromMemory(IntPtr prInfo uint numPrinters)        PRINTER_INFO_ pi = new PRINTER_INFO_ ();        PrinterInfo[] pInfo = new PrinterInfo[numPrinters];      for(int i = ; i < numPrinters; i++)        //把数据从非托管内存传送到到托管内存    Marshal PtrToStructure(prInfo pi);        pInfo[i] Name = Marshal PtrToStringAuto(pi pName);    pInfo[i] Description = Marshal PtrToStringAuto(pi pDescription);    pInfo[i] Comment = Marshal PtrToStringAuto(pi pComment);      prInfo = new IntPtr(prInfo ToInt () + Marshal SizeOf(typeof(PRINTER_INFO_ )));        return pInfo;          private void ThrowEnumPrinterException()        throw new EnumPrinterException(string Format( LastErrorCode:     Marshal GetLastWin Error()));        #endregion         cha138/Article/program/Delphi/201311/8424

相关参考

知识大全 大哥,目前我从事UV打印机销售,我怎么找客户

大哥,目前我从事UV打印机销售,我怎么找客户?  以下文字资料是由(本站网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!大哥,目

知识大全 C# 语言规范--1.11 枚举

C#语言规范--1.11枚举  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  枚举类型声明为一组相

知识大全 Aisino SK-820财税高清票据打印机打印不了发票是为什么啊

AisinoSK-820财税高清票据打印机打印不了发票是为什么啊?  以下文字资料是由(本站网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起

知识大全 Hibernate如何映射枚举类型

Hibernate如何映射枚举类型  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  问题  Jav

知识大全 自定义控件中使用枚举类型的属性

  在控件中定义枚举类型的属性  publicenumDTableStateSelectShowAddnew=SaveAddnew=ShowEdit=SaveEdit=Delete=Show=List

知识大全 拷贝枚举器以加强效率

Java技巧:拷贝枚举器以加强效率  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  当一个代码段正

知识大全 OA签证的资料怎样打印

OA签证的资料怎样打印  以下文字资料是由(本站网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!OA签证的资料怎样打印有直接在网

知识大全 驯服Tiger之深入研究枚举类型

驯服Tiger之深入研究枚举类型  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  基础知识    

知识大全 Java中的枚举器(反复器)

Java中的枚举器(反复器)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在任何集合类中必须通

知识大全 用新VB.NET 枚举提高效率

用新VB.NET枚举提高效率  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  当你还使用For…E