GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
Go Back  Xtreme Visual Basic Talk > > > GetPrinter, SetPrinter. I HATE IT! -.-


Reply
 
Thread Tools Display Modes
  #1  
Old 10-01-2010, 05:40 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default GetPrinter, SetPrinter. I HATE IT! -.-


Code:
Imports System.Runtime.InteropServices
Public Class Form1

    'declare the API in VB
    Private Declare Function AddPrinter Lib "winspool.drv" Alias "AddPrinterW" (ByVal pName As IntPtr, ByVal Level As Int32, <MarshalAs(UnmanagedType.LPStruct)> ByVal pPrinter As PRINTER_INFO_2) As Int32
    'Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterW" (ByVal hPrinter As IntPtr, ByVal Level As Integer, ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, ByRef pcbNeeded As Integer) As Integer
    'Declare Auto Function GetPrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr, ByVal Level As Integer, ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, ByRef pcbNeeded As Integer) As Boolean
    Public Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterW" (ByVal hPrinter As IntPtr, ByVal Level As Integer, ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, ByRef pcbNeeded As Integer) As Integer
    Declare Auto Function OpenPrinter Lib "winspool.drv" (ByVal pPrinterName As String, ByRef phPrinter As IntPtr, ByVal pDefault As IntPtr) As Boolean
    Declare Auto Function OpenPrinter Lib "winspool.drv" (ByVal pPrinterName As String, ByRef phPrinter As IntPtr, ByRef pDefault As PRINTER_DEFAULTS) As Boolean
    Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr) As Boolean

    '<DllImport("winspool.Drv", EntryPoint:="SetPrinterA", SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    'Public Function SetPrinter(ByVal hPrinter As IntPtr, ByVal Level As Integer, ByVal pPrinter As IntPtr, ByVal Command As Integer) As Boolean
    'End Function

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> Friend Class PRINTER_DEFAULTSs
        Public lpDataType As Int32
        Public lpDevMode As Int32
        <MarshalAs(UnmanagedType.U4)> Public DesiredAccess As PrinterAccessRights
    End Class

    Structure PRINTER_DEFAULTS
        Public pDatatype As IntPtr
        Public pDevMode As IntPtr
        Public DesiredAccess As Integer
    End Structure

    <FlagsAttribute()> Public Enum PrinterAccessRights As Integer
        ' READ_CONTROL - Allowed to read printer information        
        READ_CONTROL = &H20000
        ' WRITE_DAC - Allowed to write device access control info        
        WRITE_DAC = &H40000
        ' WRITE_OWNER - Allowed to change the object owner        
        WRITE_OWNER = &H80000
        ' SERVER_ACCESS_ADMINISTER         
        SERVER_ACCESS_ADMINISTER = &H1
        '  SERVER_ACCESS_ENUMERATE        
        SERVER_ACCESS_ENUMERATE = &H2
        ' PRINTER_ACCESS_ADMINISTER Allows administration of a printer        
        PRINTER_ACCESS_ADMINISTER = &H4
        ' PRINTER_ACCESS_USE Allows printer general use (printing, querying)        
        PRINTER_ACCESS_USE = &H8
        ' PRINTER_ALL_ACCESS Allows use and administration.        
        PRINTER_ALL_ACCESS = &HF000C
        ' SERVER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)        
        SERVER_ALL_ACCESS = &HF0003
    End Enum


    ' ##ENUMERATION_DESCRIPTION
    Public Enum Printer_Attributes
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_QUEUED
        PRINTER_ATTRIBUTE_QUEUED = &H1
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_DIRECT
        PRINTER_ATTRIBUTE_DIRECT = &H2
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_DEFAULT
        PRINTER_ATTRIBUTE_DEFAULT = &H4
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_SHARED
        PRINTER_ATTRIBUTE_SHARED = &H8
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_NETWORK
        PRINTER_ATTRIBUTE_NETWORK = &H10
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_HIDDEN
        PRINTER_ATTRIBUTE_HIDDEN = &H20
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_LOCAL
        PRINTER_ATTRIBUTE_LOCAL = &H40
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_ENABLE_DEVQ
        PRINTER_ATTRIBUTE_ENABLE_DEVQ = &H80
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS
        PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS = &H100
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST
        PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST = &H200
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_WORK_OFFLINE
        PRINTER_ATTRIBUTE_WORK_OFFLINE = &H400
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_ENABLE_BIDI The printer can operate in bidirectional mode
        PRINTER_ATTRIBUTE_ENABLE_BIDI = &H800
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_RAW_ONLY The printer can only accept raw data
        PRINTER_ATTRIBUTE_RAW_ONLY = &H1000
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_PUBLISHED The printer is shared as a network resource
        PRINTER_ATTRIBUTE_PUBLISHED = &H2000
    End Enum
    <Flags()> _
    Enum DM As Integer
        Orientation = &H1
        PaperSize = &H2
        PaperLength = &H4
        PaperWidth = &H8
        Scale = &H10
        Position = &H20
        NUP = &H40
        DisplayOrientation = &H80
        Copies = &H100
        DefaultSource = &H200
        PrintQuality = &H400
        Color = &H800
        Duplex = &H1000
        YResolution = &H2000
        TTOption = &H4000
        Collate = &H8000
        FormName = &H10000
        LogPixels = &H20000
        BitsPerPixel = &H40000
        PelsWidth = &H80000
        PelsHeight = &H100000
        DisplayFlags = &H200000
        DisplayFrequency = &H400000
        ICMMethod = &H800000
        ICMIntent = &H1000000
        MediaType = &H2000000
        DitherType = &H4000000
        PanningWidth = &H8000000
        PanningHeight = &H10000000
        DisplayFixedOutput = &H20000000
    End Enum
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #2  
Old 10-01-2010, 05:41 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

