Passing strongly typed arguments in .NET COM interop

烂漫一生 提交于 2019-11-30 21:08:04

Make sure, you define a GUID attribute, this is necessary if you make a QueryInterface (VB does probably). You have to generate a new unique GUID for every comvisible class.

[Guid("77777777-3333-40df-9C0D-2B580E7E1F3B")]
[ComVisible(true)]
public class Foo
{
}

Then i would strongly recommend to write interfaces for your COM objects, and set the ClassInterface to None, so no internals are revealed. Your typelibrary will be much cleaner this way.

[Guid("88888888-ABCD-458c-AB4C-B14AF7283A6B")]
[ComVisible(true)]
public interface IFoo
{
}

[ClassInterface(ClassInterfaceType.None)]
[Guid("77777777-3333-40df-9C0D-2B580E7E1F3B")]
[ComVisible(true)]
public class Foo : IFoo
{
}

After struggling with this same issue for a while, I found that is was having issues with passing argments by reference instead of by value. See here:

http://msdn.microsoft.com/en-us/library/ee478101.aspx

So I just added round brackets to the passed argument in VB Script, and it seemed to have solved the problem. So in your example, just doing this:

Set foo = CreateObject("TestCSProject.Foo")
Set bar = CreateObject("TestCSProject.Bar")
Call bar.Method((foo))

Should work as expected, without having to set the ClassInterface attribute, and without using Interfaces.

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