Click here to Skip to main content
11,790,021 members (58,386 online)
Click here to Skip to main content

Suppressing Hosted WebBrowser Control Dialogs

, 23 Nov 2008 CPOL 107K 7.3K 35
Rate this:
Please Sign up or sign in to vote.
Illustrates a technique for suppressing dialogs that might otherwise be shown to a user when hosting the WebBrowser control.


A software system I was involved with a while ago had a WinForms 2.0 client that was designed to facilitate a user's navigation path through pages of target web sites, and therefore hosted a WebBrowser control in the application. While most of the requirements of the system proved relatively easy to implement, several of them involved solutions that required some 'out of the square' techniques.

This article discusses one such problem area - suppressing various dialogs (including 'Security Alert' and 'Connect To ...' dialogs) - and demonstrates one possible approach to solving it.

As the article is marked 'Advanced' and the accompanying code is fairly extensively commented and reasonably self explanatory, I will not go into too much detail in the article here other than to outline the various techniques utilised in the solution.


Occasionally, it is desirable to navigate a hosted WebBrowser control to an SSL secured website which may serve, what the control determines, an invalid SSL certificate, for example, a company intranet site with a self-signed certificate. When the hosted WebBrowser control (or more accurately, a lower level Win32 DLL) determines that an SSL certificate is invalid, it will display a Security alert dialog (i.e., a dialog with the text "Security Alert" in the caption), regardless of whether your application deems the website trusted or not. Obviously, it is not always desirable, or alternatively, there is simply no need, for the user to see this dialog, so we require a method by which to suppress the dialog from ever showing.

Along similar lines, you might have a target web page which requires user logon. Again, the normal (and fully reasonable) response by the WebBrowser control is to display the user a dialog with which to enter their credentials (i.e., a dialog with the text "Connect To .." in the caption). But again, perhaps it is desirable to automate the logon process in your application and never bother the user with the dialog.

The solution to these problems lies in the use of 'Windows Hooks', some low-level functionality that is provided in the User32 DLL. Windows Hooks have been around for a long time, and importantly for this solution, what they provide for us is the ability to monitor Windows messages at a low level. Window Hooks are described in detail elsewhere (here and here, for example - also, simply Google it if you want more), so they will not be described further here.

Although .NET hides the gory details from us in layers of abstraction, the simple fact remains that when a dialog is about to be created/displayed, its message pump is sent a WM_INITDIALOG Windows message. Upon receiving this message, a dialog generally prepares itself to be displayed/used, a process which includes setting up any child controls etc. etc.

Using the WH_CALLWNDPROCRET style of Windows Hook, we can receive notification after a dialog message pump has processed the WM_INITDIALOG message and discover various properties of the dialog (such as its caption text, text, or class of contained child controls etc.). Once the required information is acquired, we can perform different actions such as simulating user-clicks on buttons and disallowing further processing of the message - the combination of which prevents the dialog from ever being displayed.

In order to access information such as the text of a dialog's caption or that of one of its child controls or the like, we make use of the following exported functions in the User32 DLL - EnumChildWindows(), GetWindowTextLength(), GetWindowText(), SetWindowText(), GetClassName(), GetDlgCtrlID(), and SendMessage() via the standard .NET Interop technology (i.e., DllImport). The signatures/descriptions of all of these functions are described in MSDN documentation, and most, if not all, have DllImport declarations at the pinvoke Web site.

Using the code

