Link to home
Start Free TrialLog in
Avatar of psyche6
psyche6

asked on

Error calling .Net lib from VB script, calling from VB6 executable works

When I create a C# .Net library (Framework 2.0 and Visual Studio 2005) and expose it to com, it works great from a Visual Basic 6 client but doesn't work from a VB script client (such as Excel).

The error message from Microsoft Visual Basic in Excel is:
Run-time error '-2147024894(800070002)';
File or assembly name TestCallingDotNetFromCom, or one of its dependencies, was not found.

Here's the C# library code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestCallingDotNetFromCom
{
    public interface iTestStuff
    {
        string SayHello();
    }

    [ClassInterface(ClassInterfaceType.None)]
    public class TestStuff : iTestStuff
    {
        public TestStuff()
        {
        }

        public string SayHello()
        {
            return "Hello there";
        }
    }
}

It's complied as a strong-name assembly with the Make assembly COM-Visible checkbox checked. I copy the library to another machine (XP Professional) and register it as:
regasm TestCallingDotNetFromCom.dll /tlb
This creates the type library and registers both the type library and the library. Then I copy the library to the GAC.

I open up the Visual Basic editor in Excel (XP version) and set a reference to TestCallingDotNetFromCom. Examining it from the Object Browser, it looks fine. I add a module with the code:
Sub Test()
    Dim obj As New TestCallingDotNetFromCom.TestStuff, _
        s As String
    s = obj.SayHello
    Set obj = Nothing
End Sub
When I run the code I get the error mentioned above.

HOWEVER, I use Visual Basic 6 to create an executable with EXACTLY  the same code, then copy the exe to the test machine and run it, it works perfectly!

What's the difference between calling a com callable .Net library from Visual Basic 6 and Visual Basic script in Excel?

Appreciate any help!
Avatar of existenz2
existenz2
Flag of Netherlands image

Avatar of psyche6
psyche6

ASKER

Thanks to existenz2, but that's not it.

I am carefully registering the library:
    regasm TestCallingDotNetFromCom.dll /tlb
which both creates the type library AND registers the library and the new type library in one pass.

I am also adding the library to the GAC:
    gacutil /i TestCallingDotNetFromCom.dll
and checking the \Windows\assembly folder afterwards.

As I mentioned, a compiled VB6 .exe that uses TestCallingDotNetFromCom as a com server works fine when copied to the test PC (in a different folder, of course, than the TestCallingDotNetFromCom.dll file so as to be sure that the .exe is using it from the GAC). The compiled VB6 .exe actually accesses the library twice - once using design time linking and once using run time (CreateObject) linking. However, when I try to access the library from VB script within Excel, I still get the same error mentioned in my first post.
Avatar of psyche6

ASKER

I found this article:
    http://discuss.joelonsoftware.com/default.asp?dotnet.12.302317.3
and thought FOR SURE
Avatar of psyche6

ASKER

I found this article
    http://discuss.joelonsoftware.com/default.asp?dotnet.12.302317.3
and thought FOR SURE I'd found the answer. Unfortunately, it didn't work. Still, this article is really intriguing - I think the answer will be something similar.
Avatar of psyche6

ASKER

After a technical support call to Microsoft, the answer is that there is a bug with VBA and/or Office linking to the wrong version of the framework if more than one version of the framework is loaded.

Here's a link to the relavent Microsoft article:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;908002

Note: this is not a fix that you load directly onto the client machine. Rather it is an update to Visual Studio 2005 which gives you a resource that you include as a prerequisite in a Visual Studio 2005 setup project that you deploy on the client machine. You can include only this resource in the setup project if you only want to apply the fix to the client machine, or you may include the resource in the setup project of an application or library you are deploying that will require this fix.

This question is closed.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial