简体   繁体   English

如何获取mshtml.IHTMLDocument6或mshtml.IHTMLDocument7?

[英]How to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7?

I am using IE11 in Windows 7. Then I added a reference in C# project c:\\Windows\\System32\\mshtml.tld and try to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7, but VS2013 doesn't see it. 我在Windows 7中使用IE11。然后我在C#项目c:\\ Windows \\ System32 \\ mshtml.tld中添加了一个引用并尝试获取mshtml.IHTMLDocument6或mshtml.IHTMLDocument7,但是VS2013没有看到它。 I can only get mshtml.IHTMLDocument, mshtml.IHTMLDocument2 .. mshtml.IHTMLDocument5. 我只能得到mshtml.IHTMLDocument,mshtml.IHTMLDocument2 .. mshtml.IHTMLDocument5。

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

Select the MSHTML assembly reference, look at its Path property and you'll see the problem: 选择MSHTML程序集引用,查看其Path属性,您将看到问题:

C:\\WINDOWS\\assembly\\GAC\\Microsoft.mshtml\\7.0.3300.0__b03f5f7f11d50a3a\\Microsoft.mshtml.dll C:\\ Windows \\组件\\ GAC \\ Microsoft.mshtml \\ 7.0.3300.0__b03f5f7f11d50a3a \\ Microsoft.mshtml.dll

This is the PIA for the mshtml type library, installed on any programmer's machine that has VS installed. 这是mshtml类型库的PIA,安装在安装了VS的任何程序员的机器上。 Version 7.0.3300 is a version number from the .NET 1.0 era, it is 14 years old. 版本7.0.3300是.NET 1.0时代的版本号,已有14年历史。 Predates IE11 of course, that's why it doesn't have the later interface types. 当然,预测IE11,这就是为什么它没有后来的接口类型。 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. 使用PIA永远不是错误的事情,它确保您的程序可以运行任何版本的IE,一直回到IE6。

But that's not what you want, you'll have to generate your own interop library from the IE11 type library. 但这不是你想要的,你必须从IE11类型库生成自己的互操作库。 Run the Visual Studio Command Prompt, navigate to your project directory and type this command: 运行Visual Studio命令提示符,导航到项目目录并键入以下命令:

  Tlbimp c:\windows\system32\mshtml.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. 它会拖延很长一段时间,它是一个很大的问题,并且会出现几个关于PIA存在的警告以及几个不能直接从.NET程序中使用的模糊方法和类型。 You can ignore them, they didn't work in the PIA either. 你可以忽略它们,它们也不适用于PIA。

You now have the Interop.mshtml.dll file in your project directory. 您现在在项目目录中有Interop.mshtml.dll文件。 Back to VS, delete the old MSHTML reference and use Project > Add Reference > Browse to select the new one you just created. 返回VS,删除旧的MSHTML引用并使用Project> Add Reference> Browse选择刚刚创建的新引用。 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. 默认情况下,它的“嵌入互操作类型”属性为True,根本不再需要PIA,您不必在程序中部署该文件。 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. 并且要小心,当程序在没有安装所需IE版本的计算机上运行时,程序可能会因E_NOINTERFACE异常而死亡。 You can use the as operator to cast the document reference to IHtmlDocument7, it will be null if IE is old. 您可以使用as运算符将文档引用强制转换为IHtmlDocument7,如果IE旧了则它将为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM