473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Detect if an exteranl process has a modal form showing

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

Nov 21 '05 #1
1 4101
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

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ChrisR | last post by:
Hi all Ive have a Main Form with Command Buttons down the Left hand side, that open various forms. Clicking on a button opens a Sub Form which is moved to take up 3/4s of the right of the main...
4
by: Paul Aspinall | last post by:
Can anyone advise how to display a form on top, and modal, without using ShowDialog?? Thanks
1
by: Jeremy | last post by:
Hello, I'm thinking this may be an easy one but I am getting frustrated. How can I detect if a process is showing a modal dialog. Like "Are you sure you want to exit?", or an error message...
23
by: Chukkalove | last post by:
Im sorry, I dont know the correct description for a hierarchy of parent and child forms. I have a main form that opens a child form modally. This child form in turn opens it's own child form...
4
by: forest demon | last post by:
i've seen previous posts somewhat related to this, so i apologize if this is redundant. I have a main form with separate threads. When showing a dialog like <dialog>.ShowDialog(), it functions...
4
by: Johnny Jörgensen | last post by:
In my current project I've got some DataGridViews containing multiple hidden columns. For some reason 8 of the 10 grids I've got on my form has suddenly started showing 1 of the hidden columns...
0
by: =?Utf-8?B?a20=?= | last post by:
I've got a simple C# app built in VS2005. When I click a menu item a modal form opens and performs a lengthy operation in the Load event handler to populate a text control, so it takes a few...
2
by: diogenes | last post by:
I have created many shortcut/popup (aka context, or right-click) menus for my application - instead of toolbars or standard drop-down menus. Within my custom menu, I am using...
3
by: RLN | last post by:
Re: Access 2003 I have a process that sends out emails to addresses (internally in our company). Form1 has all the main functionality/data, etc on it. When the user clicks "Send Emails" on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.