Jump to content

Recommended Posts

Runwait('rundll32 printui.dll PrintUIEntry /k /n "MFP"',"",@SW_HIDE)  ; MFP is the printer name

This is even better  a few line of code,  list all the information I needed on the test page

Printer name , computer name , time and date , IP address.  by the way I tested your print function in your udf and did it not work for me


Edited by antonioj84
Link to post
Share on other sites
  • 1 month later...
  • Replies 111
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Hello. I did create these few functions several months ago. I post here, if it can interest someone. These functions based on WMI queries allow you to manage printers : add / delete printer, drive

There was an error in the WMI query, fixed now (please download the updated file) Yes, you have to use the share name for remote shared printers (I added a remarks in the function header)

I got it to work, but you have to use the FQDN of the print server i.e \\Servername.domain.com\Printer name.

Posted Images

This UDF cannot export or import printer setting

please write more funtion to export or import printer setting

this is my cmd script to import printer setting

use cmd to restore local printer is ok

rundll32 printui.dll,PrintUIEntry /Sr /n "shareprint" /a "config.dat" u

but not ok if printer is network printer (share)

rundll32 printui.dll,PrintUIEntry /Sr /n "\\\shareprint" /a "config.dat" u


Link to post
Share on other sites
  • 1 month later...

Hello, @jguinch, hope you're good :)

Could you help me debug the reason my script is unable to add printers using the function "_PrintMgr_AddWindowsPrinterConnection"?

If here isn't the right place, please let me know.

I used another script I found here that uses your UDF, this topic below, by @willichan.

So, I have a server callled beniv001, and I'm at another server with a test user logged in, 3 printers from beniv001 are installed as seen below:


So, I ran the script, replacing "beniv001" to "PRINT_NIV", which is the new server.

I put a msgbox right before and after the function to figure out what it's doing and if its giving an error, (and it is). 

It's doing this:


it just says "error" :


Here's the code excerpt, and there is nothing else changed from the original script.

For $iLoop = 1 To $aReplaced[0]
    Local $isdefault = False
    If $aReplaced[$iLoop] = $sDefaultPrinter Then $isdefault = True
