InstalledPrinters and Windows Server 2003 R2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWFyYw==?=

    InstalledPrinters and Windows Server 2003 R2

    I am using VB 2005 to get the printers that have been installed on a Windows
    Server 2003 R2 box. I would like to write these printers into a table in SQL
    Server. The problem is when I run the code using:
    System.Drawing. Printing.Printe rSettings.Insta lledPrinters and count and
    item no printers are found. Printers are installed on the server and when I
    run the same code on a Win XP box all installed printers are listed. Anyone
    have any idea why the 2003 R2 box does not return the installed printers?
    Any help would be greatly appreciated.
    Marc
  • Duncan Jones

    #2
    Re: InstalledPrinte rs and Windows Server 2003 R2

    Hi

    I found that InstalledPrinte rs had problems when one or more printers had
    different permissions to the others
    I had to fall back on the EnumPrinters API. Maybe make sure that the user
    running the code has the same permissions on all the printers?

    Hope this helps,
    Duncan


    "Marc" <Marc@discussio ns.microsoft.co mwrote in message
    news:CBCE03DA-AD1D-4190-BDD9-5580493C1F0B@mi crosoft.com...
    >I am using VB 2005 to get the printers that have been installed on a
    >Windows
    Server 2003 R2 box. I would like to write these printers into a table in
    SQL
    Server. The problem is when I run the code using:
    System.Drawing. Printing.Printe rSettings.Insta lledPrinters and count and
    item no printers are found. Printers are installed on the server and when
    I
    run the same code on a Win XP box all installed printers are listed.
    Anyone
    have any idea why the 2003 R2 box does not return the installed printers?
    Any help would be greatly appreciated.
    Marc

    Comment

    • =?Utf-8?B?TWFyYw==?=

      #3
      Re: InstalledPrinte rs and Windows Server 2003 R2

      Thanks for your help Duncan. I decided to try what you have suggested using
      the EnumPrinters API but I am having problems getting this API to work on the
      Server 2003 R2 box. It works fine on XP. When the following code gets to
      the EnumPrinters line it just stops and does not return an error. Am I using
      the wrong lib for 2003 R2?

      Private Declare Function EnumPrinters Lib "winspool.d rv" Alias
      "EnumPrinte rsA" (ByVal flags As Integer, ByVal name As String, ByVal Level As
      Integer, ByRef pPrinterEnum As Integer, ByVal cdBuf As Integer, ByRef
      pcbNeeded As Integer, ByRef pcReturned As Integer) As Integer

      Private Structure PRINTER_INFO_4
      Dim pPrinterName As String
      Dim pServerName As String
      Dim Attributes As Integer
      End Structure

      Dim PrinterEnum() As PRINTER_INFO_4, FNLoop As Integer = 0,
      PRINTER_ENUM_LO CAL = &H2, pPrinterEnum() As Integer, cdBuf As Integer =
      10000, pcbNeeded As Integer, pcReturned As Integer, ReturnValue As Integer
      ReDim pPrinterEnum(0 To cdBuf / 4)

      ReturnValue = EnumPrinters(PR INTER_ENUM_LOCA L, "", 4, pPrinterEnum(0) ,
      cdBuf, pcbNeeded, pcReturned)

      Thanks,
      Marc


      "Duncan Jones" wrote:
      Hi
      >
      I found that InstalledPrinte rs had problems when one or more printers had
      different permissions to the others
      I had to fall back on the EnumPrinters API. Maybe make sure that the user
      running the code has the same permissions on all the printers?
      >
      Hope this helps,
      Duncan
      >
      >
      "Marc" <Marc@discussio ns.microsoft.co mwrote in message
      news:CBCE03DA-AD1D-4190-BDD9-5580493C1F0B@mi crosoft.com...
      I am using VB 2005 to get the printers that have been installed on a
      Windows
      Server 2003 R2 box. I would like to write these printers into a table in
      SQL
      Server. The problem is when I run the code using:
      System.Drawing. Printing.Printe rSettings.Insta lledPrinters and count and
      item no printers are found. Printers are installed on the server and when
      I
      run the same code on a Win XP box all installed printers are listed.
      Anyone
      have any idea why the 2003 R2 box does not return the installed printers?
      Any help would be greatly appreciated.
      Marc
      >

      Comment

      • Duncan Jones

        #4
        Re: InstalledPrinte rs and Windows Server 2003 R2

        I can't see anything wrong with what you have there - however just in case
        it is of use, my code is as follows:-

        #Region "EnumPrinte rs"
        <DllImport("win spool.drv", EntryPoint:="En umPrinters", _
        SetLastError:=T rue, CharSet:=CharSe t.Auto, _
        ExactSpelling:= False, _
        CallingConventi on:=CallingConv ention.StdCall) _
        Public Function EnumPrinters(<I nAttribute()ByV al Flags As
        EnumPrinterFlag s, _
        <InAttribute()B yVal Name As String, _
        <InAttribute()B yVal Level As Int32, _
        <OutAttribute() ByVal lpBuf As IntPtr, _
        <InAttribute()B yVal cbBuf As Int32, _
        <OutAttribute() ByRef pcbNeeded As Int32,
        _
        <OutAttribute() ByRef pcbReturned As
        Int32) As Boolean

        End Function

        <DllImport("win spool.drv", EntryPoint:="En umPrinters", _
        SetLastError:=T rue, CharSet:=CharSe t.Auto, _
        ExactSpelling:= False, _
        CallingConventi on:=CallingConv ention.StdCall) _
        Public Function EnumPrinters(<I nAttribute()ByV al Flags As
        EnumPrinterFlag s, _
        <InAttribute()B yVal Name As IntPtr, _
        <InAttribute()B yVal Level As Int32, _
        <OutAttribute() ByVal lpBuf As IntPtr, _
        <InAttribute()B yVal cbBuf As Int32, _
        <OutAttribute() ByRef pcbNeeded As Int32, _
        <OutAttribute() ByRef pcbReturned As Int32) As
        Boolean

        End Function

        #End Region

        and is used thus:-
        Public Sub New()
        Dim pcbNeeded As Int32 '\\ Holds the requires size of the output
        buffer (in bytes)
        Dim pcReturned As Int32 '\\ Holds the returned size of the output
        buffer
        Dim pPrinters As IntPtr
        Dim pcbProvided As Int32 = 0

        If Not EnumPrinters(En umPrinterFlags. PRINTER_ENUM_NA ME,
        String.Empty, 1, pPrinters, 0, pcbNeeded, pcReturned) Then
        If pcbNeeded 0 Then
        pPrinters = Marshal.AllocHG lobal(pcbNeeded )
        pcbProvided = pcbNeeded
        If Not EnumPrinters(En umPrinterFlags. PRINTER_ENUM_NA ME,
        String.Empty, 1, pPrinters, pcbProvided, pcbNeeded, pcReturned) Then
        Throw New Win32Exception
        End If
        End If
        End If

        If pcReturned 0 Then
        '\\ Get all the monitors for the given server
        Dim ptNext As IntPtr = pPrinters
        While pcReturned 0
        Dim pi1 As New PRINTER_INFO_1
        Marshal.PtrToSt ructure(ptNext, pi1)
        If Not pi1.pName Is Nothing Then
        Me.Add(New PrinterInformat ion(pi1.pName,
        PrinterAccessRi ghts.PRINTER_AC CESS_USE, False)) ', pi2.pLocation,
        pi2.pComment, pi2.pServerName , 1))
        End If
        ptNext = New IntPtr(ptNext.T oInt32 +
        Marshal.SizeOf( GetType(PRINTER _INFO_1)))
        pcReturned -= 1
        End While
        End If

        '\\ Free the allocated buffer memory
        If pPrinters.ToInt 32 0 Then
        Marshal.FreeHGl obal(pPrinters)
        End If

        End Sub

        (The full code is in CodePlex : http://www.codeplex.com/PrintQueueWatch in
        case I have missed anything in this cut-and-paste)

        Hope this helps,
        Duncan

        "Marc" <Marc@discussio ns.microsoft.co mwrote in message
        news:305E2ED2-2318-4108-9516-940CD685C715@mi crosoft.com...
        Thanks for your help Duncan. I decided to try what you have suggested
        using
        the EnumPrinters API but I am having problems getting this API to work on
        the
        Server 2003 R2 box. It works fine on XP. When the following code gets to
        the EnumPrinters line it just stops and does not return an error. Am I
        using
        the wrong lib for 2003 R2?
        >
        Private Declare Function EnumPrinters Lib "winspool.d rv" Alias
        "EnumPrinte rsA" (ByVal flags As Integer, ByVal name As String, ByVal Level
        As
        Integer, ByRef pPrinterEnum As Integer, ByVal cdBuf As Integer, ByRef
        pcbNeeded As Integer, ByRef pcReturned As Integer) As Integer
        >
        Private Structure PRINTER_INFO_4
        Dim pPrinterName As String
        Dim pServerName As String
        Dim Attributes As Integer
        End Structure
        >
        Dim PrinterEnum() As PRINTER_INFO_4, FNLoop As Integer = 0,
        PRINTER_ENUM_LO CAL = &H2, pPrinterEnum() As Integer, cdBuf As Integer =
        10000, pcbNeeded As Integer, pcReturned As Integer, ReturnValue As Integer
        ReDim pPrinterEnum(0 To cdBuf / 4)
        >
        ReturnValue = EnumPrinters(PR INTER_ENUM_LOCA L, "", 4, pPrinterEnum(0) ,
        cdBuf, pcbNeeded, pcReturned)
        >
        Thanks,
        Marc
        >
        >
        "Duncan Jones" wrote:
        >
        >Hi
        >>
        >I found that InstalledPrinte rs had problems when one or more printers had
        >different permissions to the others
        >I had to fall back on the EnumPrinters API. Maybe make sure that the
        >user
        >running the code has the same permissions on all the printers?
        >>
        >Hope this helps,
        > Duncan
        >>
        >>
        >"Marc" <Marc@discussio ns.microsoft.co mwrote in message
        >news:CBCE03D A-AD1D-4190-BDD9-5580493C1F0B@mi crosoft.com...
        >I am using VB 2005 to get the printers that have been installed on a
        >Windows
        Server 2003 R2 box. I would like to write these printers into a table
        in
        SQL
        Server. The problem is when I run the code using:
        System.Drawing. Printing.Printe rSettings.Insta lledPrinters and count
        and
        item no printers are found. Printers are installed on the server and
        when
        I
        run the same code on a Win XP box all installed printers are listed.
        Anyone
        have any idea why the 2003 R2 box does not return the installed
        printers?
        Any help would be greatly appreciated.
        Marc
        >>

        Comment

        Working...