Announcement

Collapse
No announcement yet.

Enumate Printers and Switch Default printer(s) on fly

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

  • Enumate Printers and Switch Default printer(s) on fly

    I'm wanting to get a list of all currently installed printers in a
    users computer and then list them in a listbox and the user selects
    the printer and the program will make it the default printer.

    I'm doing this because our MRP package now prints to the default
    printer and some users have two or more printers installed over a
    network.

    Thanks

    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

  • #2
    Greg --
    Somehow I wrote following code.
    But honestly in real programs I prefer to use default printer with a possibillity to select another
    (using PrintDlg).

    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
    ------------------
    E-MAIL: [email protected]

    Comment


    • #3
      Semen,

      Your program lists all the printers and I agree that I prefer to
      print to the default printer with a dropdown list of the
      remaining printers. Unforuntetly our MRP package doesn't do this
      and I don't want the users to have to go into printers and change
      it manually.

      So does anyone have any code to set the default printer?

      Thanks



      ------------------
      -Greg
      -Greg
      [email protected]
      MCP,MCSA,MCSE,MCSD

      Comment


      • #4
        This might not be the "microsoft approved" way, but it should work. Use write profile string to make an entry in the win.ini file for the printer.
        Use the routine below to obtain a dc for any printer. Once you have the dc, just use StartDoc on that dc, etc and print to it to create the pages.
        (note - I use wrappers around Get/WriteProfileString, so you'll have to substitute your own code.)

        '- Set the default printer
        WriteWinIni("WINDOWS","DEVICE","\\unr_server\don\hp5p")

        '- Get the default printer
        zPrinter = GetWinIni("WINDOWS", "DEVICE", "")

        Note that this code works on my Win2K machine, even though I don't see a [windows] section in the win.ini file. Windows must intercept this call.
        --Don


        ------------------
        www.basicguru.com/dickinson
        Don Dickinson
        www.greatwebdivide.com

        Comment


        • #5
          Don --
          There are problems under Win2000 with this way.
          I already posted a code and found problems.

          MSDN talks
          [quote]
          To set the default printer on Windows 2000, call SetDefaultPrinter. To set the default printer to earlier operating systems, call GetProfileString, WriteProfileString, and SendNotifyMessage, as shown in the following code:

          // read PrinterPorts section from win.ini
          // returned string should be of the form "driver,port,timeout,timeout", i.e. "winspool,LPT1:,15,45".
          GetProfileString(TEXT("Windows"),TEXT("Device"),_T(",,,"),m_szName, COUNTOF(m_szName));
          WriteProfileString(TEXT("Windows"),TEXT("Device"),pszPrinterName ));
          #if WM_WININICHANGE != WM_SETTINGCHANGE
          // Old message type; for windows95
          SendNotifyMessage(HWND_BROADCAST,WM_WININICHANGE,0,(LPARAM)szWindows);
          #endif
          // New message type
          SendNotifyMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)szWindows);
          [/code]

          I selected "old" way. But it looks that under Win2000 only new way works.
          Somebody can test - does old way work on Win98/95/NT4 ?
          If works, I'll simply add a new function exactly for Win2000.

          ------------------
          E-MAIL: [email protected]

          Comment


          • #6
            'Suggestion:
            'If using NT use PRINTER_INFO_4 and %PRINTER_ENUM_CONNECTIONS

            Code:
            #DIM ALL         'Place all printer names in PRINTERS.INI
            #REGISTER NONE
            #COMPILE EXE
            $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 GetPrinterNames() AS STRING
              DIM Level AS LONG
              DIM Needed AS LONG
              DIM Returned AS LONG
              DIM Element AS LONG
              DIM EntireString AS STRING
              IF IsWin95 THEN Level = 5 ELSE Level = 4  'Printer structure to use
              IF Level = 5 THEN
                 'First call to see how big WINDOWS 95 printer_info_5 structure
                 CALL EnumPrinters(%PRINTER_ENUM_LOCAL, _
                          BYVAL 0, _
                          BYVAL Level, _
                          BYVAL %NULL, _
                          BYVAL 0, _
                          needed&, _       'Size of structure in bytes
                          returned&)
                 DIM PI5(0) AS PRINTER_INFO_5
                 REDIM PI5(needed& \ SIZEOF(PI5(0)))
                 CALL EnumPrinters(%PRINTER_ENUM_LOCAL, _
                          BYVAL 0, _
                          BYVAL Level, _
                          BYVAL VARPTR(PI5(0)), _
                          needed&, _
                          needed&, _
                          returned&)                    'Number of printers
                FOR Element = 0 TO Returned& - 1
                    EntireString = EntireString + PI5(Element)[email protected]+","
                NEXT
                ERASE PI5
              ELSE  '                      Windows NT
                'First call to see how big WINDOWS NT printer_info_4 structure
                CALL EnumPrinters(%PRINTER_ENUM_CONNECTIONS, _
                        BYVAL 0, _
                        BYVAL Level, _
                        BYVAL %NULL, _
                        BYVAL 0, _
                        needed&, _  'Size of structure returned in needed&
                        returned&)
                DIM PI4(0) AS PRINTER_INFO_4
                REDIM PI4(needed& \ SIZEOF(PI4(0)))
                CALL EnumPrinters(%PRINTER_ENUM_CONNECTIONS, _ 'Use CONNECTIONS with NT
                        BYVAL 0, _
                        BYVAL Level, _
                        BYVAL VARPTR(PI4(0)), _
                        needed&, _
                        needed&, _
                        returned&)                      'Number of printers
                FOR Element = 0 TO Returned& - 1
                  EntireString = EntireString + PI4(Element)[email protected]+","
                NEXT
                ERASE PI4
              END IF
              GetPrinterNames = LEFT$(EntireString,LEN(EntireString)-1)
            END FUNCTION
            
            FUNCTION PBMAIN () AS LONG
              DIM PrinterNames AS STRING           'Will hold returned printer names
              DIM Count AS LONG                    'Count number of printer names
              DIM Org AS LONG
              DIM EachPrinter AS STRING
              PrinterNames = GetPrinterNames()     'Assign printer names from function
              Count = PARSECOUNT(PrinterNames)     'Number of concatenated printer names
              IF count THEN
                 OPEN "PRINTERS.INI" FOR OUTPUT AS #1
                 FOR org = 1 TO Count
                   EachPrinter = PARSE$(PrinterNames$,Org)
                   PRINT #1,EachPrinter 'comma separated printer names
                   'PRINT EachPrinter
                 NEXT
                 CLOSE #1
              END IF
              'WAITKEY$
            END FUNCTION


            ------------------
            The world is full of apathy, but who cares?

            Comment

            Working...
            X