C#获取打印机状态+API函数详解_第1页
C#获取打印机状态+API函数详解_第2页
C#获取打印机状态+API函数详解_第3页
C#获取打印机状态+API函数详解_第4页
C#获取打印机状态+API函数详解_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、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 pa

2、rtial 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_

3、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,

4、PRINTER_ENUM_ICON5 = 0x00100000, PRINTER_ENUM_ICON6 = 0x00200000, PRINTER_ENUM_ICON7 = 0x00400000, PRINTER_ENUM_ICON8 = 0x00800000, PRINTER_ENUM_HIDE = 0x01000000 StructLayout(LayoutKind.Sequential) public struct PRINTER_INFO_2 MarshalAs(UnmanagedType.LPTStr) public string pServerName; MarshalAs(Unm

5、anagedType.LPTStr) public string pPrinterName; MarshalAs(UnmanagedType.LPTStr) public string pShareName; MarshalAs(UnmanagedType.LPTStr) public string pPortName; MarshalAs(UnmanagedType.LPTStr) public string pDriverName; MarshalAs(UnmanagedType.LPTStr) public string pComment; MarshalAs(UnmanagedType

6、.LPTStr) public string pLocation; public IntPtr pDevMode; MarshalAs(UnmanagedType.LPTStr) public string pSepFile; MarshalAs(UnmanagedType.LPTStr) public string pPrintProcessor; MarshalAs(UnmanagedType.LPTStr) public string pDatatype; MarshalAs(UnmanagedType.LPTStr) public string pParameters; public

7、IntPtr pSecurityDescriptor; public uint Attributes; public uint Priority; public uint DefaultPriority; public uint StartTime; public uint UntilTime; public uint Status; public uint cJobs; public uint AveragePPM; #endregion #region 引用 WindowsAPI /引用API声明 DllImport("winspool.drv", CharSet =

8、CharSet.Auto, SetLastError = true) return: MarshalAs(UnmanagedType.Bool) public static extern bool EnumPrinters( PrinterEnumFlags Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned ); #endregion / <summary> / 遍历打印机 / </summary> / <

9、param name="Flags"></param> / <returns></returns> DllImport("User32.dll") 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 Inf

10、o2 = null; 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, r

11、ef cbNeeded, ref cReturned); if (ret) Info2 = new PRINTER_INFO_2cReturned; int offset = pAddr.ToInt32(); for (int i = 0; i < cReturned; i+) Info2i = (PRINTER_INFO_2)Marshal.PtrToStructure(new IntPtr(offset), typeof(PRINTER_INFO_2); offset += Marshal.SizeOf(typeof(PRINTER_INFO_2); this.listBox1.It

12、ems.Add("打印机 "+Info2i.pPrinterName + " 驱动为: " + Info2i.pDriverName); this.listBox1.Items.Add(""); Marshal.FreeHGlobal(pAddr); /int a = MessageBox(0, Info20.pDriverName, "有", 0); /end form /end namespaceEnumPrinters函数中文简介BOOL EnumPrinters( DWORD Flags, / types

13、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 structuresDWORD cbBuf, / size, in bytes, of array LPDWORD pcbNeeded, / pointer to variable with no. of bytes

14、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:您希望返回的

15、结构类型, 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中要求的结构

16、EnumPrinters返回非零值表示调用成功.EnumPrintersEnumPrinters 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

17、说明 枚举系统中安装的打印机 返回值 Long,非零表示成功,零表示失败。会设置GetLastError 参数表 参数 类型及说明 flags Long,一个或多个下述标志 PRINTER_ENUM_LOCAL 枚举本地打印机(包括Windows 95中的网络打印机)。名字会被忽略 PRINTER_ENUM_NAME 枚举由name参数指定的打印机。其中的名字可以是一个供应商、域或服务器。如name为NULL,则枚举出可用的打印机 PRINTER_ENUM_SHARE 枚举共享打印机(必须同其他常数组合使用) PRINTER_ENUM_CONNECTIONS 枚举网络连接列表中的打印机(即使目

18、前没有连接仅适用于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。如果

19、是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级快得多

20、。后者要求每台打印机都处于打开状态C#中Typeof是干什么的? 2011-10-21 06:39匿名| 分类:编程语言| 浏览5704次提问者采纳2011-10-21 07:25C# typeof() 和 GetType()区是什么?1、typeof(x)中的x,必须是具体的类名、类型名称等,不可以是变量名称。 2、GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof()相同,返回Type类型的当前对象的类型。 比如有这样一个变量i: Int32 i = new Int32(); i.GetType()返回值是Int32的类型,但是

21、无法使用typeof(i),因为i是一个变量,如果要使用typeof(),则只能:typeof(Int32),返回的同样是Int32的类型。C#中int和IntPtr相互转换方法一、int转IntPtrint i = 12;IntPtr p = new IntPtr(i);IntPtr转intint myi = (int)p;MessageBox.Show(myi.ToString();方法二、int转IntPtrint i = 12;IntPtr p =(IntPtr)iIntPtr转intint myi = (int)p;MessageBox.Show(myi.ToString();Int

22、Ptr问题public aaa(IntPtr myPtr,int left, int top, int width, short height) 这里myPtr应该是对应到一块内存,你需要查看aaa函数是如何把myPtr转化成它内部要使用的结构体的(一般都是结构体,也可能是其它对象,比如数组)。然后,你需要在你的托管代码中,定义该结构体,使用StructLayout特性,对结构体的字段使用MarshalAs特性,类似这样:StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Size = 13) public struct A1

23、01220Output MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11) public string TransactionAccountID; MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2) public string IsAuthenticated; 然后在需要使用的地方,获取该结构体对象的IntPtr,如下:/创建托管对象A101220Output output = new A101220Output ();output.TransactionAccountID = "110

24、00000841"output.IsAutienticated = "false"/分配非托管内存,并获取非托管内存地址起始位置指针int size = Marshal.SizeOf(output);IntPtr buffer = Marshal.AllocHGlobal(size);try /将托管对象拷贝到非托管内存 Marshal.StructureToPtr(output, buffer, false); /调用非托管方法 aaa.(buffer,0,0,640,480);finaly /释放非托管内存 Marshal.FreeHGlobal(buffer

25、);Free and Susan转C#中的IntPtr类型本文转自:问:c#中无法将类型“int”隐式转换为“System.IntPtr” 这个是我引用了一个api函数时出现的问题,我在声明中把intptr换成了int还是不可以,这是为什么呢?要如何处理呢?答:您好,C#中的IntPtr类型称为“平台特定的整数类型”,它们用于本机资源,如窗口句柄。资源的大小取决于使用的硬件和操作系统,但其大小总是足以包含系统的指针(因此也可以包含资源的名称)。 所以,在您调用的API函数中一定有类似窗体句柄这样的参数,那么当您声明这个函数时,您应该将它显式地声明为IntPtr类型。 例如,在一个C#程序中调用

