Trying to build a COM Component in C# that can be referenced and used from VB5/6

纵饮孤独 提交于 2019-12-05 20:11:48

I see you've worked out a way to get what you want while I was typing up this reply, but I'll continue anyway because it might tell you some stuff you don't know...

You've applied the ClassInterface(ClassInterfaceType.None) attribute to the class. This tells COM interop not to define an explicit interface for the class, and so your client must use IDispatch (late binding). This means that your client must have a-priori knowledge of the interfaces that the class implements. That is, you (the programmer) know what methods are available but tools like IntelliSense have no way of finding that information.

Just go ahead and call the method:

Dim response As String
response = arcom.GetServiceResponse

OK, I found something that unexpectedly made a difference.

Notice that the code above contains the decoration [ComVisible(true)]. Making the assembly visible to COM is of course essential. I thought that this covered it, but upon further searching I found that there is another place to mark it. If you look in the project properties, in the Application tab you will find the Assembly Information... button. Click on it and at the bottom of the dialog box you'll see a checkbox "Make assembly COM-visible". I checked it, recompiled (and re-ran tlbexp and regasm), and then found that the method GetServiceResponse() was visible.

I don't know why the make ComVisible checkbox works when setting the attribute doesn't.

Edited to add: I think I see what is up with that. The ComVisible(true) makes the class visible to COM, but not the assembly; this is why "Make assembly COM-visible" needs to be checked, because the GetServiceResponse method seems to get its COM-visibility through the ARCOM_Interface, which is not marked as ComVisible unless the assembly is as well.

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