我正在尝试使用 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 组件”
我错过了什么?