essPrinterJobs"

[IntPtr]$hPrinter = [IntPtr]::Zero

#
# Opens the printer and gets the its handle
#
[int]$result = $winSpoolType[0]::OpenPrinter($printerName, [ref]$hPrinter, [IntPtr]::Zero)
[int]$errorCode = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
if($result -eq 0)
{
    WriteFileAPIExceptionReport $fileName "OpenPrinter" $errorCode
    return
}

try
{
    [IntPtr]$pJob =  [IntPtr]::Zero
    [int]$pcbneeded = 0
    [int]$pcReturned = 0

    #
    # Calls the EnumJobs and get necessary buffer from the parameter of pcbneeded
    #
    $result = $winSpoolType[0]::EnumJobs($hPrinter, 0, $MAX_PRINT_JOB, $JOB_LEVEL, $pJob, 0, [ref]$pcbneeded, [ref]$pcReturned)

    if($pcbneeded -gt 0)
    {
        $pJob = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($pcbneeded)
        try
        {
            #
            # Calls the EnumJobs to get the JOB_INFO_2 array into $pJob
            #
            $result = $winSpoolType[0]::EnumJobs($hPrinter, 0, $MAX_PRINT_JOB, $JOB_LEVEL, $pJob, $pcbneeded, [ref]$pcbneeded, [ref]$pcReturned)
            $errorCode = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
            if($result -eq 0)
            {
                WriteFileAPIExceptionReport $fileName "EnumJobs" $errorCode
                return
            }

            #
            # To every job, call the API of SetJob to delete it.
            #
            [int]$ptr = $pJob.ToInt32()
            for([int]$i = 0; $i -lt $pcReturned; $i++)
            {

                $pinfo = [System.Runtime.InteropServices.Marshal]::PtrToStructure($ptr, $JOB_INFO_2.GetType())

                if($pinfo.Status -band $JOB_STATUS_PAUSED)
                {
                    $result = $winSpoolType[0]::SetJob($hPrinter, $pinfo.JobId, $JOB_LEVEL, [ref]$pinfo, $JOB_CONTROL_RESUME)
                    $errorCode = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
                    if($result -eq 0)
                    {
                        WriteFileAPIExceptionReport $fileName "SetJob" $errorCode
                    }
                }
                elseif($pinfo.Status -band $JOB_STATUS_ERROR -or $pinfo.Status -band $JOB_STATUS_DELETING -or $pinfo.Status -band $JOB_STATUS_BLOCKED_DEVQ -or $pinfo.Status -band $JOB_STATUS_USER_INTERVENTION)
                {
                    $result = $winSpoolType[0]::SetJob($hPrinter, $pinfo.JobId, $JOB_LEVEL, [ref]$pinfo, $JOB_CONTROL_DELETE)
                    $errorCode = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
                    if($result -eq 0)
                    {
                        WriteFileAPIExceptionReport $fileName "SetJob" $errorCode
                    }
                }
                $ptr += [System.Runtime.InteropServices.Marshal]::SizeOf($JOB_INFO_2.GetType())
            }
        }
        finally
        {
            [System.Runtime.InteropServices.Marshal]::FreeHGlobal($pJob)
        }
    }
}
finally
{
    $# Copyright  2008, Microsoft Corporation. All rights reserved.


PARAM($printerName)
#
# Check whether the printer driver needs be updated
#
Import-LocalizedData -BindingVariable localizationString -FileName CL_LocalizationData
. .\CL_Utility.ps1

Write-DiagProgress -activity $localizationString.progress_ts_printerDriver

[int]$PRINTER_STATUS_DRIVER_UPDATE_NEEDED = 0x04000000

[int]$printStatus = GetPrinterStatus $printerName

if($printStatus -band $PRINTER_STATUS_DRIVER_UPDATE_NEEDED)
{
    Update-DiagRootCause -id "RC_PrinterDriver" -parameter @{ "PRINTERNAME" = $printerName } -Detected $true
} else {
    Update-DiagRootCause -id "RC_PrinterDriver" -parameter @{ "PRINTERNAME" = $printerName } -Detected $false
}
