this is the result of hard work on this forum…
enjoy !
[code]Public Function SystemCountPrinters() as Integer
’ returns the number of defined printers on the running system
SystemScanPrinters
Return UBound(mPrinters)+1
End Function
[/code]
global in a module :
Public Property mPrinters() as PrinterDefinition
Structure PrinterDefinition
printerName as string*80
printerModel as string*50
printerURI as string*127
End Structure
[code]Public Sub SystemScanPrinters()
ReDim mPrinters(-1)
dim p as PrinterDefinition
’ --------------------------------------------------------------------------------
’ search for the available printers on the host system
’ --------------------------------------------------------------------------------
dim myShell as new shell
#if TargetMacOS or TargetLinux
myShell.Execute “lpstat -p”
#Elseif TargetWindows
myShell.Execute “wmic printer get name,default”
#endif
dim result as String = myShell.Result
dim reslines() as String = result.ReadFields(EndOfLine)
for each line as String in reslines
#if TargetMacOS or TargetLinux
p.printerName = MotNumero(line,2)
mPrinters.Append p
#Elseif TargetWindows
p.printerName = MotNumero(line,2)
if p.printerName<>“Name” then
mPrinters.Append p
end if
#endif
next
’ --------------------------------------------------------------------------------
’ search for the printer model
’ --------------------------------------------------------------------------------
’ --------------------------------------------------------------------------------
’ search for the printer URI
’ --------------------------------------------------------------------------------
End Sub
[/code]
and the final one :
[code]Public Sub SystemSetDefaultPrinter(aPrinterName as String)
if UBound(mPrinters)<0 then
SystemScanPrinters
end if
if UBound(mPrinters)>=0 then
dim printer,foundPrinter as PrinterDefinition
' recherche l'imprimante
for each printer in mPrinters
if printer.printerName = aPrinterName then
foundPrinter = printer
Exit For
end if
next
if foundPrinter.printerName<>"" then
dim myShell as new shell
#if TargetMacOS or TargetLinux
myShell.Execute "lpoptions -d "+aPrinterName
#Elseif TargetWindows
myShell.Execute "wmic printer where name='"+aPrinterName+"' call setdefaultprinter"
#endif
else
Alerte "SystemSetDefaultPrinter: Printer "+aPrinterName+" is unknown."
end if
Else
Alerte “SystemSetDefaultPrinter: No printer defined.”
end if
End Sub
[/code]