Home > .net 3.5, c#, JavaScript, Microsoft, Visual Studio 2008, Windows Vista, Workplace > Flash and Proxy Support on .NET WebBrowser Controls

Flash and Proxy Support on .NET WebBrowser Controls

January 3, 2008

At work, I’ve been dealing with Big Brother for the past few weeks (well, before the holiday break)—attempting to both accomplish a bit of work and some play in light of our annoying filter (blocking MSFT blogs, forums, community sites, and almost all useful search engines).

I love Daniel Mackey’s Pandora’s Box application, an encapsulation of the web browser for playing Pandora; however, it doesn’t support proxy servers.  I found a few other players, but, it appears, that IE 7 screws up a few things and the proxy settings don’t work correctly.  Bleh.  So, okay, it’s web browser and I’m a web developer—this can’t be that hard.

Heh.  If I only had a dollar for each time I said that…

Creating a Flash-enabled WebBrowser Control

One of the drawbacks to the current .NET WebBrowser control is that Flash, for some reason, can’t operate.  It just sits there—staring back at you.  I came across a post a few months ago to fix this.  It was originally targeted at 2.0, but seems to work just fine in 3.5

The fix?  Create a new user control and inherit from the WebBrowser control.  The only work is to override the WndProc method:

protected override void WndProc(ref Message m)

{

switch (m.Msg)

       {

             case 0x021:

             case 0x201:

             case 0x204:

             case 0x207:

                     base.DefWndProc(ref m);

                    return;

}

base.WndProc(ref m);

}

If you’re not familiar with how Windows forms messages are processed, you can find more information on MSDN.

Important: If you’re developing on an x64 (64–bit) machine, you must change your compile target platform to x86 (32–bit).  The WebBrowser control renders according to your platform—x64 in 64–bit, x86 in 32–bit.  Most of us use the 32–bit version of Internet Explorer on our x64 Windows because plugins, such as Flash, only have 32–bit editions.  In this case, Flash will not be able to load under the x64 WebBrowser control and will throw errors.

Implementing the Flash-enabled WebBrowser Control

I’m not real original on my control names… my derived control is called FlashWebBrowser.  Good deal.

To implement the control, add a Form to your page and size accordingly.  You can drag/drop your controls onto the form; however, for this example, I chose to do everything in code behind.

public partial class PandoraForm : Form

{

private FlashWebBrowser flashBrowser = new FlashWebBrowser();

 

       public PandoraForm()

