Problem creating COM-library only containing enum's

人盡茶涼 提交于 2019-12-05 20:33:19

To answer your last question first. What you are wanting is a TypeLib not a COM library. Where a COM interface is a bunch of code and function pointers, a TypeLib is the map for interacting with those pointers (along with defines and enums and a bunch of other stuff). Only when they come together is there a COM library. Since there is no COM interface, you cannot have a COM library.

Microsoft has provided an example on how to create a TypeLib without an interface. It is a very similar to the one you have described. As you'll notice, there's no COM interface in it; and because of this, it has to stay a lowly TypeLib.

The next problem is the .NET assembly. When you use TlbImp.exe to import the enums into your code, that allows you to use those enums within your code - inside your assembly. That is the limit of what you can do with the enums. You cannot export those enums because they don't belong to your .NET code. The enum is owned by the TypeLib. Your .NET code has permission to use the enum, but it cannot claim to own the enum.

Finally, to answer your first question. You need to use the features provided with .NET. It has the ability to define enums and export them and make them visible from COM. While I understand the frustration over naming convention, this is not something that you should try to work around or bypass. As you have seen, attempting to bypass this minor problem with the naming convention has caused major problems, effectively making your new code unusable.

I have done this:

In .NET, I created a COM visible library named PermissionControlLib with an enum like this:

public enum NetOperations
{
   Oper1,
   Oper2,
   Oper3
}

In VB6, I've created another enum like this:

Public Enum VBOperations
   Oper1=NetOperations.NetOperations_Oper1,
   Oper2=NetOperations.NetOperations_Oper2,
   Oper3=NetOperations.NetOperations_Oper3
End Enum

Usage:

Dim ud as PermissionControlLib.IUser
Set ud = New User
Dim b as Boolean
b = ud.HasPermissionInOperation(VbOperations.Oper1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!