Code:
<StructLayout(LayoutKind.Sequential)> _
    Public Class PRINTER_INFO_2
        <MarshalAs(UnmanagedType.LPTStr)> Public pServerName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pPrinterName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pShareName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pPortName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pDriverName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pComment As String
        Public pDevMode As IntPtr
        <MarshalAs(UnmanagedType.LPTStr)> Public pLocation As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pSepFile As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pPrintProcessor As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pDatatype As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pParameters As String
        Public pSecurityDescriptor As IntPtr
        Public Attributes As Integer
        Public Priority As Integer
        Public DefaultPriority As Integer
        Public StartTime As Integer
        Public UntilTime As Integer
    End Class

    Function AddMyPrinter() As Boolean

        Dim PI2 As New PRINTER_INFO_2
        'only printer name, driver name, port name and print processor need to be filled
        With PI2
            .pServerName = "\\dt-win2003svr01"
            .pPrinterName = "Xerox WorkCentre 7132 PCL 6"
            .pShareName = "LP62321"
            .pPortName = "LP62321"
            .pDriverName = "Xerox WorkCentre 7132 PCL 6"
            .pComment = "SAME"
            .pLocation = "SAMEER"
            .pDevMode = 0
            .pSepFile = String.Empty
            .pPrintProcessor = "WinPrint"
            .pDatatype = String.Empty
            .pParameters = String.Empty
            .pSecurityDescriptor = 0
            .Attributes = Printer_Attributes.PRINTER_ATTRIBUTE_SHARED Or Printer_Attributes.PRINTER_ATTRIBUTE_NETWORK '8216 ' Shared + Dictory + Network
            .Priority = 0
            .DefaultPriority = 0
            .StartTime = 0
            .UntilTime = 0
        End With

        Dim str As String = "\\dt-win2003svr01"
        Dim ptr As IntPtr = Marshal.StringToHGlobalAuto(str)
        Dim mystring As String = Marshal.PtrToStringAuto(ptr)

        Dim x As Integer = AddPrinter(ptr, 2, PI2)
        If x = 0 Then
            MsgBox("Failed !!!")
            MsgBox(Err.LastDllError)
            End
        End If
        Return True
    End Function

    Public Sub GetMyPrinter()
        Dim PI2 As New PRINTER_INFO_2
        Dim pBuf As IntPtr
        Dim hPrinter As Integer
        Dim cbNeeded As Integer
        Dim rc As Integer
        Dim pd = New PRINTER_DEFAULTS()

        rc = OpenPrinter("\\dt-win2003svr01\Xerox WorkCentre 7132 PCL 6", hPrinter, 0)
        If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())

        'The first call will fail, but it will return the length of the buffer we need to allocate
        rc = GetPrinter(hPrinter, 2, Nothing, 0, cbNeeded)

        'Allocate a memory buffer and fill the buffer with the printer info
        pBuf = Marshal.AllocHGlobal(cbNeeded)

        rc = GetPrinter(hPrinter, 2, pBuf, cbNeeded, cbNeeded)
        If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())

        'Convert the memory pointer to the printer info structre and free the pointer
        PI2 = Marshal.PtrToStructure(pBuf, GetType(PRINTER_INFO_2))
        Marshal.FreeHGlobal(pBuf)

        ClosePrinter(hPrinter)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddMyPrinter()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        GetMyPrinter()
    End Sub
