Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

c# - How to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7?

I am using IE11 in Windows 7. Then I added a reference in C# project c:WindowsSystem32mshtml.tld and try to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7, but VS2013 doesn't see it. I can only get mshtml.IHTMLDocument, mshtml.IHTMLDocument2 .. mshtml.IHTMLDocument5.

IHTMLDocument7 interface https://msdn.microsoft.com/ru-ru/library/windows/hardware/ff975572

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Select the MSHTML assembly reference, look at its Path property and you'll see the problem:

C:WINDOWSassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3aMicrosoft.mshtml.dll

This is the PIA for the mshtml type library, installed on any programmer's machine that has VS installed. Version 7.0.3300 is a version number from the .NET 1.0 era, it is 14 years old. Predates IE11 of course, that's why it doesn't have the later interface types. Using the PIA is never the wrong thing to do, it ensures that your program will run with any version of IE, all the way back to IE6.

But that's not what you want, you'll have to generate your own interop library from the IE11 type library. Run the Visual Studio Command Prompt, navigate to your project directory and type this command:

  Tlbimp c:windowssystem32mshtml.tlb /out:Interop.mshtml.dll

It will trundle for quite a while, it is a big one, and spit out several warnings about the PIA being present and several obscure methods and types that cannot be directly used from a .NET program. You can ignore them, they didn't work in the PIA either.

You now have the Interop.mshtml.dll file in your project directory. Back to VS, delete the old MSHTML reference and use Project > Add Reference > Browse to select the new one you just created. Its "Embed Interop Types" property is True by default, no longer a need for a PIA at all and you don't have to deploy the file with your program. Check the file into source control so you only have to do it once.

And be careful, your program can die with an E_NOINTERFACE exception when it runs on machine that doesn't have the required version of IE installed. You can use the as operator to cast the document reference to IHtmlDocument7, it will be null if IE is old.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...