//using System.Runtime.InteropServices;
//using System.ComponentModel;[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool OpenPrinter(string pPrinterName,
out IntPtr hPrinter, IntPtr pDefault);[DllImport("winspool.drv", SetLastError=true)]
private static extern bool ClosePrinter(IntPtr hPrinter);[DllImport("winspool.drv", SetLastError=true)]
private static extern bool GetPrinter(IntPtr hPrinter,
int dwLevel, IntPtr pPrinter, int cbBuf, out int pcbNeeded);[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct PRINTER_INFO_2
{
public string pServerName;
public string pPrinterName;
public string pShareName;
public string pPortName;
public string pDriverName;
public string pComment;
public string pLocation;
public IntPtr pDevMode;
public string pSepFile;
public string pPrintProcessor;
public string pDatatype;
public string pParameters;
public IntPtr pSecurityDescriptor;
public uint Attributes;
public uint Priority;
public uint DefaultPriority;
public uint StartTime;
public uint UntilTime;
public uint Status;
public uint cJobs;
public uint AveragePPM;
}/// <summary>
/// プリンタの情報をPRINTER_INFO_2で取得する
/// </summary>
/// <param name="printerName">プリンタ名</param>
/// <returns>プリンタの情報</returns>
public static PRINTER_INFO_2 GetPrinterInfo(string printerName)
{
//プリンタのハンドルを取得する
IntPtr hPrinter;
if (!OpenPrinter(printerName, out hPrinter, IntPtr.Zero))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
} IntPtr pPrinterInfo = IntPtr.Zero;
try
{
//必要なバイト数を取得する
int needed;
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out needed);
if (needed <= 0)
throw new Exception("失敗しました。"); //メモリを割り当てる
pPrinterInfo = Marshal.AllocHGlobal(needed); //プリンタ情報を取得する
int temp;
if (!GetPrinter(hPrinter, 2, pPrinterInfo, needed, out temp))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
} //PRINTER_INFO_2型にマーシャリングする
PRINTER_INFO_2 printerInfo =
(PRINTER_INFO_2) Marshal.PtrToStructure(pPrinterInfo,
typeof(PRINTER_INFO_2)); //結果を返す
return printerInfo;
}
finally
{
//後始末をする
ClosePrinter(hPrinter);
Marshal.FreeHGlobal(pPrinterInfo);
}
}
//using System.ComponentModel;[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool OpenPrinter(string pPrinterName,
out IntPtr hPrinter, IntPtr pDefault);[DllImport("winspool.drv", SetLastError=true)]
private static extern bool ClosePrinter(IntPtr hPrinter);[DllImport("winspool.drv", SetLastError=true)]
private static extern bool GetPrinter(IntPtr hPrinter,
int dwLevel, IntPtr pPrinter, int cbBuf, out int pcbNeeded);[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct PRINTER_INFO_2
{
public string pServerName;
public string pPrinterName;
public string pShareName;
public string pPortName;
public string pDriverName;
public string pComment;
public string pLocation;
public IntPtr pDevMode;
public string pSepFile;
public string pPrintProcessor;
public string pDatatype;
public string pParameters;
public IntPtr pSecurityDescriptor;
public uint Attributes;
public uint Priority;
public uint DefaultPriority;
public uint StartTime;
public uint UntilTime;
public uint Status;
public uint cJobs;
public uint AveragePPM;
}/// <summary>
/// プリンタの情報をPRINTER_INFO_2で取得する
/// </summary>
/// <param name="printerName">プリンタ名</param>
/// <returns>プリンタの情報</returns>
public static PRINTER_INFO_2 GetPrinterInfo(string printerName)
{
//プリンタのハンドルを取得する
IntPtr hPrinter;
if (!OpenPrinter(printerName, out hPrinter, IntPtr.Zero))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
} IntPtr pPrinterInfo = IntPtr.Zero;
try
{
//必要なバイト数を取得する
int needed;
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out needed);
if (needed <= 0)
throw new Exception("失敗しました。"); //メモリを割り当てる
pPrinterInfo = Marshal.AllocHGlobal(needed); //プリンタ情報を取得する
int temp;
if (!GetPrinter(hPrinter, 2, pPrinterInfo, needed, out temp))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
} //PRINTER_INFO_2型にマーシャリングする
PRINTER_INFO_2 printerInfo =
(PRINTER_INFO_2) Marshal.PtrToStructure(pPrinterInfo,
typeof(PRINTER_INFO_2)); //結果を返す
return printerInfo;
}
finally
{
//後始末をする
ClosePrinter(hPrinter);
Marshal.FreeHGlobal(pPrinterInfo);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货