Detect if an exteranl process has a modal form showing

J

Jeremy

I apologize for the repost but I am frustrated and desperate for a
solution.

I need to detect if an external application is displaying a modal form.
I.E. dialog box or error message.

I found a number of promising examples but unfortunetly they are VB6
examples and just don't seem to be working correctly:

---------------Start Code------------------------------------
Private Declare Function apiGetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal Hwnd As Long, ByVal _
nIndex As Long) As Long

Function IsModal(ByVal hWndFrm As Long) As Boolean
Dim hWndParent As Long
Dim wStyle As Long


Const GW_STYLE = (-16)
Const WS_DISABLED = &H8000000
Const GW_HWNDPARENT = (-8)


hWndParent = apiGetWindowLong(hWndFrm, GW_HWNDPARENT)
If hWndParent = 0 Then
IsModal = False
Else
wStyle = apiGetWindowLong(hWndParent, GW_STYLE)
IsModal = ((wStyle And WS_DISABLED) <> 0)
End If
End Function

---------------End Code------------------------------------

Then I run the following on a timer:

---------------Start Code------------------------------------

Dim MonitoredProcess As Process() =
Process.GetProcessesByName("BuggyThirdPartyApp")

IsModal(MonitoredProcess (0).MainWindowHandle)

---------------End Code------------------------------------

It always returns false even if a modal is up.
The apiGetWindowLong(hWndFrm, GW_HWNDPARENT) always returns zero.
Thanks for your help!

Jeremy
 
J

Jeremy

I solved it...there were two problems.

1. I had to get the handle of the apps main window using the FindWindow
api...not the process.MainWindowHandle, those handles are different!

2. the Const WS_DISABLED = &H8000000 was never returned when the main
window handle was in a disabled state. Instead I noticed it was always
&H1ECF000 (516882432) so I revised the constant value. (I wonder why
this, is there is no documentation stating this..that I found)

The Solution:

Private Declare Function apiGetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal Hwnd As IntPtr, ByVal _
nIndex As Long) As IntPtr

Private Declare Auto Function FindWindow Lib "user32" (ByVal
lpClassWindow As _
String, ByVal lpWindowName As String) As IntPtr

Private Const GW_STYLE As Int32 = (-16)
Private Const JH_DISABLED As Int32 = &H1ECF0000

Private Function ExternalAppDisabled() As Boolean

Dim hWnd As IntPtr = FindWindow(Nothing,
"MyAppsMainWindowName")

If apiGetWindowLong(hWnd, GW_STYLE).ToInt64 = JH_DISABLED Then
ExternalAppDisabled = True
Else
ExternalAppDisabled = False
End If

End Function

The cool thing about this is you can detect if any external app has a
modal dialog up. Hope this helps someone in the future....

Thanks,
Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top