Announcement

Collapse
No announcement yet.

Enumprinters

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Enumprinters

    i have been trying to get enumprinters to work for a long
    time and my most resent experence has resulted in a major
    crash. i found a forum on it:

    but all the links to the source code have been destroyed.
    can anyone show me some source code that for enumprinters
    please.

    ------------------

  • #2
    Here is some code I got from Semen a few weeks ago.

    Code:
       #Compile Exe
       #Register None
       #Dim All
       #Include "WIN32API.INC"
    
       Function PbMain()
          Local i As Long, dwNeeded As Long, dwReturned As Long, MaxEl As Long
          EnumPrinters %PRINTER_ENUM_LOCAL, ByVal %NULL, 5, _
             ByVal 0, ByVal 0, dwNeeded, dwReturned
          MaxEl = Ceil(dwNeeded / 20) ' 20 is length of Printer_Info_5
          ReDim PInfo5(1 To MaxEl) As Printer_Info_5
          EnumPrinters %PRINTER_ENUM_LOCAL, ByVal %NULL, 5, _
             ByVal VarPtr(PInfo5(1)), dwNeeded, dwNeeded, dwReturned
          For i = 1 To dwReturned
             MsgBox PInfo5(i)[email protected] + "," + PInfo5(i)[email protected], , _
                "Printer" + Str$(i)
          Next
       End Function
    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

    Comment


    • #3
      Another simple way
      Code:
             Dim PrinterName As Asciiz * 255, p As Asciiz Ptr, Section As Asciiz * 32767
             GetProfileString "WINDOWS", "DEVICE", ",,,", PrinterName, SizeOf(PrinterName)
             MsgBox "Default is " + PrinterName
             If GetProfileSection ("PrinterPorts", Section, SizeOf(Section)) > 0 Then _
                p = VarPtr(Section): While Len(@p): MsgBox @p: p = p + Len(@p) + 1: Wend
      ------------------
      E-MAIL: [email protected]

      Comment


      • #4
        Code:
        'Some modifications were necessary for NT
        'which I received from Dave Navarro.
        #COMPILE EXE
        #REGISTER NONE
        #DIM ALL
        #INCLUDE "WIN32API.INC"
        FUNCTION IsWin95() EXPORT AS LONG
          LOCAL vi AS OSVERSIONINFO
          vi.dwOsVersionInfoSize = SIZEOF(vi)
          GetVersionEx vi
          FUNCTION = (vi.dwPlatformId = %VER_PLATFORM_WIN32_WINDOWS)
        END FUNCTION
        FUNCTION GetPrinters AS STRING
        
          ON ERROR GOTO GetPrinterError
          DIM Level AS LONG
          DIM Needed AS LONG
          DIM returned AS LONG
          DIM Element AS LONG
          DIM Pi5(0:0) AS PRINTER_INFO_5
          DIM Pi2(0:0) AS PRINTER_INFO_2
          LOCAL Answer AS STRING
        
          IF IsWin95 THEN
             Level = 5
          ELSE
             Level = 2  'Printer structure to use
          END IF
        
          IF Level = 5 THEN
            'First call to see how big WINDOWS 95 printer_info_5 structure
            EnumPrinters %PRINTER_ENUM_LOCAL, BYVAL 0, Level, BYVAL %NULL, _
                BYVAL 0, needed&, returned&
            REDIM PI5(needed& / SIZEOF(Pi5(0)))' AS Printer_Info_5 'Dim using size of structure
            EnumPrinters %PRINTER_ENUM_LOCAL, BYVAL 0, BYVAL Level, _
                BYVAL VARPTR(PI5(0)), needed&, needed&, returned&
        
            FOR element = 0 TO returned-1
              Answer = Answer + PI5(Element,0)[email protected] _  'printer name
                              + ","                          _  'comma
                              + PI5(Element,1)[email protected]    _  'port name
                              + ","
            NEXT
            Answer = LEFT$(Answer,LEN(Answer)-1)
          ELSE  '   Windows NT or 2000
            'First call to see how big the WINDOWS NT PRINTER_INFO_2 structure should be
            EnumPrinters %PRINTER_ENUM_CONNECTIONS OR %PRINTER_ENUM_LOCAL OR %PRINTER_ENUM_NETWORK, _
                    BYVAL %NULL, Level, BYVAL %NULL, 0, needed&, returned&
            REDIM PI2(Needed& \ SIZEOF(PI2(0)) + 1)
            EnumPrinters %PRINTER_ENUM_CONNECTIONS OR %PRINTER_ENUM_LOCAL OR %PRINTER_ENUM_NETWORK, _
                    "", Level, BYVAL VARPTR(PI2(0)), SIZEOF(PI2(0)) * (UBOUND(PI2(1)) + 1), _
                    needed&, returned&
        
            FOR element = 0 TO returned& -1
              'Printer$(element,0) = TRIM$(PI2(Element)[email protected])
              'Printer(element,1) = TRIM$(PI2(Element)[email protected]) 'needed?
              'Printer$(element,1) = RTRIM$(PI2(Element)[email protected])
              Answer = Answer + PI2(Element,0)[email protected] _  'printer name
                              + ","                          _  'comma
                              + PI2(Element,1)[email protected]    _  'port name
                              + ","
            NEXT
            Answer = LEFT$(Answer,LEN(Answer)-1)
          END IF
          FUNCTION = Answer
        ExitGetPrinter:
          EXIT FUNCTION
        
        GetPrinterError:
          IF CONSOUT THEN
            STDOUT "Error " + STR$(ERR) + "Press any key to continue"
            WAITKEY$
          END IF
          RESUME ExitGetPrinter
        END FUNCTION
        
        FUNCTION PBMAIN AS LONG
          DIM answer$,printer&,TotalPrinters&,offset&
          answer$   = GetPrinters
          totalprinters& = PARSECOUNT(answer$,",")/2
          FOR printer& = 1 TO totalprinters&
            PRINT Printer&;PARSE$(answer$,printer&+offset&);TAB(30)" on port ";_ 'name
                           PARSE$(answer$,printer&+offset&+1)             'port
            offset& = offset& + 1&
          NEXT
          WAITKEY$
        END FUNCTION

        ------------------

        Comment

        Working...
        X