Friday, June 08, 2007

Crash when using windows.external to call a C# class method

Recently I was using the WebBrowser control embedded into a Windows Form. I assigned the current form object to the webBrowser control using the property ObjectForScripting property. My hope was that I would be able to use the javaScript method windows.external.PublicMethodInMyClass to call methods in my form: essentially a callback from the webBrowser control. Remember you have to declare [ComVisible(true)] on your class for it to be available to be called via JS from HTML page and if you plan on assigning it to ObjectForScripting. Now for some reason I was having a crash everytime that windows.external was being accessed in my HTML code. After much debugging I found out that the problem was that I was declaring the ClassInterfaceType.None attribute on my class. ([ClassInterface(ClassInterfaceType.None)]) Once I removed that attribute - voila! everything started working correctly. Who knew!

No comments: