第一步:
KSDLL2.idl文件中
dispinterface _DKSDLL2{ properties: methods: [id(1)] void CheckIt(); [id(2)] BSTR GetIt();}; |
第二步:
KSDLL2Ctrl.h文件中
protected: void CheckIt(void); BSTR GetIt(); |
第三步:
KSDLL2Ctrl.cpp文件中
// 调度映射BEGIN_DISPATCH_MAP(CKSDLL2Ctrl, COleControl) DISP_FUNCTION_ID(CKSDLL2Ctrl, "CheckIt", dispidCheckIt, CheckIt, VT_EMPTY, VTS_NONE) DISP_FUNCTION_ID(CKSDLL2Ctrl, "GetIt", dispidGetIt, GetIt, VT_BSTR, VTS_NONE)END_DISPATCH_MAP() |
第四步:
KSDLL2Ctrl.cpp文件中
BSTR CKSDLL2Ctrl::GetIt(){ CString strmac; strmac="12345123451234512345"; return strmac.AllocSysString();} |
第五步:
html文件中
var obj = document.getElementById("Object1");var result = obj.GetIt();alert(result); |
再举例进行说明一下:
在OCX项目中的C×××Ctrl类的.h中定义
// Dispatch maps
//{{AFX_DISPATCH(CSuperHolterCtrl)
// NOTE - ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()
//作用:获取患者的PDF报告文件名称
afx_msg BSTR GetReportPDFFileName();
// Event maps
//{{AFX_EVENT(CSuperHolterCtrl)
// NOTE - ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_EVENT
DECLARE_EVENT_MAP()
public:
enum {
//{{AFX_DISP_ID(CSuperHolterCtrl)
// NOTE: ClassWizard will add and remove enumeration elements here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_DISP_ID
dispidSetPatientData = 1L,
eventidFinishReport = 2L,
dispidGetReportPDFFileName = 3L,
dispidGetReportXMLFileName = 4L
};在C×××Ctrl类的.cpp中定义
/////////////////////////////////////////////////////////////////////////////
// Dispatch map
BEGIN_DISPATCH_MAP(C×××Ctrl, CActiveXDocControl)
//{{AFX_DISPATCH_MAP(CSuperHolterCtrl)
// NOTE - ClassWizard will add and remove dispatch map entries
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_DISPATCH_MAP
//作用:获取患者的PDF报告文件名称
DISP_FUNCTION_ID(C×××Ctrl, "GetReportPDFFileName", dispidGetReportPDFFileName, GetReportPDFFileName, VT_BSTR, VTS_NONE)
END_DISPATCH_MAP()//作用:获取患者的PDF报告文件名称
BSTR C×××Ctrl::GetReportPDFFileName()
{
CString reportPDFFileName;
CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
CHolterDoc*pDoc = (CHolterDoc *)pFrame->GetActiveDocument();
reportPDFFileName = pDoc->m_PDFFileName;
return reportPDFFileName.AllocSysString();
}
在引用Ocx控件的地方,其IDispatch 包装器类中,定义
// 操作
public:
// _DSuperHolter
// Functions
//
//作用:获取患者的PDF报告文件名称
CString GetReportPDFFileName()
{
CString reportPDFFileName;
InvokeHelper(0x3, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&reportPDFFileName, NULL);//适合无输入参数,有返回值的调度接口
return reportPDFFileName;
}