posts - 43,  comments - 9,  trackbacks - 1

public struct SYSTEMTIME
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct JOB_INFO_1
    public int JobId;
    public string pPrinterName;
    public string pMachineName;
    public string pUserName;
    public string pDocument;
    public string pDatatype;
    public string pStatus;
    public int Status;
    public int Priority;
    public int Position;
    public int TotalPages;
    public int PagesPrinted;
    public SYSTEMTIME Submitted;

[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);

[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern bool ClosePrinter(IntPtr hPrinter);

[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern int EnumJobs(IntPtr hPrinter, int FirstJob, int NoJobs, int Level, IntPtr pInfo, int cdBuf,
                                  out int pcbNeeded, out int pcReturned);

private static void peekPrinterJobs(string printerToPeek)
    IntPtr handle;
    int FirstJob = 0;
    int NumJobs = 127;
    int pcbNeeded;
    int pcReturned;

    // open printer
    OpenPrinter(printerToPeek, out handle, IntPtr.Zero);

    // get num bytes required, here we assume the maxt job for the printer quest is 128 (0..127)
    EnumJobs(handle, FirstJob, NumJobs, 1, IntPtr.Zero, 0, out pcbNeeded, out pcReturned);

    // allocate unmanaged memory
    IntPtr pData = Marshal.AllocHGlobal(pcbNeeded);

    // get structs
    EnumJobs(handle, FirstJob, NumJobs, 1, pData, pcbNeeded, out pcbNeeded, out pcReturned);

    // create array of managed job structs
    JOB_INFO_1[] jobs = new JOB_INFO_1[pcReturned];

    // marshal struct to managed
    int pTemp = pData.ToInt32(); //start pointer
    for (int i = 0; i < pcReturned; ++i)
        jobs[i] = (JOB_INFO_1) Marshal.PtrToStructure(new IntPtr(pTemp), typeof (JOB_INFO_1));
        pTemp += Marshal.SizeOf(typeof (JOB_INFO_1));

    // cleanup unmanaged memory

    // close printer

    // printer jobs are in the jobs array now, do what you want to...



posted on 2009-05-07 15:06 DongD 阅读(...) 评论(...) 编辑 收藏