首页 > 开发 > 综合 > 正文

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

2020-02-03 13:45:51
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,

c#调用win32api系列二列举局网内共享打印机

c#通过调用win32api可以实现非常强大的功能,本文将着重讲述如何通过调用win32api实现列举局域网络内所有共享的打印机。

首先我们看看enumprinters函数的定义

bool enumprinters(

dword 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+": /n"+portsarray[i].pname+"/n"+portsarray[i].pdescription+"/n"+portsarray[i].pcomment+"/n");

}

}

catch (exception) {

//console.writeline(exp.stacktrace);

}

marshal.freehglobal(outb);

}

public quicktest () {

}

public static void main() {

quicktest qt = new quicktest();

qt.enumerateprinterswin();

}

}

联系作者[email protected]

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表