Page 1 of 1

List installed printers / Set default printer

Posted: Tue Apr 29, 2003 2:34 pm
by gnozal
Code updated For 5.20+

This is an exemple how to get a list of the installed printers and how to set one to the default windows printer.
Is there a more 'clean' way, without using WIN.INI and working with all windows versions (95/98/Me/NT/2K/XP) ?

Code: Select all

; ---------------------------
; LIST ALL INSTALLED PRINTERS
;  AND SET DEFAULT PRINTER
; ---------------------------
;
; Adapted from VB source ...
; http://www.freevbcode.com/ShowCode.Asp?ID=641
;
; Printer list structure & list
Structure Printers
  P.s
EndStructure
Global NewList Printers.Printers()
; ------------
; Get installed printers
Procedure GetPrinterList()
  
  ClearList(Printers())
  
  Buffersize.l  = 8102
  *Buffer       = AllocateMemory(Buffersize,0)
  TempPrinter.s = Space(1024)
  
  If GetProfileString_("devices",0,"",*Buffer,Buffersize)
    TempString.s = PeekS(*Buffer)
    Length = Len(TempString)
    While TempString <> ""
      GetPrivateProfileString_("devices",TempString,"",TempPrinter,1024,"Win.Ini")
      AddElement(Printers())
      Printers()\P = TempString+","+StringField(TempPrinter,1,",")+","+StringField(TempPrinter,2,",")
      TempString = PeekS(*Buffer+Length + 1)
      Length = Length + Len(TempString) + 1
    Wend
  Else
    MessageRequester("Error","No printer installed",64)
  EndIf
  
  FreeMemory(*Buffer)
  
EndProcedure
; ------------
; Set default printer (should work for all windows versions)
Procedure SetDefaultPrinter(DeviceLine.s)
  
  ;DeviceLine=PrinterName,DriverName,PrinterPort
  ;Store the new printer information in the
  ;[WINDOWS] section of the WIN.INI file for
  ;the DEVICE= item
  WriteProfileString_("windows", "Device", DeviceLine)
  
  ;Cause all applications To reload the INI file
  SendMessage_(#HWND_BROADCAST, #WM_WININICHANGE, 0, "windows")
  
EndProcedure
; ------------
; MAIN

; Get installed printers
GetPrinterList()
; List the printers
ResetList(Printers())
Debug "Printers list :"
While NextElement(Printers())
  Debug Printers()\P 
Wend
Debug "---------------"
Debug "Make n°5 as default Printer"
SelectElement(Printers(), 4)
Debug "This is "+Printers()\P
SetDefaultPrinter(Printers()\P)


Posted: Wed Apr 30, 2003 8:57 pm
by Num3
Excelent code !!!

great help :mrgreen:

Posted: Thu Nov 04, 2004 7:15 pm
by Karbon
Does anyone know of a way to do this that works with 2000+ ?

Posted: Thu Nov 04, 2004 7:32 pm
by El_Choni

Code: Select all

SetDefaultPrinter_(printername.s)

Posted: Thu Nov 04, 2004 7:36 pm
by Karbon
Lets all pretend that I didn't just miss that.

*crawls back under rock*