2

我正在尝试使用 VB.NET 在 Visual Studio 2012 中为 .NET 用户控件创建 ActiveX 控件包装器。

作为参考,我对将其嵌入网页不感兴趣,我想在 VB6 应用程序中使用它。

我按照此处概述的步骤进行操作

  • 创建了一个类库
  • 添加了用户控件
  • 勾选“使程序集 COM 可见”
  • 勾选“注册 COM 互操作”

  • 然后我添加了com注册。我的用户控件类现在看起来像这样:

    <ProgId("TestAx.DummyControl")>
    <ClassInterface(ClassInterfaceType.AutoDispatch)>
    
    Public Class DummyControl
    
    <ComRegisterFunction>
    Public Shared Sub RegisterClass(key As String)
        Dim sb As New StringBuilder(key)
        sb.Replace("HKEY_CLASSES_ROOT\", "")
    
        ' Open the CLSID\{guid} key for write access  
        Dim k As RegistryKey = Registry.ClassesRoot.OpenSubKey(sb.ToString(), True)
    
        Dim ctrl As RegistryKey = k.CreateSubKey("Control")
        ctrl.Close()
    
        ' Next create the CodeBase entry - needed if not string named and GACced.  
        Dim inprocServer32 As RegistryKey = k.OpenSubKey("InprocServer32", True)
    inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase)
    inprocServer32.Close()
    
        k.Close()
    End Sub
    
    <ComUnregisterFunction>
    Public Shared Sub UnregisterClass(key As String)
        Dim sb As StringBuilder = New StringBuilder(key)
        sb.Replace("HKEY_CLASSES_ROOT\", "")
    
        ' Open HKCR\CLSID\{guid} for write access  
        Dim k As RegistryKey = Registry.ClassesRoot.OpenSubKey(sb.ToString(), True)
    
        ' Delete the 'Control' key, but don't throw an exception if it does not exist  
        If k Is Nothing Then Return
        k.DeleteSubKey("Control", False)
    
        ' Next open up InprocServer32  
        Dim inprocServer32 As RegistryKey = k.OpenSubKey("InprocServer32", True)
    
        ' And delete the CodeBase key, again not throwing if missing   
        inprocServer32.DeleteSubKey("CodeBase", False)
    
        ' Finally close the main key   
        inprocServer32.Close()
        k.Close()
    End Sub
    
    End Class
    

我可以在 VB6 中看到程序集,Project>References但当我右键单击工具箱并选择Components. 如果我尝试浏览到此处的 tlb,我会收到一条错误消息:“文件 xxxx 无法注册为 ActiveX 组件”

我错过了什么?

4

2 回答 2

0

我找到了一个解决方案,即安装 VS2010 并安装 Forms Interop Toolkit。然后我创建了一个新项目并选择了 ActiveX 控件。

之后,我设法将我的项目升级到 VS2012 并毫无问题地编译它。

这并不理想,但只需要做一次。

于 2013-07-29T16:57:13.820 回答
-1

我有一个类似的问题......我处理它的方式是将.Net代码放置一个命令行函数并从VB6调用它。

于 2013-07-29T12:46:07.430 回答