End Class
Can anyone tell me why I'm getting "The data area passed to a system call is too small" on line

Code:
rc = GetPrinter(hPrinter, 2, pBuf, cbNeeded, cbNeeded)
        If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
I had it working but I messed it up someone and i've been trying for hours, its driving me up the wall >
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #3  
Old 10-01-2010, 06:11 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

hmmm Turns out if I use the sharing name it works fine?

rc = OpenPrinter("\\dt-win2003svr01\LP1234", hPrinter, 0)
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #4  
Old 10-01-2010, 07:04 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

Code:
Public Sub GetMyPrinter()
        Dim PI2 As New PRINTER_INFO_2
        Dim pBuf As IntPtr
        Dim hPrinter As IntPtr
        Dim cbNeeded As Integer
        Dim rc As Integer

        Dim MyAccessRights As New PRINTER_DEFAULTS
        MyAccessRights.DesiredAccess = PrinterAccessRights.PRINTER_ACCESS_ADMINISTER
        MyAccessRights.pDatatype = Nothing
        MyAccessRights.pDevMode = Nothing

        Dim MyAccessPtr As IntPtr
        Dim MyAccessPtrSize As Integer

        MyAccessPtrSize = Marshal.SizeOf(MyAccessRights)
        MyAccessPtr = Marshal.AllocHGlobal(MyAccessPtrSize)
        Marshal.StructureToPtr(MyAccessRights, MyAccessPtr, True)

        'rc = OpenPrinter("\\dt-win2003svr01\Xerox WorkCentre 7132 PCL 6", hPrinter, 0&)
        rc = OpenPrinter("\\dt-win2003svr01\LP62321", hPrinter, MyAccessPtr)
        If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())

        'The first call will fail, but it will return the length of the buffer we need to allocate
        rc = GetPrinter(hPrinter, 2, Nothing, 0, cbNeeded)

        'Allocate a memory buffer and fill the buffer with the printer info
        pBuf = Marshal.AllocHGlobal(cbNeeded)

        rc = GetPrinter(hPrinter, 2, pBuf, cbNeeded, cbNeeded)
        If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())

        'Convert the memory pointer to the printer info structre and free the pointer
        PI2 = Marshal.PtrToStructure(pBuf, GetType(PRINTER_INFO_2))

        Debug.WriteLine(PI2.Attributes)

        PI2.Attributes = Printer_Attributes.PRINTER_ATTRIBUTE_SHARED Or Printer_Attributes.PRINTER_ATTRIBUTE_NETWORK Or Printer_Attributes.PRINTER_ATTRIBUTE_PUBLISHED
        Debug.WriteLine(PI2.Attributes)
        Marshal.StructureToPtr(PI2, pBuf, True)

        Dim nRet = SetPrinter(hPrinter, 2, pBuf, 0)
        'Dim nRet = Convert.ToInt16(SetPrinter(hPrinter, 2, PI2, 0))
        'Dim nRet = SetPrinterW(hPrinter, 2, PI2, 0)
        If nRet = 0 Then
            Dim lastError = Marshal.GetLastWin32Error()
            Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
        End If
        Debug.WriteLine(nRet)

        Marshal.FreeHGlobal(pBuf)
        ClosePrinter(hPrinter)
    End Sub
