Is this .tlh file correct, and if not, then how do I generate the correct one?

点点圈 提交于 2019-12-06 03:35:17

A .tlh file cannot be wrong, it is auto-generated from the type library of your COM server. An obvious flaw is that it is rather empty, you don't see any declarations at all.

The problem is with that Codeproject.com article, par for the course in such projects is that it is missing an essential step, doesn't explain what is really going on and often uses very bad practices. In order for a .NET type to be usable from a COM client, you have to be explicit about making it visible to COM clients. Add this attribute to every type that you want to export:

  [ComVisible(true)]

Apply it to both the interface and the class.

Using the [Guid] attribute, like the author proposes, is very dangerous. It is okay to leave it in place while you are developing the library, it helps avoid registry pollution and lets you skip the Regasm step (not always), but it is pretty important to remove it again before you ship the library. A rock-hard rule in COM is that a modification to an interface requires a new guid, an essential DLL Hell counter-measure. You automatically get a new one when you leave it up to the CLR to auto-generate one.

Registering the assembly in the GAC is also best avoided while you are developing, it causes too many accidents with leaving a stale copy of the assembly in the GAC. Use the /codebase option in the Regasm command so this is not needed. You can ignore the warning you get.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!