开发者

COM object only works in VB.NET not C#

I have a COM object that works fine in VB.NET, but not in C#. Both projects are .NET 4 console applications.

The COM object loads in C#, but the methods don't return an开发者_开发问答y values. Why would it work in VB.NET and not C#?

Thanks!

Sub Main()
    Dim server As New NoahVersionLib.Version

    Dim val As Int32

    server.GetNoahServerVersionMS(val)



End Sub


    static void Main(string[] args)
    {
        var server = new NoahVersionLib.Version();

        int val= 0;

        server.GetNoahServerVersionMS(ref val);
    }

val is 0 in the C# build, but has a value in the VB.NET build.

UPDATE:

I needed to put [STAThread] on my Main() in C#. It works now.


I needed to put [STAThread] on my Main() in C#. It works now.


Have you tried the following in C#?

static void Main(string[] args)    
{        
    NoahVersionLib.Version server = new NoahVersionLib.Version();        
    Int32 val= 0;        
    server.GetNoahServerVersionMS(ref val);    
}

The only differences between your 2 versions are that you are using var instead of declaring the actual type, allowing the compiler to potentially infer an incorrect type (unlikely, but surely possible); and use of int instead of Int32. As far as I know, int == Int32, but possibly in some weird corner case of COM, it may not..?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