Jump to content

set printer configuration


averlon
 Share

Recommended Posts

I have an HP Laserjet printer with duplex.

Always after updating windows, the duplex function is disabled.

Now I am looking for a autoit-script to set the duplex function what I could run every day at boot.

 

I would like to have some help how to do this!

I did not do any autoit on HW!

 

Thanks for hints!

Link to comment
Share on other sites

 

https://learn.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-setprinter-api

or as so far:
 

#include <Array.au3>
_Example()
Func _Example()

    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Local $colPrinters = $oWMIService.ExecQuery("Select * from Win32_Printer")

    For $oPrinter In $colPrinters
        ConsoleWrite($oPrinter.Name & @CRLF)

        If $oPrinter.Name = "Canon iR-ADV C5250/5255 UFR II" Then
            Local $capabilities = $oPrinter.Capabilities
            _ArrayDisplay($capabilities)
            ConsoleWrite(($capabilities=Null) & @CRLF)
            ConsoleWrite(VarGetType($capabilities) & @CRLF)
        EndIf
    Next
EndFunc   ;==>_Example

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 2 weeks later...
On 10/13/2023 at 11:58 AM, mLipok said:

 

https://learn.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-setprinter-api

or as so far:
 

#include <Array.au3>
_Example()
Func _Example()

    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Local $colPrinters = $oWMIService.ExecQuery("Select * from Win32_Printer")

    For $oPrinter In $colPrinters
        ConsoleWrite($oPrinter.Name & @CRLF)

        If $oPrinter.Name = "Canon iR-ADV C5250/5255 UFR II" Then
            Local $capabilities = $oPrinter.Capabilities
            _ArrayDisplay($capabilities)
            ConsoleWrite(($capabilities=Null) & @CRLF)
            ConsoleWrite(VarGetType($capabilities) & @CRLF)
        EndIf
    Next
EndFunc   ;==>_Example

 

What does this UDF tell me?

The output is a table with 2 columns, ROW and Col0 including some values.

I assume, this shows the raw data what capabilities the printer has. But question is how to translate the figures and how to set the duplex function.

I guess, I am not sure, there is nothing in the script on that!?

Link to comment
Share on other sites

3 hours ago, averlon said:

I guess, I am not sure, there is nothing in the script on that!?

change _ArrayDisplay() to _ArrayToString() and post the results. That way we ( @mLipok really :) ) can help you better ;) 

Edit: reading the code a bit more, I see that you have an HP and he's filtering for a cannon printer ( he may not have your printer. I myself have none ).
Do remove the filter and tweak it to have an output that makes sense.

Edited by argumentum
clarify

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

3 hours ago, argumentum said:

That way we ( @mLipok really :) ) can help you better ;) 

Rather @Nine in this topic:

 

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

O.K. - it took some time to understand the way the UDF has to be used!!!!

Anyhow, I don't get it working.

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <Printer_UDF.au3>

Opt("MustDeclareVars", True)

Example()

Func Example()
  Local $hPrinter = _PRNT_OpenPrinter("HP Color LaserJet CP202X PS Class Driver")
  Local $tDevMode = _PRNT_GetPrinterInfo($hPrinter, True)
  ConsoleWrite($tDevMode.Duplex & @CRLF)
  ConsoleWrite($tDevMode.FormName & @CRLF)
  ConsoleWrite(_PRNT_IsValidDevmode($tDevMode) & @CRLF)

  _PRNT_SetPrinterInfo($hPrinter, $DM_DUPLEX, 1, True)
  _PRNT_ClosePrinter($hPrinter)
EndFunc   ;==>Example

The printer configuration shows still "Duplex = not installed".

The "ConsoleWrite($tDevMode.Duplex & @CRLF)" shows "1".

By the way: I found a way to configure the printer via Microsoft PowerShell.

Set-PrinterProperty -PrinterName "HP Color LaserJet CP202X PS Class Driver" -PropertyName "Config:Duplexer" -Value "Installed"

Here the value to be set is "Installed" - and this works!

Therefore I tried to modify the UDF