26、Win32API mciSendString函数控制光盘驱动器,这个函数的函数原型是: MCIERROR mciSendString( LPCTSTR lpszCommand, LPTSTR lpszReturnString, UINT cchReturn, HANDLE hwndCallback ); 首先在C#中声明这个函数: DllImport("winmm.dll") private static extern long mciSendString(string a,string b,uint c,IntPtr d); 然后用这样的方法调用: mciSendStri

27、ng("set cdaudio door open", null, 0, this.Handle); 您也可以使用IntPtr.Zero将句柄设置为0; 或者使用类型强制转换: mciSendString("set cdaudio door open", null, 0, (IntPtr)0 ); 或者,使用IntPtr构造函数: IntPtr a = new IntPtr(2121); 这里有两点比较重要: 一是在C#中声明Win32API时,一定要按照WinAPI的原型来声明,不要改变它的数据类型; 二是尽量不要过多使用类型强制转换或构造函数的方式初

28、始化一个IntPtr类型的变量,这样会使程序变得难于理解并容易出错Marshal.SizeOf和sizeof的区别sizeof在非Unsafe环境下只能用于预定义的一系列类型,如Int,Short等等。而在Unsafe环境下,sizeof可以被用于值类型,但是值类型中不可以有引用类型,否则C#编译器会报错:error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('SizeOf.Program.MyStruct')而Marshal.Size

29、Of则是获得该类型被Marshal(转换,通常翻译为列集,指数据从一种类型转换到另外一种类型)到对应的非托管类型的大小。和sizeof不同,Marshal.SizeOf允许用在含有引用类型的值类型上: 1: StructLayout(LayoutKind.Sequential) 2: struct MyStruct 3: 4: string s; 5: Marshal.SizeOf(MyStruct)结果为4或者8,因为string被Marshal成char*。如果用在不含有引用类型的值类型上,其结果也有可能和sizeof完全不一样,如对于下面的值类型: 1: struct MyStruct

30、2: 3: char b; 4: sizeof(MyStruct)为2,而Marshal.SizeOf(typeof(MyStruct)结果则为1。这是因为在.NET中char总是Unicode,而缺省情况下char会被Marshal成8位的Ansi字符,因此结果不同。反之,如果我们指定这个char被Marshal成short值(也就是UTF16),如下: 1: StructLayout(LayoutKind.Sequential) 2: struct MyStruct 3: 4: MarshalAs(UnmanagedType.I2) 5: char b; 6: 那么sizeof和Marshal.SizeOf结果均为2。MarshalAs这个Attribute可以影响Marshal.SizeOf的结果,而不能影响sizeof的结果。一个有意思的情况是,如果值类型不含任何成员,如下: 1: struct MyStruct 2: 3: Sizeof和Marshal.SizeOf结果均为1,而不是0。这个结果和C+的结果是一致的。原因很简单:如果声明一个这样的数组,如

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论