Search code examples
c#vb.netinteropclrcom-interop

How VB6 host .net 2.0


We want to call some .net Assemblies from VB6. I know we can register the .net Assemblies to COM by Regasm command and then call it from VB6. But now we want to host the CLR 2.0 in VB6.

I know we can call CorRuntimeHost class to host the CLR. But MSDN said it is obselete. After googled some days, I found many people use CorBindToRuntimeEx to host CLR. but all these samples are based on C/C++. Sorry I did not find any sampels using VB6.

So does anyone know how to use VB6 to host .net framework 2.0. Except the CorRuntimeHost class, is there any another way to host .net framework in VB6?


Solution

  • Private Sub LoadLib()
     Dim unknown As IUnknown
     Dim domain As AppDomain
     Dim handle As ObjectHandle
     Dim path As String
    
     On Local Error GoTo OOPS
    
     path = GetDLLPath()
    
     Set m_host = New CorRuntimeHost
     With m_host
        .Start
        .GetDefaultDomain unknown
     End With
    
     Set domain = unknown
     Set handle = domain.CreateInstanceFrom(path, "STHRest.CXlService")
     Set m_lib = handle.Unwrap
    
    OOPS:
    If Err.Number <> 0 Then
        m_err = Err.Description
        Call RemoveCLR
    End If
    
    On Local Error GoTo 0
    Exit Sub
    

    End Sub