How to turn a DLL created in C# to VB6?

Asked

Viewed 1,412 times

1

I created a very basic DLL in C# just for testing. It looked like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;


namespace CriarDLL
{
[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsDual),
Guid("EB331808-BC1C-4B13-9ADC-E634E9102DF4")]
public interface ICalculator
{
    int AddNumbers(int x, int y);
    int SubNumbers(int x, int y);
    int MultNumbers(int x, int y);
    double DivNumbers(int x, int y);
}


[ComVisible(true), ClassInterface(ClassInterfaceType.None),
Guid("0BE3D996-63ED-4861-9310-05985C5FFA8E")]
public class Calculator : ICalculator
{
    //O construtor não pode conter parametros.
    public Calculator()
    {

    }

    public int AddNumbers(int x, int y)
    {
        return (x + y);
    }

    public int SubNumbers(int x, int y)
    {
        return (x - y);
    }

    public int MultNumbers(int x, int y)
    {
        return (x * y);
    }

    public double DivNumbers(int x, int y)
    {
        return (x / y);
    }
}
}

I did all the scoring processes Register for COM interop, marquei Sign the assembly and created a Strong Name Key, transformed Assembly Global (GAC), I created in services and components a COM+ Application (which shows the methods that are in the class).

Then I created another project in C# (Windows Forms) to test the DLL, when I went to add a reference in the COM tab, the DLL was there, but when I clicked OK the following message appeared:

A Reference to 'Criadll' could not be Added. The Activex type library 'local Criadll.tlb' was Exported from a . NET Assembly and cannot be Added as a Reference. Add a Reference to the . NET Assembly Instead.

Does anyone know if I did anything wrong? Or if something is missing?

2 answers

2


For some reason, Visual Studio doesn’t let you add a . dll COM knowing that this . dll was exposed from an Assembly . NET - it forces you to use this . dll as a common reference . NET.

In this case, to test your . dll, I suggest you create a code vbscript. I did a test here and it worked perfectly1.

Vbscript code:

' --- c:\temp\CriarDllCaller.vbs 
set objCriarDll = CreateObject("CriarDLL.Calculator")

WScript.Echo "Resultado Soma 2+2: " & objCriarDll.AddNumbers(2,2)
WScript.Echo "Resultado Subtração 10-5: " & objCriarDll.SubNumbers(10,5)
WScript.Echo "Resultado Multiplicação 5x5: " & objCriarDll.MultNumbers(5,5)
WScript.Echo "Resultado Divisao 20/4: " & objCriarDll.DivNumbers(20,4)

... and the command for execution + result :

C:\temp>C:\windows\SysWOW64\cscript.exe CriarDllCaller.vbs
Microsoft (R) Windows Script Host Versão 5.8
Copyright (C) 1996-2001 Microsoft Corporation. Todos os direitos reservados.

Resultado Soma 2+2: 4
Resultado Subtração 10-5: 5
Resultado Multiplicação 5x5: 25
Resultado Divisao 20/4: 5
  1. I actually broke my head at first; I had to force the Assembly compilation and the execution of the script both at 32-bit.

Source: http://jumbloid.blogspot.com.br/2009/12/making-net-dll-com-visible.html

1

According to an answer published for a similar question in stackoverflow.com this error occurs because instead of trying to add a reference to TBL, you should add a reference to DLL!
Please try and give us the feedback!

  • If I add a DLL by the Browser tab it works normally but if I try to add by the COM tab that error I posted.

  • But then your problem has been solved? You can use the DLL functions developed in . NET in the VB6 application?

  • No, I thought your answer referred to Visual Studio, on VB6 I did not test. I will have to test at home to be sure.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.