首页 >> 民生呼声

调用系列二列举局网内共的

民生呼声  2021-08-24 13:32 字号: 大 中 小

首先我们看看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在v这个动态库中。引入语句如下

[DllImport(\"v\", CharSet=to)]

然后是定义PRINTER_INFO_1结构

[StructLayout(quential, CharSet=to)]

struct PRINTER_INFO_1

{

int flags;

[MarshalAs(TStr)]

public string pDescription;

[MarshalAs(TStr)]

public string pName;

[MarshalAs(TStr)]

public string pComment;

}

好了,全部的源代码如下:

using System;

using llections;

using eropServices;

using agnostics;

using inting;

public class QuickTest {

[DllImport(\"v\", CharSet=to)]

着名黑人演员摩根·弗里曼将在第69届金球奖颁奖典礼上被授予塞西尔·B·德米尔终身成就奖 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 = 0x;

private const int PRINTER_ENUM_LOCAL = 0x;

private const int PRINTER_ENUM_REMOTE = 0x;

[StructLayout(quential, CharSet=to)]

struct PRINTER_INFO_1

{

int flags;

[MarshalAs(TStr)]

public string pDescription;

[MarshalAs(TStr)]

public string pName;

[MarshalAs(TStr)]

public string pComment;

}

public void EnumeratePrintersWin()

{

bool Success;

int cbRequired;

int nEntries;

IntPtr outb = ro;

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

outb = locHGlobal(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; ngth; i++)

{

portsArray[i] = (PRINTER_INFO_1) rToStructure(current,

typeof(PRINTER_INFO_1));

current=(IntPtr)((int)current+zeOf(typeof(PRINTER_INFO_1)));

iteLine(i+\": \\n\"+portsArray[i].pName+\"\\n\"+portsArray[i].pDescription+\"\\n\"+portsArray[i].pComment+\"\\n\");

}

}

catch (Exception) {

//iteLine(ackTrace);

}

eeHGlobal(outb);

}

public QuickTest () {

}

public static void Main() {

QuickTest qt = new QuickTest();

umeratePrintersWin();

}

}

济南哪家医院看白癜风
南昌去哪里看男科
九江白癜风医院哪家好
推荐资讯