C# 在enumjobs中获得JOB_INFO_1类型数组中的值

woshipysc 2007-05-23 11:57:32
我想要得到打印队列中的所有信息,请给个例子!!
...全文
423 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshipysc 2007-05-24
  • 打赏
  • 举报
回复
能不能在全点啊 ,我有个按钮,点击时,在listbox里把打印信息全显示到里边
CathySun118 2007-05-23
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/5445/5445873.xml?temp=.2538416
IT服务 2007-05-23
  • 打赏
  • 举报
回复
//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);
}
}

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