熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

Delphi必學技巧:枚舉網打印機

2022-06-13   來源: 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 StringEmpty  printers  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
    }
    }
From:http://tw.wingwit.com/Article/program/Delphi/201311/8424.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.