SOLVED: Unable to display MsgBox from Service

3 posts in this topic

#1 ·  Posted (edited)

I'm unable to display a message box from a compiled AutoIt alerting script that is executed from a service (also a compiled AutoIt script). I used $MB_SERVICE_NOTIFICATION, but the dialog doesn't appear and the alerting script continues as if the OK button had been clicked. The service script uses ShellExecute() to launch the alerter (as opposed to a *Wait() call) so it can continue processing. Note that we used Windows Service Wrapper (winsw) to turn the compiled script into a service but haven't identified any issues from it.

I tried the one-line execute example given in this thread: Message box timeout not working

$iPID = Run(@AutoItExe & ' /AutoIt3ExecuteLine  "MsgBox(2097152, ''' & $sTitle & ''', ''' & $sText & ''')"')

without the timeout code, but no luck: the MsgBox does not appear. (In any case, we don't have AutoIt installed on the target system, so it would have to be converted into a .exe file.)

We're developing and unit-testing on Win 7 Enterprise; the target OS is Win 7 Pro, and the AutoIt version is

Any solutions or suggestions will be much appreciated. Code fragments are below.



The following code fragment is the relevant portion of the alerting script that displays the MsgBox:


; Alert the operator that there's a problem with the recording
$sFeed = $aRecInfo[6]
$sSession = $aRecInfo[2]
$sTemp = $aRecInfo[4]
$sDate = _FormatDate($sTemp)
$sTemp = $aRecInfo[5]
$sTime = _FormatTime($sTemp)
_Debug2("Inactive recording feed " & $sFeed & ", Session=" & $sSession & ", Start Date/Time=" & _
                $sDate & " " & $sTime)
$sErrorMsg = "ERROR: Feed " & $sFeed & " for session " & $sSession & " stopped, notify reporter immediately"


_Debug1("Displaying MsgBox...")
_Debug1("Returned from MsgBox")



And the calling code fragment in the service is:

; Walk through the array backwards so we don't end up evaluating an index that doesn't exist
    For $iIndex = UBound($aFeedArray)-1 To 0 Step -1

            ; Before timing-out the feed, check for a .mpgpart file (=> feed may still be recording)
            $sDirPath = $sDirTemp & "\" & $sFeedTemp & "\" & $aFeedArray[$iIndex][$cSessionName]
            $sMpgPartName = GetMpgPart($sDirPath, $sFeedTemp)
            If StringLen($sMpgPartName) > 0 Then
                ; If .mpgpart file name hasn't changed in more than $iDeadFeedTime seconds, then declare feed dead
                ; ========v Test code to force error v========
                $sMpgPartName = $aFeedArray[$iIndex][$cMpgPartName]
                ; ========^ Test code to force error ^========
                _Debug2("Just set $sMpgPartName to '" & $sMpgPartName & "', should fall into dead-feed code")
                If $sMpgPartName = $aFeedArray[$iIndex][$cMpgPartName] Then
                    ; Name is same => feed is dead: alert the operator and delete the feed w/out stop-processing
                    _Debug2("Feed " & $sFeedTemp & " looks dead -- alerting the operator")
                    _Debug2("Delete GUID " & $aFeedArray[$iIndex][$cGUID])

                    ; ======== Alert app execution ========
                    ; $sAlertApp = @ScriptDir & "\" & "RecAlert.exe"
                    $iChildPid = ShellExecute($sAlertApp, $sDirPath, "", "open")
                    _Debug2("Alert app: ShellExecute(): " & _RetStr($iChildPid, @error, @extended))
                    _ArrayDelete($aFeedArray, $iIndex)                  
                    ; Otherwise, the .mpgpart name has changed, reset the timer, store the name, and continue
                    _Debug2("Feed " & $sFeedTemp & " timeout, but has new .mpgpart file -- continuing")
                    $aFeedArray[$iIndex][$cDateTime] = TimerInit()
                    $aFeedArray[$iIndex][$cMpgPartName] = $sMpgPartName



Edited by tremolux66
Corrected a typo that crept into the code

Share this post

Link to post
Share on other sites

#2 ·  Posted (edited)

Hi @tremolux66

Googled this question gave some pretty good results.

It seems like Windows Vista and above user interfaces generated by a service can't be seen.

There's some discussion in the links below, that may help you:

Show a message box from a Windows Service

Do you still use the MessageBox API in your Windows Service?

How to show message box in windows service

Edited by genius257
spell checking

Share this post

Link to post
Share on other sites

#3 ·  Posted (edited)

@genius257 Thanks for the pointers. I had assumed it was an AutoIt coding issue so I went straight to the forum, but clearly it's not. Perhaps the MsgBox() Help page could be updated to clarify this.

Since our system only has one interactive user logged-in at a time, I can probably just modify the service & rewrite the alerter script so they use a named pipe.

Thanks again.

Edited by tremolux66
Fixed a typo.

Share this post

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

  • Similar Content

    • ZeroByDevide
      By ZeroByDevide
      i have this code running but it just would not start the code:
      Local $rndSleep = Int (Random(180000,240000,1000))
      MsgBox($MB_SYSTEMMODAL, "NaaaNuuu", "This message box will show the sleeptime after closing the tabs, you got " & $rndSleep & " seconds left.", $rndSleep)
      here is the error it shows me:
      "C:\Users\numan\Desktop\scipiie.au3" (23) : ==> Variable used without being declared.:
      MsgBox($MB_SYSTEMMODAL, "NaaaNuuu", "This message box will show the sleeptime after closing the tabs, you got " & $rndSleep & " seconds left.", $rndSleep)
      MsgBox(^ ERROR
    • lupusbalo
      MsgBox do not pause script any more
      By lupusbalo
      I wrote AutoIt programs for (too!!) many years!
      I just update to W10 then install a new machine.
      Want to write a script and found that whatever flag values MsgBox won't pause the script ( just display for 2 ~ 3 seconds)
      The same occurs with _ArrayDisplay
      Thanks for Any Clue
    • PINTO1927
      MsgBox if the txt file is changed
      By PINTO1927
      Hi guys,
      through a script, a button refers me content in a txt file. You can have a MsgBox if this file is modified the content?
    • caramen
      MsgBox 4 return value ?
      By caramen
      Hello guy got some trouble in this easy script
      Case $BoutonWhrite1 $Case1 = MsgBox (4,"Are you sure ?" ,"Reg key gonna be changed." ) While 1 If $Case1 = "No" Then ConsoleWrite(">Case -1 Started" & @CRLF) ExitLoop EndIf $InputType = InputBox ("Value Type ?", 'Type of key to write: "REG_SZ", "REG_MULTI_SZ", "REG_EXPAND_SZ", "REG_DWORD", "REG_QWORD", or "REG_BINARY".' ) ;~ RegWrite (""&Reg1,""&$RegName1,""&$InputType, ""&$RegValue1) ExitLoop WEnd I tryed
      If $case1 = 1
      If $case1 = -1
      If $case1 = "No"
      What is the returned value by the msg box YES or NO ?
      The script is going wrong about the final purpose (Whrite a Registry key if the Script user unswer YES ) But dont take attention i am gonna corect after get the returned value xD it was for testing. 
    • pranaynanda
      Msgbox not Showing when GUI Runs
      By pranaynanda
      I'm trying to run this code:
      #include <GuiListView.au3> #include <GUIConstants.au3> Dim $Services Dim $ServicesList #cs While 1 CheckService() Sleep(30000) ; sleep 30 seconds WEnd #ce ;#cs #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <ListViewConstants.au3> #include <TabConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 615, 438, 192, 124) $Tab1 = GUICtrlCreateTab(0, 48, 609, 385) $TabSheet1 = GUICtrlCreateTabItem("Running Services") $ListView1 = GUICtrlCreateListView("Service Name|Status", 8, 72, 593, 281, -1, BitOR($LVS_EX_GRIDLINES,$LVS_EX_CHECKBOXES,$LVS_EX_FULLROWSELECT)) GUICtrlSendMsg($ListView1, $LVM_SETCOLUMNWIDTH, 0, 300) GUICtrlSendMsg($ListView1, $LVM_SETCOLUMNWIDTH, 1, 288) $Button1 = GUICtrlCreateButton("Stop Services", 464, 376, 129, 33) $TabSheet2 = GUICtrlCreateTabItem("Stopped Services") GUICtrlSetState(-1,$GUI_SHOW) $ListView2 = GUICtrlCreateListView("Service Name|Status", 8, 72, 593, 281, -1, BitOR($LVS_EX_GRIDLINES,$LVS_EX_CHECKBOXES,$LVS_EX_FULLROWSELECT)) GUICtrlSendMsg($ListView2, $LVM_SETCOLUMNWIDTH, 0, 300) GUICtrlSendMsg($ListView2, $LVM_SETCOLUMNWIDTH, 1, 288) $Button2 = GUICtrlCreateButton("Start Services", 464, 376, 129, 33) GUICtrlCreateTabItem("") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd ;#ce ;$Tab1 = GUICtrlCreateTab(16, 8, 601, 377) ;$TabSheet1 = GUICtrlCreateTabItem("Running Services") ;$ListView1 = GUICtrlCreateListView("Service Name", 24, 40, 582, 334) ;_GUICtrlListView_SetExtendedListViewStyle($ListView1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES)) ;$ServiceName = "wuauserv" Local $Services = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2") Local $ServicesList = $Services.ExecQuery("SELECT * FROM Win32_Service") If IsObj($ServicesList) then For $Services in $ServicesList ;If $Services.Name = $ServiceName Then ; if $Services.State = "Running" Then MsgBox(8192,"Hello", $Services.Name & $Services.State,0,$Form1) ;GUICtrlCreateListViewItem( $Services.Name & "|" & $Services.State , $ListView1) ;Run (@ComSpec & " /c " & 'net stop wuauserv') ; EndIf ;EndIf Next EndIf ;EndFunc But the msgbox does turn up when GUI runs. However, if I comment the GUI section, it works perfectly fine. Please help.