Witto Posted November 10, 2010 Share Posted November 10, 2010 (edited) Dear all,I found a script to add a certificate to Windows with CertMgr.exe.http://www.msfn.org/board/topic/29123-trusted-publishers-certificates/I adapted it to my needs.It works with AutoIt3.exe v3.2.2.0.It does not work with AutoIt3.exe v3.3.6.1, the latest version.The "Security Warning" window appears, but the WinWait() function does not do it's job when I use v3.3.6.1.Does somebody know why?; AutoIT Parameters #NoTrayIcon AutoItSetOption("MustDeclareVars", 1) AutoItSetOption("RunErrorsFatal", 0) ; Operational Constants const $WinTitle = "Security Warning" const $WinText = "You are about to install a certificate" const $WinCtrl = 6 const $FileName = '"' & @ScriptDir & '\MyCert.cer"' ; Variables Dim $r Dim $e ; Program Function $e = 0 Run(@ScriptDir & "\CertMgr.exe -add -c " & $FileName & " -s ROOT") If @error = 0 Then $r = WinWait($WinTitle, $WinText, 30) If $r = 1 Then WinActivate($WinTitle, $WinText) ControlClick($WinTitle, $WinText, $WinCtrl, "left", 1) Else $e = 1 EndIf Else $e = 1 EndIf Exit($e) Edited November 11, 2010 by Witto Link to comment Share on other sites More sharing options...
Richard Robertson Posted November 10, 2010 Share Posted November 10, 2010 Does certmgr.exe run with administrative privileges? If so, your script also needs to run as admin to interact with the dialog. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 10, 2010 Moderators Share Posted November 10, 2010 Witto,WinWait works perfectly, but you are looking for the wrong return value. From the History in the Help file:18th December, 2009 - v3.3.2.0Added #764: Return Pid on ProcessWait() and handle on WinWait(), WinWaitActive, WinActivate(), WinActive(), WinMove() when successful. So your $r = 1 condition will never be met. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Witto Posted November 10, 2010 Author Share Posted November 10, 2010 Dear Richard, CertMgr.exe is launched by the script. Administrative privileges are not needed. The script can be run by an ordinary user. Link to comment Share on other sites More sharing options...
Witto Posted November 10, 2010 Author Share Posted November 10, 2010 (edited) Dear Melba23,It is not clear to me what I should be looking for. What should I do with the if-statement?If $r = 1 Then Edited November 11, 2010 by Witto Link to comment Share on other sites More sharing options...
Developers Jos Posted November 10, 2010 Developers Share Posted November 10, 2010 (edited) WinWait() retuns the window handle when successfull, not a 1. So something like this should work: $r = WinWait($WinTitle, $WinText, 30) If $r <> 0 Then WinActivate($WinTitle, $WinText) ControlClick($WinTitle, $WinText, $WinCtrl, "left", 1) Else $e = 1 EndIf Edited November 10, 2010 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 10, 2010 Moderators Share Posted November 10, 2010 Witto,From the Help file for WinWait:Return ValueSuccess: Returns handle to the requested window. Failure: Returns 0 if timeout occurred. In your script WinWait returns it when it has found the window (in which case $r is the handle) or if it timed out ($r = 0).So just check for $r <> 0. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Witto Posted November 10, 2010 Author Share Posted November 10, 2010 (edited) Dear Jos,Dear Melba23,Thanks for the reply. That makes the script working again. But I presume I can do smarter things with a return value like "0x000B0D78"?; AutoIT Parameters #NoTrayIcon AutoItSetOption("MustDeclareVars", 1) AutoItSetOption("RunErrorsFatal", 0) ; Operational Constants const $WinTitle = "Security Warning" const $WinText = "You are about to install a certificate" const $WinCtrl = 6 const $FileName = '"' & @ScriptDir & '\MyCert.cer"' ; Variables Dim $r Dim $e ; Program Function $e = 0 Run(@ScriptDir & "\CertMgr.exe -add -c " & $FileName & " -s ROOT") If @error = 0 Then $r = WinWait($WinTitle, $WinText, 30) If $r <> 0 Then ; MsgBox(1,"Return Value", $r, 30) WinActivate($WinTitle, $WinText) ControlClick($WinTitle, $WinText, $WinCtrl, "left", 1) Else $e = 1 EndIf Else $e = 1 EndIf Exit($e) Edited November 11, 2010 by Witto Link to comment Share on other sites More sharing options...
Richard Robertson Posted November 10, 2010 Share Posted November 10, 2010 Well that return value can be used for any functions that need a window handle. For example, you could instead do WinActivate($r) ControlClick($r, "", $WinCtrl, "left", 1) Link to comment Share on other sites More sharing options...
Witto Posted November 10, 2010 Author Share Posted November 10, 2010 (edited) Dear Richard, Thanks for your reply. I have installed the latest "AutoIt Full Installation v3.3.6.1" Thanks for your code. It seems to work. But I did not find it in the help. Function Reference WinWait -------------------------------------------------------------------------------- Pauses execution of the script until the requested window exists. WinWait ( "title" [, "text" [, timeout]] ) Parameters title The title of the window to check. See Title special definition. text [optional] The text of the window to check. timeout [optional] Timeout in seconds Return Value Success: Returns handle to the requested window. Failure: Returns 0 if timeout occurred. Remarks None. Related WinActive, WinExists, WinWaitActive, WinWaitClose, WinWaitNotActive, WinWaitDelay (Option), ProcessWait Example ;Wait for the window "[CLASS:Notepad]" to exist Run("notepad") WinWait("[CLASS:Notepad]") ;Wait a maximum of 5 seconds for "[CLASS:Notepad]" to exist WinWait("[CLASS:Notepad]", "", 5) I have another question: This works: ControlClick($r, "", $WinCtrl, "left", 1) This does not work: ControlClick($r, "", $WinCtrl, "right", 1) Edited November 11, 2010 by Witto Link to comment Share on other sites More sharing options...
MvGulik Posted November 11, 2010 Share Posted November 11, 2010 (edited) A control will ignore* click-types it don't knows. *(will seems to ignore them, as it just don't knows what to do with them.) For example. Try closing this messagebox with a right-mouse-click. You can't, and ControleClick can't either. MsgBox(0, "MsgBox", "MsgBox") Edited November 11, 2010 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
Witto Posted November 11, 2010 Author Share Posted November 11, 2010 (edited) A control will ignore* click-types it don't knows. *(will seems to ignore them, as it just don't knows what to do with them.) For example. Try closing this messagebox with a right-mouse-click. You can't, and ControleClick can't either. MsgBox(0, "MsgBox", "MsgBox") I see, it is the "ControlID" that defines which control button has to be clicked. "Button" is the mouse button. In my example, this would make me click "No": const $WinCtrl = 7 Edited November 11, 2010 by Witto Link to comment Share on other sites More sharing options...
MvGulik Posted November 11, 2010 Share Posted November 11, 2010 it is the "ControlID" that defines which control button has to be clicked. correct. "Button" is the mouse button. for the 'left', 'right' and 'middle', yes. In my example, this would make me click "No":const $WinCtrl = 7 If its a [ (yes) (no) ] gui message box. Probably. (But I don't run system related executables I don't know. So I have not seen the Gui your clicking on.) You can however use the Au3Info tools to look a the buttons properties to see what will target it. (see: http://www.autoitscript.com/autoit3/docs/intro/controls.htm for additional information about control targeting.) "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
Witto Posted November 11, 2010 Author Share Posted November 11, 2010 (edited) Thanks MvGulik,If I start CertMgr.exe to install my certificate, it could be that it has been installed before.In that case, I get a pop-up with a question if I want to delete the existing certificate. I want to answer "Yes" to that question. How could I see if the alternate window is opened first?expandcollapse popup; AutoIT Parameters #NoTrayIcon AutoItSetOption("MustDeclareVars", 1) AutoItSetOption("RunErrorsFatal", 0) ; Operational Constants const $WinTitle = "Security Warning" const $WinText = "You are about to install a certificate" const $WinCtrlYes = 6 ; const $WinCtrlNo = 7 const $FileName = '"' & @ScriptDir & '\MyCert.cer"' const $MouseBtnMain = "primary" ; const $MouseBtnMenu = "secondary" ; const $MouseBtnMiddle = "middle" ; Variables Dim $handle Dim $error ; Program Function $error = 0 Run(@ScriptDir & "\CertMgr.exe -add -c " & $FileName & " -s ROOT") ; These are the text and the title of the other window that could appear first. ; How can I see which window is opened first? ; Dim $Title1 = "Root Certificate Store" ; Dim $Text1 = "Do you want to DELETE the following certificate from the Root Store?" If @error = 0 Then $handle = WinWait($WinTitle, $WinText, 30) If $handle <> 0 Then ; check if older version of AutoIt3 is used If $Handle = 1 then WinActivate($WinTitle, $WinText) ControlClick($WinTitle, $WinText, $WinCtrlYes, "left", 1) Else WinActivate($handle) ControlClick($handle, "", $WinCtrlYes, $MouseBtnMain, 1) EndIf Else $error = 1 EndIf Else $error = 1 EndIf Exit($error) Edited November 11, 2010 by Witto Link to comment Share on other sites More sharing options...
Witto Posted November 12, 2010 Author Share Posted November 12, 2010 (edited) I think I found a way using a loopexpandcollapse popup; AutoIT Parameters #NoTrayIcon AutoItSetOption("MustDeclareVars", 1) ; This option is not supported anymore since v3.2.12.0 ; AutoItSetOption("RunErrorsFatal", 0) ; Operational Constants Const $WinCtrlYes = 6 ; Const $WinCtrlNo = 7 Const $FileName = '"' & @ScriptDir & '\MyCert.cer"' Const $MouseBtnMain = "primary" ; Const $MouseBtnMenu = "secondary" ; Const $MouseBtnMiddle = "middle" ; This box appears when trying to add the certificate Const $WinTitle0 = "Security Warning" Const $WinText0 = "You are about to install a certificate" ; This box appears if the certificate has been installed before Const $WinTitle1 = "Root Certificate Store" Const $WinText1 = "Do you want to DELETE the following certificate from the Root Store?" ; Variables Dim $handle Dim $error Dim $Success = 0 Dim $Counter = 0 ; Program Function Run(@ScriptDir & "\CertMgr.exe -add -c " & $FileName & " -s ROOT") If @error = 0 Then Do ; Check if Certificate has been installed before $handle = WinWait($WinTitle1, $WinText1, 1) If $handle <> 0 Then If $Handle = 1 Then ; support for versions of AutoIt older then 3.3.2.0 WinActivate($WinTitle1, $WinText1) ControlClick($WinTitle1, $WinText1, $WinCtrlYes, "left", 1) Else ; code for AutoIt v3.3.2.0 or more recent WinActivate($handle) ControlClick($handle, "", $WinCtrlYes, $MouseBtnMain, 1) EndIf EndIf ; Check if pop-up appears to install the certificate $handle = WinWait($WinTitle0, $WinText0, 1) If $handle <> 0 Then If $Handle = 1 then WinActivate($WinTitle0, $WinText0) ControlClick($WinTitle0, $WinText0, $WinCtrlYes, "left", 1) Else WinActivate($handle) ControlClick($handle, "", $WinCtrlYes, $MouseBtnMain, 1) EndIf $Success = 1 EndIf $Counter = $Counter + 1 Until $Success = 1 or $Counter = 30 if $Counter = 30 Then $Error = 1 exit $Error EndIf Else $Error = 1 Exit $Error EndIf Edited November 12, 2010 by Witto Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now