;    ConsoleWrite("Adding \\" & $sNewPrintServer & "\" & $aReplaced[$iLoop] & "(" & $isdefault & ")" & @CRLF)
     MsgBox(64,"WEG","Adding \\" & $sNewPrintServer & "\" & $aReplaced[$iLoop])
    _PrintMgr_AddWindowsPrinterConnection("\\" & $sNewPrintServer & "\" & $aReplaced[$iLoop])
    if @error then MsgBox(0,"error","error")
    If $isdefault Then _PrintMgr_SetDefaultPrinter("\\" & $sNewPrintServer & "\" & $aReplaced[$iLoop])

But I have no idea to debug what's going on. Could you help me?


Link to post
Share on other sites

@DavidLago : can you try with this one ?

Func _AddPrinterConnection($sPrinterName)
    Local $aRet = DllCall("Winspool.drv", "bool", "AddPrinterConnectionW", "wstr", $sPrinterName)
    If @error Or Not $aRet[0] Then Return SetError(1, 0, 0)
    Return 1
EndFunc ; ===> _AddPrinterConnection


Link to post
Share on other sites
  • 3 months later...

@jguinch Excellent UDF, I was using PrintUi previously but prefer using this method as it can be run silently.  Just a couple of things I found which I thought I'd mention is that when using the functions with network printers, found I needed to either double up the backslashes for server/print paths, otherwise functions like _Printmgr_PrinterExists wouldn't work, for example:

$bPrinterExists = _Printmgr_PrinterExists("\\server.fqdn.com\PrinterName") ;~ Doesn't work
$bPrinterExists = _Printmgr_PrinterExists("\\\\server.fqdn.com\\PrinterName") ;~ Works
$bPrinterExists = _Printmgr_PrinterExists("%server.fqdn.com%PrinterName") ;~ Also works

So I ended up putting in a StringReplace($sPrinterName, "\", "\\") within the function, although actually thinking of using "%" aka StringReplace($sPrinterName, "\", "%")

Also noticed a number of the functions are missing the appending "%" and "%" suffix after the "Like" operator, not sure if that was intended or not, but thought I'd mention it, example below.

Thanks again for the UDF though very helpful, just used it along with @Melba23 Toast to migrate staff printers from one Server with 100+ printers to new Windows Server 2016 print server.

Local $oPrinters = $oWMIService.ExecQuery ("Select * from Win32_Printer Where DeviceID like '" & $sPrinterName & "'", "WQL", $wbemFlagReturnImmediately


Link to post
Share on other sites
  • 5 months later...

If you have appropriate rights to access the machine remotely and WMI is enabled on it, you could use retrieve the OS architecture from WMI:

Example using CMD line:

wmic.exe /node:%computername% os get osarchitecture

Example WMI COM Object:

#include <Array.au3>

Local $oWMIService = _WMIService()  ;Instantiate WMIService Object - Establish Connection
If $oWMIService = 0 Then Exit   ;If WMI Object failed to instantiate, Abort Script

Local $sWMIQueryClass = "Win32_OperatingSystem"  ;Declare WMI Class to Query
Local $aWMIQueryFields[]= ["OSArchitecture"]  ;Declare Fields to Retrieve From WMI Class

$aResults = _WMIQuery($oWMIService,$sWMIQueryClass,$aWMIQueryFields)  ;Run WMI Query against WMIService Object
$oWMIService = 0  ;Termninate WMIService Object

_ArrayDisplay($aResults)    ;Display Results Array

Func _WMIService($sHost = @ComputerName) ;Connect to WMI Service
    $oWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sHost & "\root\cimv2")  ;Create WMI Service Object
    If Not IsObj($oWMIService) Then Return SetError(1,0,0)  ;If Obj Instantiation Failed, Error Out
    Return $oWMIService ;Return WMI Service Object

Func _WMIQuery($oWMIService,$sWMIQueryClass,$aWMIQueryFields) ;Perform WMI Query with Query String and Data Return Parameters
    If Not IsObj($oWMIService) Then Return SetError(1,0,0)  ;If oWMIService is not a valid Object, Error Out
    If Not IsArray($aWMIQueryFields) Then Return SetError(2,0,0)    ;If Query Fields is not a valid array, Error Out

    Local $aResults = $aWMIQueryFields  ;Create Results Array
    _ArrayColInsert($aResults, 1)   ;Add Dimension to Results Array to Hold Query Return Values

    Local $sWMIQuery = "SELECT " & _ArrayToString($aWMIQueryFields,",") & " FROM " & $sWMIQueryClass    ;Create WMI Query String
    $oItems = $oWMIService.ExecQuery ($sWMIQuery)  ;Execute query using WMI Service Object

    For $i = 0 to UBound($aWMIQueryFields) - 1  ;Loop through WMI Query Results
        For $oItem In $oItems
            $aResults[$i][1] = Execute("$oItem." & $aWMIQueryFields[$i])  ;Result

    Return $aResults  ;Return Result Array


Link to post
Share on other sites
  • 3 weeks later...

Help me !

It not working. I try using PrintMgr.au3 for add driver for Canon LBP 2900 in window 8 64bit

#Include "PrintMgr.au3"

Local $sDriverName = "Canon LBP2900"
$oTest = _PrintMgr_AddPrinterDriver($sDriverName, "Windows NT x64", @ScriptDir & "\LBP2900_R150_V330_W64_uk_EN_2\x64\Driver\", @ScriptDir & "\LBP2900_R150_V330_W64_uk_EN_2\x64\Driver\CNAB4STD.INF")


Link to post
Share on other sites

What is the returned value ? (what is the output of ConsoleWrite) ?

Are you sure you have enough rights to add a printer driver ? (use #RequireAdmin at the top of your script to check it)



Edited by jguinch
Link to post
Share on other sites
  • 1 year later...
  • 1 month later...

@jguinch, thanks for writing up this UDF. I'm trying to use the _PrintMgr_EnumPrinters and _PrintMgr_SetDefaultPrinter. No question on how to use these two. Just that when I do the syntax checker, it complains about these functions, which I can safely ignore because I don't use them (yet) but others might:

"C:\Program Files (x86)\AutoIt3\Include\PrintMgr.au3"(173,26) : warning: $ret possibly not declared/created yet
    $ret = $oNewPort.Put_
"C:\Program Files (x86)\AutoIt3\Include\PrintMgr.au3"(252,87) : warning: $tPortName possibly not declared/created yet
        $tPortName = DllStructCreate("wchar Data[512]", DllStructGetData($t_PORT_INFO_2, 1))
"C:\Program Files (x86)\AutoIt3\Include\PrintMgr.au3"(253,90) : warning: $tMonitorName possibly not declared/created yet
        $tMonitorName = DllStructCreate("wchar Data[256]", DllStructGetData($t_PORT_INFO_2, 2))
"C:\Program Files (x86)\AutoIt3\Include\PrintMgr.au3"(254,87) : warning: $tPortDesc possibly not declared/created yet
        $tPortDesc = DllStructCreate("wchar Data[256]", DllStructGetData($t_PORT_INFO_2, 3))
"C:\Program Files (x86)\AutoIt3\Include\PrintMgr.au3"(834,58) : warning: $iRet possibly not declared/created yet
        $iRet = $oPrinter.RenamePrinter($sPrinterNewName)

I tried to look up for a newer one that may have fixed the above warnings but there's nothing newer.


John Babbitt

Link to post
Share on other sites
  • 5 weeks later...

Hello jguinch,
I'm trying to use AddPrinter but I got an error when I execute the following lines, even one line at a time :

#Include "PrintMgr.au3"

ConsoleWrite(_PrintMgr_AddPrinter("PDF", "PDFCreator", "pdfcmon_2"))
ConsoleWrite(_PrintMgr_AddPrinter("nomimprimante", "Kyocera TASKalfa 4551 ci KX", "I-LP-10"))

The message is :
Line 41 (File "C:\Users\...\Test.exe"):
Error: The requested action with this object has failed.

I did not use AddDriver, the drivers were installed previously.
I used _PrintMgr_EnumPrinterDriver() to identify the driver name.

I'm using win 10 pro 64bits...



Link to post
Share on other sites

If I try with F5 on Scite I see :
"C:\Users\samuel.ruet\Desktop\installation auto imprimantes\PrintMgr.au3" (117) : ==> The requested action with this object has failed.:
$oPrinter^ ERROR
->11:17:36 AutoIt3.exe ended.rc:1
+>11:17:36 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 0.5531

In the code no value is assigned to $oPrinter.Put_, is this normal?

Func _PrintMgr_AddPrinter($sPrinterName, $sDriverName, $sPortName, $sLocation = '', $sComment = '')
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    If NOT IsObj($oWMIService) Then Return SetError(1, 0, 0)
    Local $oPrinter = $oWMIService.Get("Win32_Printer").SpawnInstance_
    If NOT IsObj($oPrinter) Then Return SetError(1, 0, 0)
    $oPrinter.DriverName = $sDriverName
    $oPrinter.PortName   = $sPortName
    $oPrinter.DeviceID   = $sPrinterName
    $oPrinter.Location   = $sLocation
    $oPrinter.Comment    = $sComment
    Return _Printmgr_PrinterExists($sPrinterName)
EndFunc ; ==> _PrintMgr_AddPrinter


Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By hek
      Hey everyone,
      Was wondering how I would be able to implement this on a local computer instead of using connectserver? 
      Any suggestions or help would be appreciated. Thanks. 
    • By DirtyJohny
      Hi everyone.Need rewrite this function how in еxample.
      #RequireAdmin #NoTrayIcon Opt("MustDeclareVars",1) Func _a() Local $sls=ObjGet("winmgmts:{impersonationLevel=impersonate," _ &"authenticationLevel=Pkt}!\\"& _ @ComputerName&'\root\wmi'),$lss=$sls.ExecQuery _ ('SELECT * FROM WmiMonitorID'), _ $lll,$sll,$sss="",$lsl,$lls,$i,$z For $z In $lss $lsl=$z.UserFriendlyName For $i=0 To Ubound($lsl)-1 if ($lsl[$i]) Then $lll&=Chr($lsl[$i]) Next $lls=$z.SerialNumberID For $i=0 To Ubound($lls)-1 if ($lls[$i]) Then $sll&=Chr($lls[$i]) Next $sss&="Model"&@TAB&@TAB&": "&$lll&@CR&"Serial Number"&@TAB&": "&$sll&@CR&@CR $lll="" $sll="" Next MsgBox(262144,'Monitors '&$lss.Count,$sss&" "&@CR) $lss=Null $sls=Null EndFunc _a() Example:
      Func _InfoPC() Local $ObjService = ObjGet('winmgmts:{impersonationLevel = impersonate}!\\' & @ComputerName & '\root\cimv2') Local $ObjMB = $ObjService.ExecQuery('SELECT * FROM Win32_BaseBoard', 'WQL', 0x30) If IsObj($ObjService) Then For $objItemMB In $ObjMB $sInfo &= @TAB & 'Motherboard: ...... ' & $objItemMB.Product & @CRLF I'm beginner in this sphere and need  you all speak easy and simply because i'm Russian.Thanks)
    • By shital
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** RunAs(test", @ComputerName, "testinng",2,"wmic product where ""name= '%notepadexamples%'"" call uninstall", @SystemDir & "\wbem", @SW_MAXIMIZE)  it not working 
    • By Colduction
      Hi AutoIt Programmers!

      I've seen a code that gives Name of Group by writing it's group's SID, but this one response very slower than Windows Command-Line WMIC
      Func _GetLocalGroupName($sSID = 'S-1-5-18') $objWMIService = ObjGet ("winmgmts:\\" & @ComputerName & "\root\cimv2") $colItems = $objWMIService.ExecQuery('SELECT Name FROM Win32_Group where SID="' & $sSID & '"') For $GroupNames in $colItems MsgBox (0,"",$GroupNames.Name) ExitLoop Next EndFunc I don't want call and use any third-party programs even CMD, i just want use from Windows API, netapi32.dll or AutoIt Functions (Standalone).
      Do you have any idea to improve speed/performance of this code? I'll happy of your comments
      ====================== SOLOUTION by @Subz ======================
    • By jresine
      Hello, is it possible to know via a script or command, to have the percentage of disk usage of a process? thank you in advance.
      ps: see image

  • Create New...