I'm getting access denied, Any idea's? =(
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #5  
Old 10-02-2010, 06:43 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

Code:
Imports System.Runtime.InteropServices
Public Class Form1

    'declare the API in VB
    Private Declare Function AddPrinter Lib "winspool.drv" Alias "AddPrinterW" (ByVal pName As IntPtr, ByVal Level As Int32, <MarshalAs(UnmanagedType.LPStruct)> ByVal pPrinter As PRINTER_INFO_2) As Int32
    Public Declare Unicode Function GetPrinterW Lib "winspool.drv" (ByVal hPrinter As IntPtr, ByVal Level As Integer, ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, ByRef pcbNeeded As Integer) As Integer
    Public Declare Ansi Function OpenPrinterA Lib "winspool.drv" (ByVal pPrinterName As String, ByRef phPrinter As IntPtr, ByRef pDefault As PRINTER_DEFAULTS) As Boolean
    Public Declare Ansi Function SetPrinterW Lib "winspool.drv" (ByVal hPrinter As IntPtr, ByVal Level As Integer, ByVal pPrinter As IntPtr, ByVal Command As Integer) As Boolean
    Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr) As Boolean


    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> Public Class PRINTER_DEFAULTSS
        Public pDataType As Int32
        Public pDevMode As Int32
        <MarshalAs(UnmanagedType.U4)> Public DesiredAccess As PrinterAccessRights
    End Class

    Structure PRINTER_DEFAULTS
        Public pDatatype As String
        Public pDevMode As Integer
        Public DesiredAccess As Integer
    End Structure

    Structure PRINTER_DEFAULTSSS
        Public pDatatype As IntPtr
        Public pDevMode As IntPtr
        Public DesiredAccess As Integer
    End Structure

    <FlagsAttribute()> Public Enum PrinterAccessRights As Integer
        ' READ_CONTROL - Allowed to read printer information        
        READ_CONTROL = &H20000
        ' WRITE_DAC - Allowed to write device access control info        
        WRITE_DAC = &H40000
        ' WRITE_OWNER - Allowed to change the object owner        
        WRITE_OWNER = &H80000
        ' SERVER_ACCESS_ADMINISTER         
        SERVER_ACCESS_ADMINISTER = &H1
        '  SERVER_ACCESS_ENUMERATE        
        SERVER_ACCESS_ENUMERATE = &H2
        ' PRINTER_ACCESS_ADMINISTER Allows administration of a printer        
        PRINTER_ACCESS_ADMINISTER = &H4
        ' PRINTER_ACCESS_USE Allows printer general use (printing, querying)        
        PRINTER_ACCESS_USE = &H8
        ' PRINTER_ALL_ACCESS Allows use and administration.        
        PRINTER_ALL_ACCESS = &HF000C
        ' SERVER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE)        
        SERVER_ALL_ACCESS = &HF0003
    End Enum


    ' ##ENUMERATION_DESCRIPTION
    Public Enum Printer_Attributes
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_QUEUED
        PRINTER_ATTRIBUTE_QUEUED = &H1
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_DIRECT
        PRINTER_ATTRIBUTE_DIRECT = &H2
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_DEFAULT
        PRINTER_ATTRIBUTE_DEFAULT = &H4
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_SHARED
        PRINTER_ATTRIBUTE_SHARED = &H8
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_NETWORK
        PRINTER_ATTRIBUTE_NETWORK = &H10
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_HIDDEN
        PRINTER_ATTRIBUTE_HIDDEN = &H20
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_LOCAL
        PRINTER_ATTRIBUTE_LOCAL = &H40
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_ENABLE_DEVQ
        PRINTER_ATTRIBUTE_ENABLE_DEVQ = &H80
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS
        PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS = &H100
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST
        PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST = &H200
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_WORK_OFFLINE
        PRINTER_ATTRIBUTE_WORK_OFFLINE = &H400
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_ENABLE_BIDI The printer can operate in bidirectional mode
        PRINTER_ATTRIBUTE_ENABLE_BIDI = &H800
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_RAW_ONLY The printer can only accept raw data
        PRINTER_ATTRIBUTE_RAW_ONLY = &H1000
        ' ##ENUMERATION_MEMBER_DESCRIPTION PRINTER_ATTRIBUTE_PUBLISHED The printer is shared as a network resource
        PRINTER_ATTRIBUTE_PUBLISHED = &H2000
    End Enum
    <Flags()> _
    Enum DM As Integer
        Orientation = &H1
        PaperSize = &H2
        PaperLength = &H4
        PaperWidth = &H8
        Scale = &H10
        Position = &H20
        NUP = &H40
        DisplayOrientation = &H80
        Copies = &H100
        DefaultSource = &H200
        PrintQuality = &H400
        Color = &H800
        Duplex = &H1000
        YResolution = &H2000
        TTOption = &H4000
        Collate = &H8000
        FormName = &H10000
        LogPixels = &H20000
        BitsPerPixel = &H40000
        PelsWidth = &H80000
        PelsHeight = &H100000
        DisplayFlags = &H200000
        DisplayFrequency = &H400000
        ICMMethod = &H800000
        ICMIntent = &H1000000
        MediaType = &H2000000
        DitherType = &H4000000
        PanningWidth = &H8000000
        PanningHeight = &H10000000
        DisplayFixedOutput = &H20000000
    End Enum
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure DEVMODE
        Public Const CCHDEVICENAME As Integer = 32
        Public Const CCHFORMNAME As Integer = 32

        <MarshalAs(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 DM

        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
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCHFORMNAME)> _
        Public dmFormName As String
        Public dmLogPixels As Short
        Public dmBitsPerPel As Integer ' Declared wrong in the full framework
        Public dmPelsWidth As Integer
        Public dmPelsHeight As Integer
        Public dmDisplayFlags As Integer
        Public dmDisplayFrequency As Integer

        Public dmICMMethod As Integer
        Public dmICMIntent As Integer
        Public dmMediaType As Integer
        Public dmDitherType As Integer
        Public dmReserved1 As Integer
        Public dmReserved2 As Integer
        Public dmPanningWidth As Integer
        Public dmPanningHeight As Integer

        Public dmPositionX As Integer ' Using a PointL Struct does not work
        Public dmPositionY As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Class PRINTER_INFO_2
        <MarshalAs(UnmanagedType.LPTStr)> Public pServerName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pPrinterName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pShareName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pPortName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pDriverName As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pComment As String
        Public pDevMode As IntPtr
        <MarshalAs(UnmanagedType.LPTStr)> Public pLocation As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pSepFile As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pPrintProcessor As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pDatatype As String
        <MarshalAs(UnmanagedType.LPTStr)> Public pParameters As String
        Public pSecurityDescriptor As IntPtr
        Public Attributes As Integer
        Public Priority As Integer
        Public DefaultPriority As Integer
        Public StartTime As Integer
        Public UntilTime As Integer
        Public Status As Integer
        Public cJobs As Integer
        Public AveragePPM As Integer
    End Class

    Function AddMyPrinter() As Boolean

        Dim PI2 As New PRINTER_INFO_2
        'only printer name, driver name, port name and print processor need to be filled
        With PI2
            .pServerName = "\\dt-win2003svr01"
            .pPrinterName = "Xerox WorkCentre 7132 PCL 6"
            .pShareName = "LP62321"
            .pPortName = "LP62321"
            .pDriverName = "Xerox WorkCentre 7132 PCL 6"
            .pComment = "SAME"
            .pLocation = "SAMEER"
            .pDevMode = 0
            .pSepFile = String.Empty
            .pPrintProcessor = "WinPrint"
            .pDatatype = String.Empty
            .pParameters = String.Empty
            .pSecurityDescriptor = 0
            .Attributes = Printer_Attributes.PRINTER_ATTRIBUTE_SHARED Or Printer_Attributes.PRINTER_ATTRIBUTE_NETWORK '8216 ' Shared + Dictory + Network
            .Priority = 0
            .DefaultPriority = 0
            .StartTime = 0
            .UntilTime = 0
        End With
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #6  
Old 10-02-2010, 06:45 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