The VS2008 project (C#.NET 2.0) - downloadable using the link above - contains all of the required (and extensively commented) code, and can be tested by building and running the project. However, you must first fill in some values to test with before it will work:

  1. the URL of a site with an 'invalid' SSL certificate (e.g., (see the btnTestNoSecurityAlertDialog_Click() event handler).
  2. the URL of a site which requires Credentials for which you may want to perform auto logon (see the btnTestNoCredentialsDialog_Click() event handler).
  3. a Username and Password (see the WindowsInterop_ConnectToDialogWillBeShown() event handler) (only if wanting to test/use #2).

Points of interest

Using Windows Hooks to post-process dialog initialization Windows messages.


  • 23.11.2008 - Original article submission.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionFile Download doesnt dissappear Pin
Johngan10-Mar-15 19:11
memberJohngan10-Mar-15 19:11 
Questionit works Pin
Member 971321815-Dec-14 2:48
memberMember 971321815-Dec-14 2:48 
QuestionNot Working RE: Suppressing Hosted WebBrowser Control Dialogs Pin
Member 108507573-Jul-14 10:39
memberMember 108507573-Jul-14 10:39 
QuestionCatching Windows Security (Authentication box) Pin
Member 1041999922-Nov-13 9:49
memberMember 1041999922-Nov-13 9:49 
QuestionWindows Security popup - Choose from a list of client certificates Pin
Member 91533971-Nov-13 4:11
memberMember 91533971-Nov-13 4:11 
QuestionAppDomain.GetCurrentThreadId() Pin
Kanghoon Park15-Oct-13 13:17
memberKanghoon Park15-Oct-13 13:17 
SuggestionGreat Code !!! Beware Foreign Languages Pin
Fernando JB23-Jan-13 8:33
memberFernando JB23-Jan-13 8:33 
GeneralQuery for suppressing "Windows Security" dialog in Windows7 Pin
Chivate Atul16-Sep-10 1:54
memberChivate Atul16-Sep-10 1:54 
GeneralSuppressing Hosted WebBrowser Control Dialogs Pin
kiran523315-Sep-10 1:01
memberkiran523315-Sep-10 1:01 
GeneralRe: Suppressing Hosted WebBrowser Control Dialogs Pin
Member 91533971-Nov-13 4:16
memberMember 91533971-Nov-13 4:16 
GeneralA minor issue Pin
erhangorgun11-Nov-09 10:23
membererhangorgun11-Nov-09 10:23 
QuestionFile Download message Pin
tanya_kuraeva1-Oct-09 3:49
membertanya_kuraeva1-Oct-09 3:49 
AnswerRe: File Download message Pin
BimJeam19-Oct-09 15:14
memberBimJeam19-Oct-09 15:14 
GeneralHandling bypasses Document_Complete call Pin
gwakefield29-Jul-09 11:02
membergwakefield29-Jul-09 11:02 
GeneralRe: Handling bypasses Document_Complete call Pin
BimJeam29-Jul-09 18:20
memberBimJeam29-Jul-09 18:20 
GeneralRe: Handling bypasses Document_Complete call Pin
BimJeam29-Jul-09 18:29
memberBimJeam29-Jul-09 18:29 
GeneralRe: Handling bypasses Document_Complete call Pin
gwakefield30-Jul-09 3:21
membergwakefield30-Jul-09 3:21 
GeneralHandling for "save as" dialog box Pin
powered186-Jul-09 18:20
memberpowered186-Jul-09 18:20 
GeneralRe: Handling for "save as" dialog box Pin
BimJeam29-Jul-09 18:42
memberBimJeam29-Jul-09 18:42 
GeneralRe: Handling for "save as" dialog box Pin
powered1830-Jul-09 15:25
memberpowered1830-Jul-09 15:25 
GeneralRe: Handling for "save as" dialog box Pin
SimoneCapra20-Jan-10 7:40
memberSimoneCapra20-Jan-10 7:40 
GeneralUnhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll Pin
gian7721-May-09 3:54
membergian7721-May-09 3:54 
GeneralRe: Unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll Pin
BimJeam21-May-09 21:41
memberBimJeam21-May-09 21:41 
GeneralAlmost as good as sliced bread Pin
oldskoolmadskilz7-Apr-09 17:09
memberoldskoolmadskilz7-Apr-09 17:09 
GeneralRe: Almost as good as sliced bread Pin
BimJeam21-May-09 21:07
memberBimJeam21-May-09 21:07 
Questionanother way? Pin
umeca7415-Feb-09 5:48
memberumeca7415-Feb-09 5:48 
AnswerRe: another way? Pin
BimJeam15-Feb-09 14:14
memberBimJeam15-Feb-09 14:14 
GeneralThanks [modified] Pin
Tobias Manthey19-Jan-09 0:49
memberTobias Manthey19-Jan-09 0:49 
GeneralRe: Thanks Pin
Troy Russell6-Jun-09 14:44
memberTroy Russell6-Jun-09 14:44 
GeneralNeed same task in Visual C++ Pin
Wahaj Khan30-Nov-08 22:56
memberWahaj Khan30-Nov-08 22:56 
GeneralRe: Need same task in Visual C++ Pin
BimJeam1-Dec-08 1:01
memberBimJeam1-Dec-08 1:01 
GeneralRe: Need same task in Visual C++ Pin
Troy Russell6-Jun-09 14:40
memberTroy Russell6-Jun-09 14:40 
GeneralThanks (and some thoughts) Pin
NTrackMind24-Nov-08 6:02
memberNTrackMind24-Nov-08 6:02 
GeneralRe: Thanks (and some thoughts) Pin
BimJeam24-Nov-08 14:27
memberBimJeam24-Nov-08 14:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1509028.1 | Last Updated 23 Nov 2008
Article Copyright 2008 by BimJeam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid