equityfund
Guest
|
Hi,
I need a way to temporarily disable user keyboard and mouse input.
I've tried the following:
First we define a function:
Friend Declare Function BlockInput Lib "user32" Alias "BlockInput" (ByVal fBlock As Boolean) As Boolean
Then I call this function as (it is wrapped by a Try/Catch/Finally statement):
BlockInput(True) ' Block input
...Do some work...
BlockInput(False) ' Enable input
The BlockInput statements are executes in a background thread.
Here is the link to the MSDN description:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646290(v=vs.85).aspx
I have made successful tests in a simple test form using this code, but for some reason it won't work in my AddIn. In the test case I made it worked both from the main thread and from a separate thread.
Thank you.
Update:
I tested this in my AddIn:
Try
MsgBox(BlockInput(True))
Dim lastError As String = Err.LastDllError
MsgBox(lastError)
System.Threading.Thread.Sleep(5000)
Catch ex As Exception
Finally
BlockInput(False)
End Try
The first messagebox gives "False", and the second one gives 5.
I looked it up here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
Apparently 5 means ERROR_ACCESS_DENIED. |
|
Andrei Smolin
Add-in Express team
Posts: 18823
Joined: 2006-05-11
|
Hello,
The last comment at that page as well as ERROR_ACCESS_DENIED suggest that you need to run the host application from the elevated Command Prompt.
Andrei Smolin
Add-in Express Team Leader |
|
equityfund
Guest
|
Hi,
The comment says "on Vista", but I guess it is possible it applies to Win 7/8 as well.
But the Host Application is Excel I assume? It works when I create a simple windows form project (i.e. not an ADX AddIn), and I don't start that program in any special way.
Do you know if there is any other method for blocking user input?
Thank you. |
|
Andrei Smolin
Add-in Express team
Posts: 18823
Joined: 2006-05-11
|
equityfund writes:
The comment says "on Vista", but I guess it is possible it applies to Win 7/8 as well.
Yes.
equityfund writes:
But the Host Application is Excel I assume?
Yes.
equityfund writes:
I don't start that program in any special way.
I assume that UAC is turned on. If so, I suppose that you start the program from Visual Studio running with elevated permissions. If so, try running the .EXE file instead. The main goal is running the progam with non-elevated permissions; I expect that the BlockInput method will fire an exception in this case.
equityfund writes:
Do you know if there is any other method for blocking user input?
Another strategy would be to undo all user actions.
Andrei Smolin
Add-in Express Team Leader |
|
equityfund
Guest
|
Hi Andrei,
You are right, I did execute the program through Visual Studio.
I will try to run the .exe instead and see if I get an exception. |
|