Code:
        Dim str As String = "\\dt-win2003svr01"
        Dim ptr As IntPtr = Marshal.StringToHGlobalAuto(str)
        Dim mystring As String = Marshal.PtrToStringAuto(ptr)

        Dim x As Integer = AddPrinter(ptr, 2, PI2)
        If x = 0 Then
            MsgBox("Failed !!!")
            MsgBox(Err.LastDllError)
            End
        End If
        Return True
    End Function

    Public Sub GetMyPrinter()
        Dim PI2 As New PRINTER_INFO_2
        Dim pBuf As IntPtr
        Dim hPrinter As IntPtr
        Dim PreCbNeeded As Integer
        Dim cbNeeded As Integer
        Dim rc As Integer

        Dim MyAccessRights As New PRINTER_DEFAULTS
        MyAccessRights.DesiredAccess = PrinterAccessRights.PRINTER_ALL_ACCESS
        Dim MyAccessPtr As IntPtr
        Dim MyAccessPtrSize As Integer

        rc = OpenPrinterA("\\dt-win2003svr01\LP62321", hPrinter, MyAccessRights)
        If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())

        'The first call will fail, but it will return the length of the buffer we need to allocate
        rc = GetPrinterW(hPrinter, 2, Nothing, 0, cbNeeded)

        'Allocate a memory buffer and fill the buffer with the printer info
        pBuf = Marshal.AllocHGlobal(cbNeeded * 2) 'Weird!?

        rc = GetPrinterW(hPrinter, 2, pBuf, cbNeeded * 2, cbNeeded) 'Weird!? cbNeeded + 1 worked?
        If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())

        'Convert the memory pointer to the printer info structre and free the pointer
        PI2 = Marshal.PtrToStructure(pBuf, GetType(PRINTER_INFO_2))

        Debug.WriteLine("Attributes: " & PI2.Attributes)
        Debug.WriteLine("DefaultPriority: " & PI2.DefaultPriority)
        Debug.WriteLine("pComment: " & PI2.pComment)
        Debug.WriteLine("pDatatype: " & PI2.pDatatype)
        Debug.WriteLine("pDevMode: " & PI2.pDevMode.ToString)
        Debug.WriteLine("pDriverName: " & PI2.pDriverName)
        Debug.WriteLine("pLocation: " & PI2.pLocation)
        Debug.WriteLine("pServerName: " & PI2.pServerName)
        Debug.WriteLine("pShareName: " & PI2.pShareName)
        Debug.WriteLine("Attributes: " & PI2.Attributes)

        PI2.Attributes = Printer_Attributes.PRINTER_ATTRIBUTE_SHARED Or Printer_Attributes.PRINTER_ATTRIBUTE_NETWORK Or Printer_Attributes.PRINTER_ATTRIBUTE_PUBLISHED
        Debug.WriteLine(PI2.Attributes)
        Marshal.StructureToPtr(PI2, pBuf, True)

        Dim nRet = Convert.ToInt16(SetPrinterW(hPrinter, 2, pBuf, 0))
        If nRet = 0 Then
            Dim lastError = Marshal.GetLastWin32Error()
            Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
        End If
        Debug.WriteLine(nRet)

        Marshal.FreeHGlobal(pBuf)
        ClosePrinter(hPrinter)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddMyPrinter()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        GetMyPrinter()
    End Sub
End Class
Alright, I still cant get this darn thing to be checked in "LIST IN DIRECTORY"


Output
Quote:
Attributes: 24
DefaultPriority: 0
pComment: SAME
pDatatype:
pDevMode: 0
pDriverName: Xerox WorkCentre 7132 PCL 6
pLocation: \\dt-win2003svr01\LP62321,DrvCo
pServerName: \\dt-win2003svr01
pShareName: LP62321
Attributes: 24
8216
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
  #7  
Old 01-05-2011, 07:08 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

I figure I would come back with the answer.

The answer to this issue is Printer_Info_7 =)
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.- GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
 
GetPrinter, SetPrinter. I HATE IT! -.-
GetPrinter, SetPrinter. I HATE IT! -.-
 
-->