Detecting UAC prompt

Hi all,

I’m working on an app that continuously tracks the mouse pointer. It takes actions when the mouse is at various corners of the screen, for example when it’s in the upper-left corner (0, 0).

I detect the mouse coordinates using the GetCursorPos API.

A bug I’m running into is that GetCursorPos returns 0, 0 for the mouse when the system is displaying a full-screen UAC prompt, such as this one:

Does anyone know how to detect if a UAC prompt is currently being displayed? This way I can throw out the data while the dialog is displayed.

Answered my own question!

For anyone else, the solution is to test for the process CONSENT.EXE, which is the application Windows uses to prompt for UAC elevation.

You can get a false positive if some other app developer uses CONSENT.EXE as a program name though. So if anyone knows a better way, I’m all ears.

This happens because consent.exe switches to a different virtual desktop to show the UAC prompt. Only apps running on the currently active desktop can query the mouse/keyboard state.

This method compares the name of the currently active desktop to the “default” desktop (i.e. the one your app is running on):

[code]Function CanQueryUI() As Boolean
Declare Function OpenInputDesktop Lib “User32” (Flags As Integer, Inheritable As Boolean, DesiredAccess As Integer) As Integer
Declare Function GetUserObjectInformationW Lib “User32” (hDesktop As Integer, Index As Integer, Buffer As Ptr, Length As Integer, ByRef LengthNeeded As Integer) As Boolean
Declare Function CloseDesktop Lib “User32” (hDesktop As Integer) As Boolean

Const UOI_NAME = 2

Dim ret As Boolean
Dim size As Integer
Dim hDesktop As Integer = OpenInputDesktop(0, False, 0)

Call GetUserObjectInformationW(hDesktop, UOI_NAME, Nil, 0, size)
If size > 0 Then
Dim mb As New MemoryBlock(size)
Call GetUserObjectInformationW(hDesktop, UOI_NAME, mb, mb.Size, size)
ret = (mb.WString(0) = “default”)
End If
Call CloseDesktop(hDesktop)
Return ret
End Function

[/code]

Thank you for this! This is a MUCH better solution as it also takes other Virtual Desktops into account, something which I had completely forgotten about.