List installed printers / Set default printer

Share your advanced PureBasic knowledge/code with the community.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

List installed printers / Set default printer

Post 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)

Last edited by gnozal on Fri Nov 05, 2004 8:35 am, edited 1 time in total.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2810
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Excelent code !!!

great help :mrgreen:
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Does anyone know of a way to do this that works with 2000+ ?
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Code: Select all

SetDefaultPrinter_(printername.s)
El_Choni
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Lets all pretend that I didn't just miss that.

*crawls back under rock*
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply