Using a C# dll in VB6

Hi,

I would like to know if it is possible to use a dll that has been created in C#, in Visual Basic 6.0. If so it would be great if you could let me know how I can make this possible.

Thanks & Regards,
Frenz



Answer this question

Using a C# dll in VB6

  • BrandyR

    Digimonk,

    I tried it, but I still get the error "Invalid procedure call or argument.". In fact I also tried using this code:

    Dim Exceptionobj As Exception
    Set Exceptionobj = New Exception

    Errorob.ShowError(Exceptionobj)

    Can you figure it out why this is happening.

    Thanks,

    Frenz



  • charliestrause

    Hi,

    Is it possiblt to include our own C# dll's to VB 6.0 pgm. The dll's contains more no.of user controls. I wish to use those user controls to our VB pgm. Can give the clear steps for this plz.

    Cheer.



  • Dhanya

    You can create an extra class to map the ErrObject to your .NET dll:


    [ComVisible(true)]
    [Guid("80FA0002-4976-4226-A539-397BD93E9B6A")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface _ErrorObject
    {
    int ErrorCode
    {
    get;
    set;
    }

    string Message
    {
    get;
    set;
    }

    string Description
    {
    get;
    set;
    }

    string Source
    {
    get;
    set;
    }
    }

    [ComVisible(true)]
    [Guid("1EB825A6-ED23-4b17-9C62-0D54DDCD3DD9")]
    [ClassInterface(ClassInterfaceType.None)]
    public class ErrorObject : _ErrorObject
    {
    private int _errorCode;
    private string _message;
    private string _description;
    private string _source;

    public int ErrorCode
    {
    get
    {
    return _errorCode;
    }
    set
    {
    _errorCode = value;
    }
    }

    public string Message
    {
    get
    {
    return _message;
    }
    set
    {
    _message = value;
    }
    }

    public string Description
    {
    get
    {
    return _description;
    }
    set
    {
    _description = value;
    }
    }

    public string Source
    {
    get
    {
    return _source;
    }
    set
    {
    _source = value;
    }
    }

    public ErrorObject()
    {
    }
    }



    Usage:

    Private Sub Form_Load()
    Dim myVb6Error As New ErrObject


    Dim err As New ErrorObject
    err.ErrorCode = myVb6Error.ErrorCode
    err.Message = "My error"
    err.Description = myVb6Error.Description
    err.Source = myVb6Error.Source

    Errorob.ShowError (err)
    End Sub




  • Tuhin

    Hi,

    Thanks! It works but now I have a small problem. In my class file (C#) there is a method defined as follows

    public void ShowError(Exception err)
    {

    ///code to be executed

    }

    Now in my vb code I have given as

    On Error GoTo Errorline

    'the code to be executed
    Errorline:

    Dim obj As New ErrObject
    Dim Errorob As Error.Test
    Set Errorob = New Error.Test
    Set obj = Err
    Errorob.ShowError (obj)
    End Sub

    Now I'm getting the error at Errorob.ShowError (obj) saying "Invalid procedure call or argument". How can I make this possible. Is it because the object "obj" in VB is not compatible with the "err" object in C#. I want to do this through the dll only. Please provide me with a solution.

    Thanks & Regards,
    Frenz


  • Raphael Londner

    Frenz,

    The problem is that you are mixing up two different object types. The C# function is looking for a SystemException object, and the VB6 app is sending the VBA.Err object.

    What you need to do is to open the References dialog in VB6 by going to Project | References. Find 'mscorlib.dll' and check it, and click OK. In the VB6 code declare an object as a SystemException (which is a new type introduced by mscorlib.dll) and pass that.

    Dim Exception As SystemException
    Set Exception = New SystemException

    Errorob.ShowError(Exception)

    This should help. You don't get the IntelliSense autocompletion with the SystemException object, so you'll have to refer to the documentation to use its properties and methods.

    I hope this helps.

    digimonk

  • pdesai

    Yep! it's possible... You just have to create your C# class and compile it to a dll, then use regasm.exe for assembly registration and typelib generation...

    regasm YourAssembly.dll /tlb:YourAssembly.tlb

    This command makes the appropriate registry entries and generates a typelib (.tlb) from the .NET assembly so that it can be referenced from VB 6 and just use the referenced library just as you would use a COM object.

    HTH,

  • Using a C# dll in VB6