logo资料库

C#获取打印机状态+API函数详解.pdf

第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
资料共14页,剩余部分请下载后查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; using System.Drawing.Printing; namespace testprinter2 { public partial class Form1 : Form { #region 预定义类型 [ FlagsAttribute public ] enum PrinterEnumFlags { PRINTER_ENUM_DEFAULT = 0x00000001, PRINTER_ENUM_LOCAL = 0x00000002, PRINTER_ENUM_CONNECTIONS = 0x00000004, PRINTER_ENUM_FAVORITE = 0x00000004, PRINTER_ENUM_NAME = 0x00000008, PRINTER_ENUM_REMOTE = 0x00000010, PRINTER_ENUM_SHARED = 0x00000020, PRINTER_ENUM_NETWORK = 0x00000040, PRINTER_ENUM_EXPAND = 0x00004000, PRINTER_ENUM_CONTAINER = 0x00008000, PRINTER_ENUM_ICONMASK = 0x00ff0000, PRINTER_ENUM_ICON1 = 0x00010000, PRINTER_ENUM_ICON2 = 0x00020000, PRINTER_ENUM_ICON3 = 0x00040000, PRINTER_ENUM_ICON4 = 0x00080000, PRINTER_ENUM_ICON5 = 0x00100000,
PRINTER_ENUM_ICON6 = 0x00200000, PRINTER_ENUM_ICON7 = 0x00400000, PRINTER_ENUM_ICON8 = 0x00800000, PRINTER_ENUM_HIDE = 0x01000000 } StructLayout struct public ( LayoutKind .Sequential)] PRINTER_INFO_2 string pServerName; string pPortName; string pDriverName; string pPrinterName; string pShareName; string pComment; string pLocation; pDevMode; IntPtr string pSepFile; MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public public MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public MarshalAs ( UnmanagedType.LPTStr)] public public public public public public public public public public string pParameters; IntPtr uint uint uint uint uint uint uint uint Attributes; Priority; DefaultPriority; StartTime; UntilTime; Status; cJobs; AveragePPM; string pPrintProcessor; string pDatatype; pSecurityDescriptor; [ { [ [ [ [ [ [ [ [ [ [ [ } #endregion
#region 引用 WindowsAPI [ true )] [ // 引用 API声明 DllImport ( "winspool.drv" , CharSet = CharSet .Auto, SetLastError = return : MarshalAs ( UnmanagedType.Bool)] extern bool EnumPrinters( public static PrinterEnumFlags Flags, string Name, uint IntPtr uint ref uint ref uint pcbNeeded, pcReturned pPrinterEnum, Level, cbBuf, ); #endregion /// /// 遍历打印机 /// /// /// [ ( "User32.dll" )] DllImport public static extern int MessageBox(int h, string m, string c, int type); public Form1() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { PRINTER_INFO_2[] Info2 = null ; uint uint cbNeeded = 0; cReturned = 0; bool ret = EnumPrinters( PrinterEnumFlags .PRINTER_ENUM_LOCAL, null , 2, IntPtr .Zero, 0, IntPtr ref cbNeeded, ref cReturned); pAddr = Marshal .AllocHGlobal(( int )cbNeeded); ret = EnumPrinters( PrinterEnumFlags .PRINTER_ENUM_LOCAL, null , 2,
pAddr, cbNeeded, ref cbNeeded, ref cReturned); if (ret) { Info2 = new PRINTER_INFO_2[cReturned]; offset = pAddr.ToInt32(); int for ( int i = 0; i < cReturned; i++) PRINTER_INFO_2) Marshal .PtrToStructure( new { Info2[i] = ( IntPtr offset += (offset), typeof ( PRINTER_INFO_2)); Marshal .SizeOf( this .listBox1.Items.Add( typeof "打印机 ( PRINTER_INFO_2)); " +Info2[i].pPrinterName + " 驱动为 : " + Info2[i].pDriverName); this .listBox1.Items.Add( "" ); } } } Marshal .FreeHGlobal(pAddr); //int a = MessageBox(0, Info2[0].pDriverName, " 有 ", 0); } //end form } //end namespace
EnumPrinters 函数中文简介 BOOL EnumPrinters( DWORD Flags, // types of printer objects to enumerate LPTSTR Name, // name of printer object DWORD Level, // specifies type of printer info structure LPBYTE pPrinterEnum, // pointer to buffer to receive printer info structures DWORD cbBuf, // size, in bytes, of array LPDWORD pcbNeeded, // pointer to variable with no. of bytes copied (or required) LPDWORD pcReturned // pointer to variable with no. of printer info. structures copied ); 这个 API 用于了解可用的打印机的信息 参数的意义是 : Flags: 可以是 PRINTER_ENUM_LOCAL, PRINTER_ENUM_NAME, PRINTER_ENUM_SHARED, PRINTER_ENUM_DEFAULT, PRINTER_ENUM_CONNECTIONS等或者某些合法的组合 Name: 与 Flag 相关的名字 , 例如, 服务器名 , 域名等 Level: 您希望返回的结构类型 , 95 可以用 1, 2, 5, NT 可以用 1, 2, 4, 5 分别代表 PRINTER_INFO_1, PRINTER_INFO_2, PRINTER_INFO_4, PRINTER_INFO_5 各个结构的不同点是返回信息的详尽程度或者用途不同 pPrinterEnum: 您提供的一个接受返回结果的缓冲区 cbBuf: 这个缓冲区的大小 (in Bytes)
pcbNeeded: 您提供的空间 , API 调用返回时 , 将告诉您用了 pPrinterEnum 多少字节 ( 成功 时), 或者需要多少字节 ( 如果空间不够 ) pcReturned: 您提供的空间 , API 调用返回时 , 将告诉您到底返回了多少个 level 中要求的结 构 EnumPrinters 返回非零值表示调用成功 . EnumPrinters EnumPrinters VB 声明 Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" (ByVal flags As Long, ByVal name As String, ByVal Level As Long, pPrinterEnum As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long 说明 枚举系统中安装的打印机 返回值 Long,非零表示成功,零表示失败。会设置 GetLastError 参数表 参数 类型及说明 flags Long,一个或多个下述标志 PRINTER_ENUM_LOCAL 枚举 本地打印机 (包括 Windows 95 中的网络打印机) 。名字会被忽 略 PRINTER_ENUM_NAME 枚举由 name 参数指定的打印机。 其中的名字可以是一个供应商、 域 或服务器。如 name 为 NULL,则枚举出可用的打印机 PRINTER_ENUM_SHARE 枚举共享打印机(必须同其他常数组合使用) PRINTER_ENUM_CONNECTIONS 枚举网络连接列表中的打印机(即使目前没有连接 用于 NT) ——仅适 PRINTER_ENUM_NETWORK 枚举通过网络连接的打印机。级别( Level)必须为 1。仅适用于 NT PRINTER_ENUM_REMOTE 枚举通过网络连接的打印机和 打印服务器 。级别必须为 1。仅适用
于 NT name String, vbNullString 表示枚举同本机连接的打印机。否则由标志和级别决定 Level Long, 1, 2,4 或 5( 4 仅适用于 NT; 5 仅适用于 Win95 和 NT 4.0),指定欲枚举的结 构的类型。如果是 1,则 name 参数由标志设置决定。如果是 2 或 5,那么 name 就代表欲 对 其 打 印 机 进 行 枚 举 的 服 务 器 的 名 字; 或 者 为 vbNullString 。 如 果 是 4, 那 么 只 有 PRINTER_ENUM_LOCAL和 PRINTER_ENUM_CONNECTIONS才有效。名字必须是 vbNullString pPrinterEnum Byte ,包含 PRINTER_ENUM_x结构的缓冲区,其中的 x 代表级别( Level) cbBuf Long, pPrinterEnum 缓冲区中的字符数量 pcbNeeded Long,指向一个 Long 型 变量 的 指针 ,该变量用于保存请求的缓冲区长度,或者 实际读入的字节数量 pcReturned Long,载入缓冲区的结构数量(用于那些能返回多个结构的函数) 注解 第 4 和第 5 级将它们的结构建立在 系统注册表 的基础上, 而且比第 2 级快得多。 后者要求每 台打印机都处于打开状态 C#中 Typeof 是干什么的? 2011-10-21 06:39 匿名 | 分类: 编程语言 | 浏览 5704 次 提问者采纳 2011-10-21 07:25 C# typeof() 和 GetType() 区是什么? 1、typeof(x) 中的 x,必须是具体的类名、类型名称等,不可以是变量名称。 2、GetType() 方法继承自 Object ,所以 C#中任何对象都具有 GetType() 方法, 它的作用和 typeof() 相同,返回 Type 类型的当前对象的类型。 比如有这样一个变量 i: Int32 i = new Int32();
i.GetType() 返回值是 Int32 的类型,但是无法使用 typeof(i) ,因为 i 是一个变量, 如果要使用 typeof() ,则只能: typeof(Int32) ,返回的同样是 Int32 的类型。 C#中 int 和 IntPtr 相互转换 方法一、 int 转 IntPtr int i = 12; IntPtr p = new IntPtr(i); IntPtr 转 int int myi = (int)p; MessageBox.Show(myi.ToString()); 方法二、 int 转 IntPtr int i = 12; IntPtr p =(IntPtr)i IntPtr 转 int int myi = (int)p; MessageBox.Show(myi.ToString()); IntPtr 问题
分享到:
收藏