betway88电子书籍教程中心网文荟萃客户留言繁體中文
设为首页
加入收藏
联系betway88
您当前的位置:betway88 -> 教程中心 -> 编程经验 -> JAVA DotNet -> 教程内容 退出登录 betway88,
栏目导航
· Visual Basic· Delphi
· Visual C++· C++ Builder
· JAVA DotNet· 其他相关
热门教程
· 如何在Visual Basic...
· 使用Visual Basic操...
· 使用VB6.0设计Activ...
· VB动态调用外部函数...
· 建立一个程序员自己...
· 如何编写高质量的VB...
· [图文] 为更新到Visual Bas...
· 再谈在VB中调用VC++...
· 用VB语言编程实现JP...
· 用DTS实现SQL数据库...
相关教程
· C#调用WIN32API系列...
· Visual Basic调用Wi...
· 大家一起分享C#调用...
· 用Visual C#调用Win...
· 如何利用Windows XP...
· [组图] Win XP中如何配置和...

C#调用WIN32API系列二列举局网内共享打印机
betway88,作者:佚名来源:本站整理发布时间:2005-12-29 17:58:52发布人:admin

减小字体 增大字体


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 pDes cription;
LPTSTR pName;
LPTSTR pComment;
} PRINTER_INFO_1

C#要调用API首先要引入动态库,EnumPrinters在winspool.drv这个动态库中。引入语句如下
[DllImport("http://92power.net/files/Article/17/110/2005/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 pDes cription;
[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("http://92power.net/files/Article/17/110/2005/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 pDes cription;
[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].pDes cription+"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();
}
}
联系作者zlyperson@163.net


[] [返回上一页] [打 印] [收 藏]
下一篇教程:betway88,C#学习文档
∷相关教程评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 网站地图 - 管理登录
Copyright 2020 betway88. All Rights Reserved .
浙ICP备05047688号