Func _PRNT_SetPrinterInfo($hPrinter, $iProp, $iValue, $bGlobal)
  If Not $hPrinter Or Not BitAND($iProp, $PRINTER_AUTHORIZED_PROPERTIES) Or Not IsBool($bGlobal) Then Return SetError(1)

and eliminated (for test purposes) the testing of the $iValue and used

_PRNT_SetPrinterInfo($hPrinter, $DM_DUPLEX, "Installed", True)

but also with no positive result.

Link to comment
Share on other sites

I changed the script slightly:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <Printer_UDF.au3>

Opt("MustDeclareVars", True)

Example()

Func Example()
  Local $hPrinter = _PRNT_OpenPrinter("HP Color LaserJet CP202X PS Class Driver")
  Local $tDevMode = _PRNT_GetPrinterInfo($hPrinter, False)
  ConsoleWrite($tDevMode.Duplex & @CRLF)
  ConsoleWrite($tDevMode.FormName & @CRLF)
  ConsoleWrite(_PRNT_IsValidDevmode($tDevMode) & @CRLF)
  
  Local $aList = _PRNT_EnumJobs($hPrinter)
  ConsoleWrite(_ArrayToString($aList) & @CRLF)

  _PRNT_SetPrinterInfo($hPrinter, $DM_DUPLEX, 1, False)
  
  Local $tDevMode = _PRNT_GetPrinterInfo($hPrinter, True)
  ConsoleWrite($tDevMode.Duplex & @CRLF)
  ConsoleWrite($tDevMode.FormName & @CRLF)
  ConsoleWrite(_PRNT_IsValidDevmode($tDevMode) & @CRLF)
  
  Local $aList = _PRNT_EnumJobs($hPrinter)
  ConsoleWrite(_ArrayToString($aList) & @CRLF)

_PRNT_SetPrinterInfo($hPrinter, $DM_DUPLEX, 1, True)
  
  
  _PRNT_ClosePrinter($hPrinter)
EndFunc   ;==>Example

The console output is:

1
A4
1
0||||||||||
1
A4
1
0||||||||||
>Exit code: 0

image.png.7414af03188a50ef9b89ada94e095e13.png

Question:

Does "ConsoleWrite($tDevMode.Duplex & @CRLF)" only show, that the device has the option "Duplex", but not, whether it is enabled or not!?

 

Beside all this:

_PRNT_SetPrinterInfo($hPrinter, $DM_DUPLEX, "Installed", True)

does not bring any error (with the Original UDF), although $iValue is !!not!! and Integer! Where based on my knowledge it shoud bring an error:

If Not $hPrinter Or Not IsInt($iValue) Or Not BitAND($iProp, $PRINTER_AUTHORIZED_PROPERTIES) Or Not IsBool($bGlobal) Then Return SetError(1)

 

Edited by averlon
Link to comment
Share on other sites

There is 3 possible values for Duplex :

 

Value Meaning
DMDUP_SIMPLEX(1) Normal (nonduplex) printing.
DMDUP_HORIZONTAL(3) Short-edge binding, that is, the long edge of the page is horizontal.
DMDUP_VERTICAL(2) Long-edge binding, that is, the long edge of the page is vertical.
Link to comment
Share on other sites

unfortunately it still does not work!

Befor Script:

image.png.d46f46002fb5456816a0abb2919b4419.png

Script:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <Printer_UDF.au3>
#include <WinAPIGdi.au3>
#include <WinAPIDiag.au3>


Opt("MustDeclareVars", True)

Example()

