Jump to content

BlockInput Not working when using as Windos Startup Script


Recommended Posts

Cannot seem to find the answer to this anywhere.  This is a special case software deployment so we are adding the compiled script to the Windows 7 startup folder and when its done running it calls a file to delete itself.  Everything works great except BlockInput will not work when it runs as a windows login script.  If I go in and manually run the script BlockInput works just fine but it simply will not take as a login script. 

We need to block users from opening any office applications for the 30-40 seconds the entire script runs or package will fail so any other ideas are welcomed.

Here is the first part I have pulled for testing, the sleep parameter is just for testing purposes.

#RequireAdmin
#include <AutoItConstants.au3>
#include <Date.au3>
#include <MsgBoxConstants.au3>

block()

Func block()
    BlockInput(1)
    Sleep(5000)
    checknetwork()
EndFunc   ;==>block

Func checknetwork()
    $var = Ping("server")
    If Not @error Then
        BlockInput(0)
        MsgBox(0,"avail", "SERVER IS AVAIL")
        ;verify_8()
    Else
        ;MsgBox for testing purposes only
        BlockInput(0)
        MsgBox(0, "Error!", "SERVER Is Not Accessible!", 10)
        Exit
    EndIf
EndFunc   ;==>checknetwork

 

Link to comment
Share on other sites

Under what directory did you put your script?

There's the local, for the current user

Quote

C:\Users\Me\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

And then there's the startup for all users

Quote

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

It could just be a permissions issue and putting it in the other directory might make it work.

Link to comment
Share on other sites

Quote

Under what directory did you put your script?

That is a great question.  Before posting I tried both but current user and the all users folder.  It really needs to be in the all users folder for this to work easily as far as deployment goes but at this point neither current user or all users works. 

Link to comment
Share on other sites

Change 

Func block()
    BlockInput(1)
    Sleep(5000)
    checknetwork()
EndFunc   ;==>block

to 

Func block()
    Do
        $InputBlockedState = BlockInput(1)
        Sleep(1000)
    Until $InputBlockedState = 1
    Sleep(5000)
    checknetwork()
EndFunc   ;==>block

This will keep trying to BlockInput() until it returns success.

 

 

Link to comment
Share on other sites

On 3/1/2016 at 7:30 PM, BetaLeaf said:
Func block()
    Do
        $InputBlockedState = BlockInput(1)
        Sleep(1000)
    Until $InputBlockedState = 1
    Sleep(5000)
    checknetwork()
EndFunc   ;==>block

With the above nothing happens, the script hangs presumably since blocked state never equals 1.

 

On 3/1/2016 at 11:20 PM, AdamUL said:

It looks like when running the script, it is not fully elevated, which is what is needed for BlockInput.  Test using the IsAdmin function to see if the script is elevated.

Thank you for this tip, it is not running as admin.

Given this information it looks like running a login script as an admin may not be possible in the startup folder.  I have even tried requireAdministrator as the execution level when compiling and nothing changes.  Looks like I may have to use the task scheduler to run at startup with admin privileges to achieve this.

Interestingly enough when I create a startup script that calls another script the second script will run as admin, just not the first. 

Edited by ekim415
Link to comment
Share on other sites

This may or may not be of help for running self scripts as an admin user:

Global $sAdminUser = "user"
Global $sAdminPassword = "password"
Global $sDomain = @ComputerName
Global $iLogOnFlag = 0


If @UserName <> $sAdminUser And Not IsAdmin() Then
    $sParameters = ""
    If Not @Compiled Then
        $sParameters = ' "' & @ScriptFullPath & '"'
    EndIf

    If RunAs($sAdminUser, $sDomain, $sAdminPassword, $iLogOnFlag, @AutoItExe & $sParameters & " " & $CmdLineRaw) Then
        Exit
    Else
        Exit MsgBox(16 + 262144, "ERROR!", "Unable to run under administrator account.")
    EndIf
EndIf

;Run with Admin Token in Windows Vista and Higher.
If @UserName = $sAdminUser And Not IsAdmin() And Not StringRegExp(@OSVersion, "_(XP|200(0|3))") Then
    $sParameters = ""
    If Not @Compiled Then
        $sParameters = '"' & @ScriptFullPath & '"'
    EndIf

    If ShellExecute(@AutoItExe, $sParameters & " " & $CmdLineRaw, "", "runas") Then
        Exit
    Else
        Exit MsgBox(16 + 262144, "ERROR!", "Unable to elevate to Admin due to UAC.")
   EndIf
EndIf

 

Link to comment
Share on other sites

Try this, no elevated privileges needed

#include <GUIConstants.au3>
#include <WinAPI.au3>

Global const $SLEEP_TIME = 41000
Global $timer

Global $frmBlock = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST), WinGetHandle(AutoItWinGetTitle()))
Global $lblTimer = GUICtrlCreateLabel("Going through login procedure, please wait" & @CRLF & $SLEEP_TIME / 1000 & " seconds remaining", 0, @DesktopHeight * .4, @DesktopWidth, @DesktopHeight * .4, $SS_CENTER)

Global $hMod = _WinAPI_GetModuleHandle(0)
Global $dll_block1 = DllCallbackRegister("_BlockInput", "long", "int;wparam;lparam")
Global $dll_block2 = DllCallbackRegister("_BlockInput", "long", "int;wparam;lparam")
GLobal $hook_ex_1 = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($dll_block1), $hMod)
GLobal $hook_ex_2 = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($dll_block2), $hMod)

GUICtrlSetFont(-1, 24, "", "", "Segoe UI")
GUICtrlSetColor(-1, 0xFF0000)
GUISetBkColor(0x000000, $frmBlock)

_WinAPI_SetLayeredWindowAttributes($frmBlock, 0x000000, 255)

GUISetState(@SW_SHOW, $frmBlock)
$timer = TimerInit()

While (Ceiling(($SLEEP_TIME - TimerDiff($timer)) / 1000) > 0)
    Sleep(200)
    GUICtrlSetData($lblTimer, "Going through login procedure, please wait" & @CRLF & Ceiling(($SLEEP_TIME - TimerDiff($timer)) / 1000) & " seconds remaining")
WEnd

GUIDelete($frmBlock)

_WinAPI_UnhookWindowsHookEx($hook_ex_1)
_WinAPI_UnhookWindowsHookEx($hook_ex_2)
DllCallbackFree($dll_block1)
DllCallbackFree($dll_block2)
Exit 0

Func _BlockInput($nCode, $wParam, $lParam)
    Return 1
EndFunc

 

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

×
×
  • Create New...