       {

            InitializeComponent();

            flashBrowser.Dock = DockStyle.Fill;

            flashBrowser.AllowWebBrowserDrop = false;

            flashBrowser.IsWebBrowserContextMenuEnabled = false;

            flashBrowser.WebBrowserShortcutsEnabled = false;

            flashBrowser.ScriptErrorsSuppressed = true;

                       

            Controls.Add(flashBrowser);

            Load += new EventHandler(Form1_Load);

       }

Our flashBrowser object applies a few common attributes, such as filling the form and locking out the context menus.  Also, you’ll notice ScriptErrorsSuppressed.  Any JavaScript errors will pop-up automatically using the WebBrowser control (on IE, they’re visible in the status bar and only displayed if you explicitly have the option set to); this causes quite a bit of annoyance using services like Pandora, Google, and Flickr.  ScriptErrorsSuppressed will… suppress those errors.

Information: I’d like to caveat that last statement with a note.  It will USUALLY suppress those errors.  For some reason, IE 7.0 seems to ignore that statement and still throws the errors.  *sigh*  More information can be found ala Google.  I’m still researching this and hope to have a better understanding soon.

Our final method, Form1_Load as specified above, simply contains our flashBrowser’s Navigate method.

private void Form1_Load(object sender, EventArgs e)

{

flashBrowser.Navigate(http://www.pandora.com/?cmd=mini”);

}

Implementing Proxy Support for the WebBrowser Control

I haven’t cleaned this up into the FlashWebBrowser control yet, but I wanted to post this up for those interested.  Proxy support is REALLY useful if you’d like a particular application or service to use a proxy server, but don’t want to deal with the hassle of constantly changing your Internet settings.

You could use the HttpWebRequest object, pass along a WebProxy, and go that route.  However, quite honestly, I haven’t worked out how you can apply that to a WebBrowser control—only at the console.  I’m sure it can be done and may be more secure than this method, but this forum code works.

Cleaned up, the code looks like:

public struct INTERNET_PROXY_INFO

{

public int dwAccessType;

public IntPtr proxy;

public IntPtr proxyBypass;

};

 

[DllImport(“wininet.dll”, SetLastError = true)]

private static extern bool InternetSetOption(IntPtr hInternet,

int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

 

private void RefreshIESettings(string strProxy)

{

const int INTERNET_OPTION_PROXY = 38;

const int INTERNET_OPEN_TYPE_PROXY = 3;

 

INTERNET_PROXY_INFO struct_IPI;

 

// Filling in structure

struct_IPI.dwAccessType =

INTERNET_OPEN_TYPE_PROXY;

struct_IPI.proxy =

Marshal.StringToHGlobalAnsi(strProxy);

       struct_IPI.proxyBypass =

Marshal.StringToHGlobalAnsi(“local”);

 

       // Allocating memory

       IntPtr intptrStruct =

Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));

 

       // Converting structure to IntPtr

       Marshal.StructureToPtr(struct_IPI, intptrStruct, true);

 

       bool iReturn = InternetSetOption(IntPtr.Zero,

INTERNET_OPTION_PROXY,

intptrStruct,

Marshal.SizeOf(struct_IPI));

}

wininet.dll is the Windows library that contains most of the internet functions used by Windows and Windows-based applications.  Proxy settings are not the only things configurable using this API; for more information, read here.  Also, for a full understanding of what this code is doing, MSDN has a nice walkthrough demonstrating the various options and how to get/set them.

With that method added to our project, there’s only one change needed—we need to call our RefreshIESettings method prior to the Navigate method of our WebBrowser control.  Our Form1_Load method now looks like:

private void Form1_Load(object sender, EventArgs e)

{

RefreshIESettings(“myproxy.server.com:99”);

       flashBrowser.Navigate(http://www.pandora.com/?cmd=mini”);

}

I want to integrate the proxy settings INTO the WebBrowser control—and still might.  When that’s done, I’ll post up the code.  For now, however, that’s it.  We now have a WebBrowser control and a baby Windows Forms application that loads up a Flash site, like Pandora AND goes through a proxy server (so I can sit and listen to tunes at work OR be productive and access the MSFT community forums).  Good deal.

  1. Chad
    March 27, 2009 at 7:12 pm

    Can you explain how to unset the proxy through WinINET as well since you show how to set it, but not how to turn it off (or back to the default settings)?

    Thanks.

  2. May 8, 2009 at 8:29 pm

    Thank you so much! You saved me many hours trying to track down why flash didn’t run in the WebBrowser!

  3. May 21, 2009 at 7:34 pm

    Nice blog post, a good read, i’ll be bookmarking this.

  4. May 25, 2009 at 5:53 pm

    And who does not wish to pay for a hosting, is urgent here – the best free web hosting!

  5. May 26, 2009 at 11:01 am

    The friend recommended recently a good free web hosting, all the same the best service among the free!

  6. May 26, 2009 at 11:36 am

    My new partner – a free web hosting, be registered everything, I recommend!

  7. Jim
    June 12, 2009 at 3:33 pm

    Hey there, I really like this proxy control for the webbrowser component in visu studio 2008. I also would like to know how you would go about unsetting the proxy or defaulting it. If you could let me know how to do this I would really appreciate it.

    That is my real email address if you don’t want to answer via comments. Once again thanks for the great bit of code it really helped me out.

    • June 12, 2009 at 4:29 pm

      Hi Jim. I also wondering how to reset the proxy. After I figured it out, I I wrote about it: http://chadsowald.com/?p=33

      Thanks ~Chad

  1. January 4, 2008 at 3:34 pm
  2. May 21, 2009 at 8:52 pm
Comments are closed.
%d bloggers like this: