7

I created a COM-interop .dll with this simple class:

using System.Runtime.InteropServices;

namespace ClassLibrary1
{
    [ComVisible(true)]
    [Guid("795ECFD8-20BB-4C34-A7BE-DF268AAD3955")]
    public interface IComWeightedScore
    {
        int Score { get; set; }
        int Weight { get; set; }
}

[ClassInterface(ClassInterfaceType.None)]
[Guid("9E62446D-207D-4653-B60B-E624EFA85ED5")]
public class ComWeightedScore : IComWeightedScore
{

    private int _score;

    public int Score
    {
        get { return _score; }
        set { _score = value; }
    }
    private int _weight;

    public int Weight
    {
        get { return _weight; }
        set { _weight = value; }
    }

    public ComWeightedScore()
    {
        _score = 0;
        _weight = 1;
    }
  }

} I registered it using: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\regasm C:\ComClasses\Classlibrary1.dll /tlb: Classlibrary1.tlb

Finally I succesfully added a reference to the .dll after which VB6 gave me intellisense on the object.

Private Sub Form_Load()
    Dim score1 As ComWeightedScore

    Set score1 = New ComWeightedScore
    score1.Score = 500

End Sub

On the line Set score1=new ComWeightedScore the exception Automation Error is raised.

It can hardly be any simpler than this... Where is the error?!

1
  • And why do you say the error is in assigning int or long? what fails is the constructor call. Does it still fail if you remove the assignments from the body of the constructor? Also, provide more info and details on the error you get. Commented Aug 28, 2011 at 22:29

3 Answers 3

10

You forgot the /codebase option in the Regasm.exe command line.

Without it, you'll have to strong-name the assembly and put it in the GAC with gacutil.exe. Good idea on the client machine, just not on yours.

Sign up to request clarification or add additional context in comments.

Comments

4

If you are running on a 64bit processor with your project compiling as 'CPU-Any' you will either need to compile only for x86 or register the dll in the 64bit COM+ space.

Example of both 32 and 64bit regasm:

%windir%\Microsoft.NET\Framework\v4.0.30319\regasm "Contoso.Interop.dll" /tlb:Contoso.Interop.tlb /codebase Contoso.Interop

%windir%\Microsoft.NET\Framework64\v4.0.30319\regasm "Contoso.Interop.dll" /tlb:Contoso.Interop.tlb /codebase Contoso.Interop

Comments

1

I also encountered a similar issue when I ran a generated EXE because I let a local copy of the dll in the VB6 project directory (previously for test purposes).

Running the project in debugging mode (F5) was fine, but the EXE loaded the local dll rather than getting the registred TLB.

Such code below, referencing the interface crashed:

Dim sf As StuffUtils.IStuffer
Set sf = New StuffUtils.Stuffer

Just letting this answer here since it might prevent another coder to waste his time on it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.