Here is my main program:
class Program
{
    static void Main(string[] args)
    {
        Printer printer = new Printer();
        IntPtr printerHandle = printer.getPrinterHandle("TASKalfa 2551ci");
        UInt32 jobId = printer.getJobId();
        Console.WriteLine(printerHandle+","+jobId);
        printer.getJob(printerHandle, jobId);
        Console.ReadLine();
    }
}
I got the following error message from VS 2015 Community Edition, when I call printer.getJob Method, even I have catched the System.NullReferenceException.
System.NullReferenceException was unhandled
Message: An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll
Additional information: Object reference not set to an instance of an object.
I have checked that both variable printerHandle and jobId are not null, so I cannot figure out what is problem.
However the value of printerHandle is not a constant, is it correct?
Here is my printer object source code:
using System;
using System.Collections;
using System.Management;
using System.Runtime.InteropServices;
class Printer
{
    public Printer()
    {
    }
    public ArrayList getPrinterNameList()
    {
        ArrayList result = new ArrayList();
        var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer");
        foreach (var printer in printerQuery.Get())
        {
            result.Add(printer.GetPropertyValue("Name"));
        }
        return result;
    }
    public UInt32 getJobId()
    {
        UInt32 jobId=0;
        var printJobQuery = new ManagementObjectSearcher("select * from Win32_PrintJob");
        foreach (var printJob in printJobQuery.Get())
        {
            jobId= (UInt32)printJob.Properties["JobId"].Value;
        }
        return jobId;
    }
    public IntPtr getPrinterHandle(String printerName)
    {
        IntPtr result=new IntPtr(0);
        Console.WriteLine ("OpenPrinter="+OpenPrinter(printerName,out result, result));
        return result;
    }
    public void getJob(IntPtr printerHandle,UInt32 jobId)
    {
        int BUFFER_SIZE = 250;
        IntPtr pcbNeeed = new IntPtr(0);
        byte[] byteBuffer = new byte[BUFFER_SIZE];
        try
        { 
            Console.WriteLine("GetJob="+GetJob(printerHandle, (Int32)jobId, 1, out byteBuffer, BUFFER_SIZE, out pcbNeeed));
        }
        catch (System.NullReferenceException err)
        {
            Console.WriteLine(err.Message);
        }
    }
    [DllImport("winspool.drv", SetLastError = true)]
    static extern int OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);
     [DllImport(
        "winspool.drv",
        EntryPoint = "GetJobW",
        SetLastError = true,
        CharSet = CharSet.Ansi,
        ExactSpelling = false,
        CallingConvention = CallingConvention.StdCall)]
            private static extern bool GetJob
        ([InAttribute()] IntPtr hPrinter,
        [InAttribute()] Int32 JobId,
        [InAttribute()] Int32 Level,
        [OutAttribute()] out byte[] pJob,
        [InAttribute()] Int32 cbBuf,
        [OutAttribute()] out IntPtr pcbNeeded);
}
Here is my WIN API version:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string msg = null;
                var hPrinter = new IntPtr();
                bool open = NativeMethods.OpenPrinterW("TASKalfa 2551ci", ref hPrinter, IntPtr.Zero);
                Debug.Assert(open);
                /* Query for 99 jobs */
                const uint firstJob = 0u;
                const uint noJobs = 99u;
                const uint level = 1u;
                // Get byte size required for the function
                uint needed;
                uint returned;
                IntPtr tempptr = IntPtr.Zero;
                bool b1 = NativeMethods.EnumJobsW(
                    hPrinter, firstJob, noJobs, level, IntPtr.Zero, 0, out needed, out returned);
                uint lastError = NativeMethods.GetLastError();
                Console.WriteLine("b1="+b1);
                //Debug.Assert(lastError == NativeConstants.ERROR_INSUFFICIENT_BUFFER);
                NativeMethods.FormatMessage(0x1300, ref tempptr, lastError, 0, ref msg, 255, ref tempptr);
                Console.WriteLine("lastError=" + msg);
                // Populate the structs
                IntPtr pJob = Marshal.AllocHGlobal((int)needed);
                uint bytesCopied;
                uint structsCopied;
                bool b2 = NativeMethods.EnumJobsW(
                    hPrinter, firstJob, noJobs, level, pJob, needed, out bytesCopied, out structsCopied);
                lastError = NativeMethods.GetLastError();
                Console.WriteLine("b2="+b2);
                NativeMethods.FormatMessage(0x1300, ref tempptr, lastError, 0, ref msg, 255, ref tempptr);
                Console.WriteLine("lastError="+ msg);
                var jobInfos = new JOB_INFO_1W[structsCopied];
                int sizeOf = Marshal.SizeOf(typeof(JOB_INFO_1W));
                IntPtr pStruct = pJob;
                for (int i = 0; i < structsCopied; i++)
                {
                    var jobInfo_1W = (JOB_INFO_1W)Marshal.PtrToStructure(pStruct, typeof(JOB_INFO_1W));
                    jobInfos[i] = jobInfo_1W;
                    pStruct += sizeOf;
                }
                Marshal.FreeHGlobal(pJob);
                Console.WriteLine("structsCopied="+structsCopied);
                Console.ReadLine();
            }
            public class NativeConstants
            {
                public const int ERROR_INSUFFICIENT_BUFFER = 122;
            }
            public partial class NativeMethods
            {
                [DllImport("kernel32.dll", EntryPoint = "GetLastError")]
                public static extern uint GetLastError();
                [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
                public extern static int FormatMessage(int flag, ref IntPtr source, uint msgid, int langid, ref string buf, int size, ref IntPtr args);
            }
            public partial class NativeMethods
            {
                [DllImport("Winspool.drv", EntryPoint = "OpenPrinterW")]
                [return: MarshalAs(UnmanagedType.Bool)]
                public static extern bool OpenPrinterW([In] [MarshalAs(UnmanagedType.LPWStr)] string pPrinterName,
                    ref IntPtr phPrinter, [In] IntPtr pDefault);
                [DllImport("Winspool.drv", EntryPoint = "EnumJobsW")]
                [return: MarshalAs(UnmanagedType.Bool)]
                public static extern bool EnumJobsW([In] IntPtr hPrinter, uint FirstJob, uint NoJobs, uint Level, IntPtr pJob,
                    uint cbBuf, [Out] out uint pcbNeeded, [Out] out uint pcReturned);
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct JOB_INFO_1W
            {
                public uint JobId;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string pPrinterName;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string pMachineName;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string pUserName;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string pDocument;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string pDatatype;
                [MarshalAs(UnmanagedType.LPWStr)]
                public string pStatus;
                public uint Status;
                public uint Priority;
                public uint Position;
                public uint TotalPages;
                public uint PagesPrinted;
                public SYSTEMTIME Submitted;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct SYSTEMTIME
            {
                public ushort wYear;
                public ushort wMonth;
                public ushort wDayOfWeek;
                public ushort wDay;
                public ushort wHour;
                public ushort wMinute;
                public ushort wSecond;
                public ushort wMilliseconds;
            }
        }
    }
I am using Windows 10.

