分享
 
 
 

枚举打印机(winapi)

王朝system·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

using System;

using System.Runtime.InteropServices;

using System.Runtime.Serialization;

namespace Sample.GetPrinter

{

class App

{

static void Main(string[] args)

{

Console.WriteLine("请输入你想搜索类型的序号:");

Console.WriteLine("1. Default printer(only Win95,Win98,WinME)\n2. Enumerates the locally installed printers;\n3. Enumerates the list of printers to which the user has made previous connections;\n4. Enumerates the printer identified by Name.;\n5. Enumerates network printers and print servers in the computer's domain;\n6. Enumerates printers that have the shared attribute;\n7. Enumerates network printers in the computer's domain;\n======================================================================");

int pt =0;

try

{

pt=Int32.Parse(Console.ReadLine());

}

catch(Exception e)

{

Console.WriteLine("错误信息: {0}",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打印机名: {0}\n描叙:: {1}\n注释: {2}\n=====================================\n",

pi.Name, pi.Description, pi.Comment);

}

}

static PRINTER_ENUM GetPrinterConType(int ins)

{

switch(ins)

{

case 1:

return PRINTER_ENUM.DEFAULT ;

case 2:

return PRINTER_ENUM.LOCAL;

case 3:

return PRINTER_ENUM.CONNECTIONS;

case 4:

return PRINTER_ENUM.NAME;

case 5:

return PRINTER_ENUM.REMOTE;

case 6:

return PRINTER_ENUM.SHARED;

case 7:

return PRINTER_ENUM.NETWORK;

default:

return PRINTER_ENUM.LOCAL ;

}

}

}

#region 打印机位置状态枚举 PRINTER_ENUM

public enum PRINTER_ENUM

{

DEFAULT = 0x01,

LOCAL = 0x02,

CONNECTIONS = 0x04,

NAME = 0x08,

REMOTE = 0x10,

SHARED = 0x20,

NETWORK = 0x40

}

#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_1

{

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[0];

uint iNeeded = 0, iReturned = 0, iSize = 0;

IntPtr printers = IntPtr.Zero;

if (!EnumPrinters(printerKind, String.Empty, 1, printers, 0, ref iNeeded, ref iReturned) )

{

//返回由上一个非托管函数返回的错误代码,该函数是使用设置了 -

//DllImport属性中SetLastError=true 标志的平台调用来调用的

int err = Marshal.GetLastWin32Error();

if (err != Win32Error.ERROR_INSUFFICIENT_BUFFER)

ThrowEnumPrinterException();

}

iSize = iNeeded;

if (iNeeded != 0)

{

try

{

//使用AllocHGlobal分配非托管内块

printers = Marshal.AllocHGlobal(new IntPtr(iSize));

//如果调用不成功抛出异常

if (!EnumPrinters(printerKind, String.Empty,1,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 Win32Error

{

private Win32Error() {}

public const int ERROR_INSUFFICIENT_BUFFER = 122;

}

#endregion

#region EnumPrinters

[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Auto)]

[return: MarshalAs(UnmanagedType.Bool)]

private static extern bool EnumPrinters ([MarshalAs(UnmanagedType.U4)] PRINTER_ENUM flags,

[MarshalAs(UnmanagedType.LPStr)] string sName,

uint iLevel,

IntPtr pPrinterDesc,

uint iSize,

[MarshalAs(UnmanagedType.U4)] ref uint iNeeded,

[MarshalAs(UnmanagedType.U4)] ref uint iReturned

);

#endregion

#region GetPrinterInfoFromMemory

private PrinterInfo[] GetPrinterInfoFromMemory(IntPtr prInfo, uint numPrinters)

{

PRINTER_INFO_1 pi = new PRINTER_INFO_1();

PrinterInfo[] pInfo = new PrinterInfo[numPrinters];

for(int i = 0; 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.ToInt32() + Marshal.SizeOf(typeof(PRINTER_INFO_1)));

}

return pInfo;

}

private void ThrowEnumPrinterException()

{

throw new EnumPrinterException(string.Format("LastErrorCode: {0}",

Marshal.GetLastWin32Error()));

}

#endregion

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有