开发者

Is there a way to have a VB 6 app call a .NET assembly?

I'm having trouble translating C# code to VB 6 (Haven't done that in a looooong while). Is there a solution where I can code it in .NET and then have VB6 call the开发者_开发知识库 function instead?


Yes, you could expose your managed assembly as COM object using the regasm.exe utility. In order for the classes to be visible the assembly or individual classes need to be marked with the [ComVisible(true)] attribute. Once the assembly exposed as COM object you could consume it from VB6 as you would any standard COM object.


To expose your .NET method to COM, you'll need to create an interface:

[Guid("CF4CDE18-8EBD-4e6a-94B4-6D5BC0D7F5DE")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IFoo {

    [DispId(1)]
    string MyMethod(string value);
}

Your class will derive from the interface:

[Guid("7EBD9126-334C-4893-B832-706E7F92B525")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[ProgId("MyNamespace.Foo")]
public class Foo: IFoo {

    public string MyMethod(string value){
        return somestring;
    }
}

From VB6, your call will look something like:

Dim oFoo as New Foo
dim sReturn as string

sReturn = oFoo.MyMethod("someValue")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