Func Example()
  ConsoleWrite("--- START ---" & @CRLF)
  Local $hPrinter = _PRNT_OpenPrinter("HP Color LaserJet CP202X PS Class Driver")
  Local $tDevMode = _PRNT_GetPrinterInfo($hPrinter, True)
  ConsoleWrite("Orientation: " & $tDevMode.Orientation & @CRLF)
  ConsoleWrite("PaperSize: " & $tDevMode.PaperSize & @CRLF)
  ConsoleWrite("PaperLength: " & $tDevMode.PaperLength & @CRLF)
  ConsoleWrite("PaperWidth: " & $tDevMode.PaperWidth & @CRLF)
  ConsoleWrite("Scale: " & $tDevMode.Scale & @CRLF)
  ConsoleWrite("Copies: " & $tDevMode.Copies & @CRLF)
  ConsoleWrite("DefaultSource: " & $tDevMode.DefaultSource & @CRLF)
  ConsoleWrite("PaperWidth: " & $tDevMode.PaperWidth & @CRLF)
  ConsoleWrite("Color: " & $tDevMode.Color & @CRLF)
  ConsoleWrite("Duplex: " & $tDevMode.Duplex & @CRLF)
  ConsoleWrite("YRresolution: " & $tDevMode.YRresolution & @CRLF)
  ConsoleWrite("TTOption: " & $tDevMode.TTOption & @CRLF)
  ConsoleWrite("Collate: " & $tDevMode.Collate & @CRLF)
  ConsoleWrite("FormName: " & $tDevMode.FormName & @CRLF)
  ConsoleWrite("is valid DevMode: " & _PRNT_IsValidDevmode($tDevMode) & @CRLF)
  
  Local $aList = _PRNT_EnumJobs($hPrinter)
  ConsoleWrite("---aList: " & _ArrayToString($aList) & @CRLF)

  _PRNT_SetPrinterInfo($hPrinter, $DM_DUPLEX, 2, False)
  Local $tDevMode = _PRNT_GetPrinterInfo($hPrinter, False)
  ConsoleWrite("--- after SetPrinterInfo ---" & @CRLF)
  ConsoleWrite("Duplex: " & $tDevMode.Duplex & @CRLF)
  
  _PRNT_ClosePrinter($hPrinter)
EndFunc   ;==>Example

Console Print:

--- START ---
Orientation: 1
PaperSize: 9
PaperLength: 2970
PaperWidth: 2100
Scale: 100
Copies: 1
DefaultSource: 15
PaperWidth: 2100
Color: 2
Duplex: 1
YRresolution: 0
TTOption: 3
Collate: 1
FormName: A4
is valid DevMode: 1
---aList: 0||||||||||
--- after SetPrinterInfo ---
Duplex: 2
>Exit code: 0

Printer configuration after Script:

image.png.fbc521cf2701598ac5fe4d6b8cfb403f.png

I have crosschecked the configuration with some application where I wanted to print a page (Word) and open the configuration before printing. Duplx printing is not offered!

And by the way: I doubt, there are these options you mentioned. As far as I know, the type of duplex printing can be selected when printing.

But what I am looking for is "to enable" duplex printing.

So the configuration setting must tell me "Installed". At that configuration setting you don't have the option to select long- or short-page!

As I stated above, this PowerShell-Script does the trick - but I don't want to enable PowerShell on my device!

Set-PrinterProperty -PrinterName "HP Color LaserJet CP202X PS Class Driver" -PropertyName "Config:Duplexer" -Value "Installed"

I have the feeling, we are talking about different type of settings.

Edited by averlon
Link to comment
Share on other sites

19 minutes ago, averlon said:

I have the feeling, we are talking about different type of settings.

No doubt.  Spooler will not install your duplex unit.  Maybe minic your manual installation with UIAutomation.

Link to comment
Share on other sites

I found a solution. Not the very best, but it seems to work.

There is a Microsoft Tool where you can store the printers configuration in a .dat-file.

This is what you need to do only once, naturally when the printer settings are according to your needs - duplex: "Installed" - in my case.

rundll32.exe printui.dll,PrintUIEntry /Ss /n"<printer name>" /a"<some path to file>\printer_configuration.dat" d

You can do this via WINDOWS+R and past the command into the commandline field and ENTER.

Now, you have the printer configuration stored in the file specified!

Now you can create a autoit script and run the script at "autostart".

Run('rundll32.exe printui.dll,PrintUIEntry /Sr /n"<printer name>" /a"<some path to file>\printer_configuration.dat" d')

This will restore the configuration to the settings you stored with the commandline shown above.

For more information on the printerui see https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rundll32-printui#related-links

 

So far, I havn't found any way to modify one specific printer property. So the only way is to store and restore the whole configuration!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...