views:

232

answers:

3

Hi folks,

I have a winforms c# app that has an embedded webbrowser control inside it generated through VS.NET 2008. We sink events by inheriting our events class from HTMLDocumentEvents2.

 public class IEHTMLDocumentEvents : mshtml.HTMLDocumentEvents2
 {
     public bool onclick(mshtml.IHTMLEventObj pEvtObj)
     {
         // Clicking on an input (checkbox, radio, button, image)
         if (pEvtObj.srcElement.tagName == "INPUT")
         {
             // The following will result in a null pointer without VS.NET installed
             HTMLInputElementClass input = pEvtObj.srcElement as HTMLInputElementClass;
         }
     }
 }

The code above works fine when clicking on elements in the webbrowser control on our dev machines with VS.NET installed. However it fails to cast the pEvtObj.srcElement when VS.NET is not installed. This immediately starts working when we install the most basic VS.NET with C# that you can.

To note: The rest of the c# app works fine, and you can browser the web through the control fine as well, just that the events like the above 'onclick' can't be handled properly.

I thought it would be a DLL version loaded issue but doing a diff of the files loaded indicates only minor differences.

1c1
< Process: C# App without VS.NET installed
---
> Process: C# App with VS.NET 2008 installed
18d17
< C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\CustomMarshalers\e148983beeb0f30918b0564849a16456\CustomMarshalers.ni.dll CustomMarshalers.ni.dll Microsoft .NET Framework Custom Marshalers Microsoft Corporation 2.0.50727.3053
36d34
< C:\Documents and Settings\XpHome\Local Settings\History\History.IE5\index.dat index.dat   
37a36
> C:\Documents and Settings\XpHome\Local Settings\History\History.IE5\index.dat index.dat   
44,45c43,44
< C:\Program Files\<hidden>\<hidden>\Microsoft.mshtml.dll Microsoft.mshtml.dll     7.0.3300.1
< C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\Microsoft.VisualBas#\5b3d048d8c003d743ea5e72caf07773a\Microsoft.VisualBasic.ni.dll Microsoft.VisualBasic.ni.dll Visual Basic Runtime Library Microsoft Corporation 8.0.50727.3053
---
> C:\WINDOWS\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll Microsoft.mshtml.dll     7.0.3300.1
> C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualBasic\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll Microsoft.VisualBasic.dll Visual Basic Runtime Library Microsoft Corporation 8.0.50727.3053
50,52c49,51
< c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorjit.dll mscorjit.dll Microsoft .NET Runtime Just-In-Time Compiler Microsoft Corporation 2.0.50727.3053
< C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\mscorlib\9adb89fa22fd5b4ce433b5aca7fb1b07\mscorlib.ni.dll mscorlib.ni.dll Microsoft Common Language Runtime Class Library Microsoft Corporation 2.0.50727.3053
< c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll mscorwks.dll Microsoft .NET Runtime Common Language Runtime - WorkStation Microsoft Corporation 2.0.50727.3053
---
> c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorjit.dll mscorjit.dll Microsoft .NET Runtime Just-In-Time Compiler Microsoft Corporation 2.0.50727.3082
> C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\mscorlib\7124a40b9998f7b63c86bd1a2125ce26\mscorlib.ni.dll mscorlib.ni.dll Microsoft Common Language Runtime Class Library Microsoft Corporation 2.0.50727.3603
> c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll mscorwks.dll Microsoft .NET Runtime Common Language Runtime - WorkStation Microsoft Corporation 2.0.50727.3603
94,98c93,97
< C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Configuration\cb4cb21d14767292e079366a5d3d76cd\System.Configuration.ni.dll System.Configuration.ni.dll System.Configuration.dll Microsoft Corporation 2.0.50727.3053
< C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Drawing\6978f2e90f13bc720d57fa6895c911e2\System.Drawing.ni.dll System.Drawing.ni.dll .NET Framework Microsoft Corporation 2.0.50727.3053
< C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System\aa7926460a336408c8041330ad90929d\System.ni.dll System.ni.dll .NET Framework Microsoft Corporation 2.0.50727.3053
< C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Windows.Forms\9a254c455892c02355ab0ab0f0727c5b\System.Windows.Forms.ni.dll System.Windows.Forms.ni.dll .NET Framework Microsoft Corporation 2.0.50727.3053
< C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Xml\36f3953f24d4f0b767bf172331ad6f3e\System.Xml.ni.dll System.Xml.ni.dll .NET Framework Microsoft Corporation 2.0.50727.3053
---
> C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll System.Configuration.dll System.Configuration.dll Microsoft Corporation 2.0.50727.3053
> C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Drawing\abb2ac7e08bee026f857d8fa36f9fe6f\System.Drawing.ni.dll System.Drawing.ni.dll .NET Framework Microsoft Corporation 2.0.50727.3053
> C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System\3de5bd01124463d7862bd173af90bc83\System.ni.dll System.ni.dll .NET Framework Microsoft Corporation 2.0.50727.3053
> C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Windows.Forms\d2ea8d76f015817db1607075812b555f\System.Windows.Forms.ni.dll System.Windows.Forms.ni.dll .NET Framework Microsoft Corporation 2.0.50727.3053
> C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Xml\5913d3f81e77194ec833991b1047a532\System.Xml.ni.dll System.Xml.ni.dll .NET Framework Microsoft Corporation 2.0.50727.3082
+1  A: 

Theoretically, because the IE version on your computer and the IE version on the user's computer can be different, you need to generate wrappers for the user's IE version. This can be done in your installer by adding a custom action that runs tlbimp mshtml.tlb in the command prompt, but that requires .Net SDK on the machine first, and there was a bug in IE6's type library that generate a wrong signature for an event so you may want to ship an IE7 version for IE6 users in case you code depend the event to fire.

Fortunately most of the interfaces in IE's type library haven't changed in years (for the interfaces you are most likely interested in). You can ship your app with Microsoft.mshtml.dll, which is included in Office PIAs so you can find prerequisite packages from Office developers.

Or better, you can write a smaller assembly that only contains the interface you use and redistribute it. Visual Studio 2010 does this for you, though may not be perfectly.

Sheng Jiang 蒋晟
I do distribute the Microsoft.mshtml.dll already, and you can see it loaded in the diff but from different locations depending on the environment. It loads it from the packaged version when VS.NET is not installed, and loads it from the GAC when it is installed.
Shane
A: 

Flag the webbrowser external reference (dll) in your project to be always copied to the output dir when the prj is built.

vaitrafra
Not sure what you mean here. If the webbrowser dll wasn't being linked correctly, then surely it would fail to work at all during runtime in the environment without the VS.NET installed. The c# app works fine except for when the events on the web control are triggered. I.e., you can browse the web as normal through the app but the events aren't handled correctly.
Shane
+1  A: 

Ok, found the answer finally. It appears that even though it successfully loads the microsoft.mshtml.dll that we distribute with the app. The COM interfaces cannot be accessed unless you register it in the GAC on the client machine using regasm.exe.

Shane