";无法访问ActiveX控件类型库“”;MFC设计器中出现错误
本文关键字:MFC 错误 quot 访问 ActiveX 类型 控件 | 更新日期: 2023-09-27 18:20:19
问题
在基于MFC对话框的应用程序中,在Visual Studio设计器中尝试为ActiveX控件添加变量时(通过右键单击->"添加变量"或通过class向导),我遇到了"无法访问ActiveX控件类型库"错误。我需要它来处理这个ActiveX控件的方法和事件,它是从windows窗体控件创建的,并在系统(Windows7)中注册。
我拥有的
以下是简化的C#COM实现(一个从MFC端调用的方法和一个通知MFC任何事情的事件):
[ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IWinFormControl
{
[DispId(1)]
string MethodTest(string s);
}
[ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IWinFormControlEvents
{
[DispId(2)]
void SimpleEvent(string s);
}
[ComVisible(true),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(IWinFormControl)),
ComSourceInterfaces(typeof(IWinFormControlEvents))]
public partial class WinFormControlImpl : UserControl, IWinFormControl
{
public delegate void SimpleEventType(string s);
public event SimpleEventType SimpleEvent;
public WinFormControlImpl()
{
this.InitializeComponent();
}
private void Button_Click(object sender, EventArgs e)
{
if (this.SimpleEvent != null)
{
this.SimpleEvent("TESTEVENT");
}
}
public string MethodTest(string s)
{
MessageBox.Show("TESTMETHOD " + s);
}
// ... ComRegisterFunction and ComUnregisterFunction
}
检查了"Register for COM interop"项目标志,在构建组件成功添加到注册表后,我可以使用TstCon32实用程序对其进行测试,并在那里调用MethodTest。
在MFC项目中,我在设计视图中打开对话框面板,右键单击并选择"插入ActiveX控件",然后成功找到我的ActiveX控件并将其插入对话框面板。应用程序运行时,我看到了包含所有内容的ActiveX。但我不能为这个控件创建变量。
我尝试了什么
研究给了我两个通用的解决办法。首先是忘记COM,将对.NET项目的引用添加到MFC项目中,并使用CWinFormsControl
和/CLR为用户控件创建本机C++包装器。这不适合我的任务。
另一种可能的解决方案是手动为ActiveX控件创建变量,但我找不到足够的示例来实现这一点。我发现的最接近的黑客是有问题的,在CWnd
变量中创建的ActiveX对象和该COM的功能通过接口访问,但我不知道如何放置该变量信息显示对话框面板的视觉内容。
问题是:如何在基于MFC对话框的窗口中为ActiveX组件(在C#中创建)创建变量?
希望它能有所帮助:
if (m_pWndFramework == NULL)// CWnd* m_pWndFramework;
{
m_pWndFramework = new CWnd;
CRect rcRange; //set the display area
rcRange.left = 0;
rcRange.top = 0;
rcRange.right = 500;
rcRange.bottom = 500;
//get the ID of the user control
m_pWndFramework = GetDlgItem(IDC_TESTT1);
//if(bCreate)
{
IUnknown * pUn = m_pWndFramework->GetControlUnknown();
if(pUn)
{
pUn->QueryInterface(IID_IDispatch,(void **)&m_pFramework); //m_pFramework
bResult = TRUE;
}
}
}
m_pFramework->messageShowClose();