Probleme avec Api Setjob

Probleme avec Api Setjob - Divers - Programmation

Marsh Posté le 30-01-2007 à 12:42:07    

Bonjour
 
Je veux changer le nom d’un job d’impression qui est attente dans le spool Windows.
J’utilise la fonction API « SetJob » et la structure « JOB_INFO_2 » qui contient le champ « pDocument » qui doit permettre le changement de nom. Mais ça ne marche pas
Je ne trouve pas mon erreur, ni d’information sur la méthode
Voilà mon code en VB.net
 
Merci pour votre aide.
 
 
Option Strict Off
Option Explicit On
 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
 
 
Module Module1
 
    <DllImport("winspool.drv", EntryPoint:="SetJobA", _
        SetLastError:=True, CharSet:=CharSet.Ansi, _
        ExactSpelling:=True, _
        CallingConvention:=CallingConvention.StdCall)> _
        Public Function SetJob _
        (ByVal hPrinter As Int32, _
        ByVal JobId As Int32, _
        ByVal Level As Int32, _
        ByRef pJob As JOB_INFO_2, _
        ByVal Command_Renamed As Int32) As Boolean
    End Function                   'Le "A" pour Ansi et le "W" pour Unicode
 
    <DllImport("winspool.drv", EntryPoint:="OpenPrinterW", _
        SetLastError:=True, CharSet:=CharSet.Auto, _
        ExactSpelling:=True, _
        CallingConvention:=CallingConvention.StdCall)> _
        Public Function OpenPrinter _
        (ByVal pPrinterName As IntPtr, _
        ByRef phPrinter As IntPtr, _
        ByVal pDefault As IntPtr) As Boolean
    End Function
 
    <DllImport("winspool.drv", EntryPoint:="ClosePrinter", _
        SetLastError:=True, CharSet:=CharSet.Auto, _
        ExactSpelling:=True, _
        CallingConvention:=CallingConvention.StdCall)> _
        Public Function ClosePrinter _
        (ByVal hPrinter As IntPtr) As Boolean
    End Function
 
    Const CCHDEVICENAME As Integer = 32
    Const CCHFORMNAME As Integer = 32
 
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure SYSTEMTIME
        Public wYear As Short
        Public wMonth As Short
        Public wDayOfWeek As Short
        Public wDay As Short
        Public wHour As Short
        Public wMinute As Short
        Public wSecond As Short
        Public wMilliseconds As Short
    End Structure
 
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure DEVMODE
        <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCHDEVICENAME)> _
        Public dmDeviceName As String
        Public dmSpecVersion As Short
        Public dmDriverVersion As Short
        Public dmSize As Short
        Public dmDriverExtra As Short
        Public dmFields As Integer
        Public dmOrientation As Short
        Public dmPaperSize As Short
        Public dmPaperLength As Short
        Public dmPaperWidth As Short
        Public dmScale As Short
        Public dmCopies As Short
        Public dmDefaultSource As Short
        Public dmPrintQuality As Short
        Public dmColor As Short
        Public dmDuplex As Short
        Public dmYResolution As Short
        Public dmTTOption As Short
        Public dmCollate As Short
        <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCHFORMNAME)> _
        Public dmFormName As String
        Public dmUnusedPadding As Short
        Public dmBitsPerPel As Integer
        Public dmPelsWidth As Integer
        Public dmPelsHeight As Integer
        Public dmDisplayFlags As Integer
        Public dmDisplayFrequency As Integer
    End Structure
 
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure JOB_INFO_2
        Public JobId As Integer
        Public pPrinterName As String
        Public pMachineName As String
        Public pUserName As String
        Public pDocument As Integer
        Public pNotifyName As String
        Public pDatatype As String
        Public pPrintProcessor As String
        Public pParameters As String
        Public pDriverName As String
        Public pDevMode As Integer       '    DEVMODE
        Public pStatus As String
        Public pSecurityDescriptor As Integer      '   SECURITY_DESCRIPTOR
        Public Status As Integer
        Public Priority As Integer
        Public Position As Integer
        Public StartTime As Integer
        Public UntilTime As Integer
        Public TotalPages As Integer
        Public Size As Integer
        Public Submitted As SYSTEMTIME
        Public time As Integer
        Public PagesPrinted As Integer
    End Structure
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure ACL
        Public AclRevision As Byte
        Public Sbz1 As Byte
        Public AclSize As Short
        Public AceCount As Short
        Public Sbz2 As Short
    End Structure
 
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure SECURITY_DESCRIPTOR
        Public Revision As Byte
        Public Sbz1 As Byte
        Public Control As Integer
        Public Owner As Integer
        Public Group As Integer
        Public Sacl As ACL
        Public Dacl As ACL
    End Structure
 
    Public Const JOB_CONTROL_PAUSE As Integer = 1
    Public Const JOB_CONTROL_RESUME As Integer = 2
    Public Const JOB_CONTROL_CANCEL As Integer = 3
    Public Const JOB_CONTROL_RESTART As Integer = 4
    Public Const JOB_CONTROL_DELETE As Integer = 5
 
    Public Sub Impression()
        'Executé par le bouton
 
        Dim hPrinter As Integer 'Descripteur d'imprimante
        Dim res As Boolean   'Résultat des appels à l'API
        Dim jobinfo As New JOB_INFO_2
 
        'Ouvre un objet Printer pour l'imprimante spécifiée et récupère son handle
        res = OpenPrinter(Marshal.StringToHGlobalUni("LaserJet I" ), hPrinter, 0)
        If res = 0 Then
            MsgBox("Impossible d'ouvrir l'imprimante" )
            Exit Sub
        End If
 
        'Renseigne la structure JOB_INFO_2 (Uniquement les champs utiles)
        jobinfo.JobId = 2  'Numéro du job dans le spool
        jobinfo.pDocument = Marshal.StringToHGlobalAuto("Nouveau Nom du Job dans le spool" )
        'jobinfo.pDocument = ("Nouveau Nom du Job dans le spool" )
 
 
        'Affecte au job son nom
        res = SetJob(hPrinter, jobinfo.JobId, 2, jobinfo, 2)
 
        If res = 0 Then 'Affiche un message d'erreur
            MsgBox("SetJob a échoué" )
        End If
 
        'Ferme l'objet Printer ouvert
        res = ClosePrinter(hPrinter)
 
        If res = 0 Then 'Affiche un message d'erreur
            MsgBox("ClosePrinter a échoué" )
            Exit Sub
        End If
 
    End Sub
 
End Module

Reply

Marsh Posté le 30-01-2007 à 12:42:07   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed