|
怎样利用MFC创建VB能引用的DLL方法 我们在使用MFC写DLL文件后,无法在VB工程(Project)的References中查找到并引用它,即无法使用Dim s As New class 或 Dim s As class, Set s = New class方式使用。 下面解释怎样利用MFC创建VB能引用的DLL方法,其步骤: (1)选择MFC AppWizard (dll)创建新工程;(比如:DlgTest.dll) (2)选择Regular DLL with MFC statically linked 并选择Automation; (3)创建MFC Class 其Base class为CCmdTarget且选中Automation;(假设类名为Dlg) (4)修改DlgTest.def文件中的:LIBRARY "DlgTest.dll" (修改)DllUnregisterServer PRIVATE(添加); (5)在DlgTest.odl文件中添加dll文件在vb引用中浏览显示的内容(可选) [uuid(........), version(1.0), helpstring("My test contents ...")] (6)在构造和析构函数内添加下列代码: CDlg::Dlg() CDlg::~Dlg() { { AfxOleLockApp(); AfxOleUnlockApp(); } } (7)注释掉OnFinalRelease函数中的CCmdTarget::OnFinalRelease(),添加delete this; (8)在Dlg.cpp文件中添加下列代码(有下划线、紫色的代码) BEGIN_MESSAGE_MAP(CDlg, CCmdTarget) ............ END_MESSAGE_MAP() IMPLEMENT_OLECREATE(CDlg, "DlgTest.Dlg", ClassGuid Value) ClassGuid Value为.odl文件中的coclass处的uuid值 (9)在Dlg.h文件中,添加下列下划线、紫色的代码 DECLARE_MESSAGE_MAP() DECLARE_OLECREATE(CDlg) |