VB6如何托管.net 2.0

时间:2014-12-14 03:59:43

标签: c# vb.net interop clr com-interop

我们想从VB6调用一些.net程序集。 我知道我们可以通过Regasm命令将.net程序集注册到COM,然后从VB6调用它。 但现在我们想在VB6中托管CLR 2.0。

我知道我们可以调用CorRuntimeHost类来托管CLR。但是MSDN表示这很有道理。经过几天的谷歌搜索后,我发现很多人使用CorBindToRuntimeEx来托管CLR。但所有这些样本都基于C / C ++。对不起,我没有找到任何使用VB6的样本。

所以有人知道如何使用VB6来托管.net framework 2.0。除了CorRuntimeHost类之外,还有其他方法可以在VB6中托管.net框架吗?

4 个答案:

答案 0 :(得分:0)

CLR主机必须是非托管的本机应用程序。使用VB6无法直接承载CLR

唯一可行的方法是编写一个公开__declspec C函数的C ++ DLL,并使用VB6应用程序中的这些函数。 Someone did it for FoxPro, so there is some hope

即使假设它运行良好(我不知道两个"片段中的COM层(VB6 VM和主机)是否可以很好地一起播放)你仍然需要在C ++中完成它。 那么......听听John Saunders的评论。不要这样做,选择另一条道路。

答案 1 :(得分:0)

我终于找到了答案。我在这里发布了它,因为它可能是其他人使用的。

您可以在excel VBA中引用mscorlib.tlb,然后:

Dim ap as ApplicationDomain, apm As mscorlib.AppDomainManager, result as object
Set apm = New mscorlib.AppDomainManager
Set m_domain = apm.CreateDomain("kissingerDomain", Nothing, Nothing)
set object = m_domain.CreateInstanceFrom(DLL_Path, typeName).Unwrap

现在你可以调用对象方法,哈哈。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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