C#调用WIN32API系列二列举局网内共享打印机 (转)

C#调用WIN32API系列二列举局网内共享打印机 (转)[@more@]首先我们看看EnumPrinters 函数的定义
BOOL EnumPrinters(
D word Flags, // printer object types
LPTSTR Name, // name of printer object
DWORD Level, // information level
LPBYTE pPrinterEnum, // printer information buffer
DWORD cbBuf, // size of printer information buffer
LPDWORD pcbNeeded, // bytes received or required
LPDWORD pcReturned // number of printers enumerated
);
这个 api有几个返回参数, 其中最重要的是pPrinterEnum所指的缓冲区中,是一个
PRINTER_INFO_N的结构数组, 这里N根据Level参数而变化, 这里我们用的是1, 所以用到的结构是
typedef struct _PRINTER_INFO_1 {
DWORD Flags;
LPTSTR pDescription;
LPTSTR pName;
LPTSTR pComment;
} PRINTER_INFO_1

C#调用API首先要引入动态库,EnumPrinters在winspool.drv这个动态库中。引入语句如下
[DllImport("winspool.drv", CharSet=CharSet.Auto)]
然后是定义PRINTER_INFO_1结构
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct PRINTER_INFO_1
{
int flags;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDescription;
[MarshalAs(UnmanagedType.LPTStr)]
public string pName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pComment;
}
好了,全部的 源代码如下:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Drawing.Printing;
public class QuickTest {
[DllImport("winspool.drv", CharSet=CharSet.Auto)]
static extern bool EnumPrinters(int flags, string name, int level, IntPtr pPrinterEnum,
int cbBuf, out int pcbNeeded, out int pcReturned);

private const int PRINTER_ENUM .NETWORK = 0x00000040;
private const int PRINTER_ENUM_LOCAL = 0x00000002;
private const int PRINTER_ENUM_REMOTE = 0x00000010;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct PRINTER_INFO_1
{
int flags;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDescription;
[MarshalAs(UnmanagedType.LPTStr)]
public string pName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pComment;
}

public void EnumeratePrintersWin()
{
bool Success;
int cbRequired;
int nEntries;

IntPtr outb = IntPtr.Zero;

Success = EnumPrinters(PRINTER_ENUM_NETWORK | PRINTER_ENUM_LOCAL | PRINTER_ENUM_REMOTE, null , 1, outb, 0, out cbRequired, out nEntries);
outb = Marshal.AllocHGlobal(cbRequired);
Success = EnumPrinters(PRINTER_ENUM_NETWORK | PRINTER_ENUM_LOCAL | PRINTER_ENUM_REMOTE, null , 1, outb, cbRequired, out cbRequired, out nEntries);

PRINTER_INFO_1[] portsArray = new PRINTER_INFO_1[cbRequired];


IntPtr current = outb;
try {
for (int i=0; i{
portsArray[i] = (PRINTER_INFO_1) Marshal.PtrToStructure(current,
typeof(PRINTER_INFO_1));
current=(IntPtr)((int)current+Marshal.SizeOf(typeof(PRINTER_INFO_1)));
Console.WriteLine(i+": "+portsArray[i].pName+" "+portsArray[i].pDescription+" "+portsArray[i].pComment+" ");
}
}
catch (Exception) {
//Console.WriteLine(exp.StackTrace);
}
Marshal.FreeHGlobal(outb);
}
public QuickTest () {

}
public static void Main() {
QuickTest qt = new QuickTest();
qt.EnumeratePrintersWin();
}
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-961533/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10748419/viewspace-961533/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值