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

1

I've created a pretty basic C # DLL, just to test. It looks 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 have done all the processes of marking Register for COM interop , marquei Sign the assembly and created a Strong Name Key , made 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 was adding a reference in the COM tab, there was the DLL, but when I clicked OK the following message appeared:

  

A reference to 'CreateDLL' could not be added.   The ActiveX type library 'local \ CreateLL.tlb' was exported from a .NET assembly and can not be added as a reference.   Add a reference to the .NET assembly instead.

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

    
asked by anonymous 31.01.2014 / 01:59

2 answers

2

For some reason, Visual Studio does not let you add a COM .DLL knowing that this .dll was exposed from a .NET assembly - it forces you to use this .dll as a common .NET reference.

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

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 to execute + 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
  • I actually broke my mind at first; I had to force assembly compilation and script execution both in 32-bit.
  • Source: link

        
    31.01.2014 / 12:35
    1

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

        
    31.01.2014 / 11:55