其他语言

位置:IT落伍者 >> 其他语言 >> 浏览文章

Delphi必学技巧:枚举网打印机


发布日期:2019年09月18日
 
Delphi必学技巧:枚举网打印机
在某些情况下我们需要枚举网内的打印机试试以下的代码很有可能帮得了你

using System;

using SystemRuntimeInteropServices;

using SystemRuntimeSerialization;

namespace SampleGetPrinter

{

class App

{

static void Main(string[] args)

{

ConsoleWriteLine(请输入你想搜索类型的序号:);

ConsoleWriteLine( Default printer(only WinWinWinME)\n Enumerates the locally installed printers;\n Enumerates the list of printers to which the user has made previous connections;\n Enumerates the printer identified by Name;\n Enumerates network printers and print servers in the computers domain;\n Enumerates printers that have the shared attribute;\n Enumerates network printers in the computers domain;\n==========================);

int pt =;

try

{

pt=IntParse(ConsoleReadLine());

}

catch(Exception e)

{

ConsoleWriteLine(错误信息: {}eMessage);

return;

}

PRINTER_ENUM printerKind = GetPrinterConType(pt);

PrinterSearcher p = new PrinterSearcher();

PrinterSearcherPrinterInfo[] printers = pSearch(printerKind);

foreach(PrinterSearcherPrinterInfo pi in printers)

{

ConsoleWriteLine(=====================================\n打印机名: {}\n描叙:: {}\n注释: {}\n=====================================\n

piName piDescription piComment);

}

}

static PRINTER_ENUM GetPrinterConType(int ins)

{

switch(ins)

{

case :

return PRINTER_ENUMDEFAULT ;

case :

return PRINTER_ENUMLOCAL;

case :

return PRINTER_ENUMCONNECTIONS;

case :

return PRINTER_ENUMNAME;

case :

return PRINTER_ENUMREMOTE;

case :

return PRINTER_ENUMSHARED;

case :

return PRINTER_ENUMNETWORK;

default:

return PRINTER_ENUMLOCAL ;

}

}

}

#region 打印机位置状态枚举 PRINTER_ENUM

public enum PRINTER_ENUM

{

DEFAULT = x

LOCAL = x

CONNECTIONS = x

NAME = x

REMOTE = x

SHARED = x

NETWORK = x

}

#endregion

#region 异常派生 EnumPrinterException

[Serializable]

public class EnumPrinterException: ApplicationException

{

public EnumPrinterException() { }

public EnumPrinterException (string message): base(message) { }

public EnumPrinterException (string message Exception inner):

base(message inner) {}

protected EnumPrinterException (SerializationInfo info

StreamingContext context) : base(info context)

{ }

}

#endregion

//加上这个属性可以按导出到非托管对像的顺序排序

[StructLayout(LayoutKindSequential CharSet = CharSetAuto)]

public class PRINTER_INFO_

{

public int flags;

public IntPtr pDescription;

public IntPtr pName;

public IntPtr pComment;

}

public class PrinterSearcher

{

#region Search

public PrinterInfo[] Search(PRINTER_ENUM printerKind)

{

PrinterInfo[] pInfo = new PrinterInfo[];

uint iNeeded = iReturned = iSize = ;

IntPtr printers = IntPtrZero;

if (!EnumPrinters(printerKind StringEmptyprinters ref iNeeded ref iReturned) )

{

//返回由上一个非托管函数返回的错误代码该函数是使用设置了

//DllImport属性中SetLastError=true 标志的平台调用来调用的

int err = MarshalGetLastWinError();

if (err != WinErrorERROR_INSUFFICIENT_BUFFER)

ThrowEnumPrinterException();

}

iSize = iNeeded;

if (iNeeded != )

{

try

{

//使用AllocHGlobal分配非托管内块

printers = MarshalAllocHGlobal(new IntPtr(iSize));

//如果调用不成功抛出异常

if (!EnumPrinters(printerKind StringEmptyprinters iSize ref iNeeded ref iReturned) )

{

ThrowEnumPrinterException();

}

else

{

pInfo = GetPrinterInfoFromMemory(printers iReturned);

}

}

finally

{

//释放分配的内存块

if (printers != IntPtrZero)

MarshalFreeHGlobal(printers);

}

}

return pInfo;

}

#endregion

#region PrinterInfo

public struct PrinterInfo

{

public string Name;

public string Description;

public string Comment;

}

public sealed class WinError

{

private WinError() {}

public const int ERROR_INSUFFICIENT_BUFFER = ;

}

#endregion

#region EnumPrinters

[DllImport(winspooldrv SetLastError = true CharSet = CharSetAuto)]

[return: MarshalAs(UnmanagedTypeBool)]

private static extern bool EnumPrinters ([MarshalAs(UnmanagedTypeU)] PRINTER_ENUM flags

[MarshalAs(UnmanagedTypeLPStr)] string sName

uint iLevel

IntPtr pPrinterDesc

uint iSize

[MarshalAs(UnmanagedTypeU)] ref uint iNeeded

[MarshalAs(UnmanagedTypeU)] ref uint iReturned

);

#endregion

#region GetPrinterInfoFromMemory

private PrinterInfo[] GetPrinterInfoFromMemory(IntPtr prInfo uint numPrinters)

{

PRINTER_INFO_ pi = new PRINTER_INFO_();

PrinterInfo[] pInfo = new PrinterInfo[numPrinters];

for(int i = ; i < numPrinters; i++)

{

//把数据从非托管内存传送到到托管内存

MarshalPtrToStructure(prInfo pi);

pInfo[i]Name = MarshalPtrToStringAuto(pipName);

pInfo[i]Description = MarshalPtrToStringAuto(pipDescription);

pInfo[i]Comment = MarshalPtrToStringAuto(pipComment);

prInfo =new IntPtr(prInfoToInt() + MarshalSizeOf(typeof(PRINTER_INFO_)));

}

return pInfo;

}

private void ThrowEnumPrinterException()

{

throw new EnumPrinterException(stringFormat(LastErrorCode: {}

MarshalGetLastWinError()));

}

#endregion

}

}               

上一篇:Microsoft Visual C++Runtime Library

下一篇:从细节了解Delphi的命令行参数