Html页面文件中调用OCX控件中带有BSTR参数的函数出错的问题(OCX控件由MFCActiveXControlWizard创建)
描述:
相关代码如下:
OCX控件对应的.odl文件
/******************
OCXTest_CalcPi.odl
控件有4个函数CalcPi,TestFunc,TestFunc2,AboutBox
,一个put属性Digits
*******************/
#include <olectl.h>
#include <idispids.h>
[ uuid(5424A731-F67B-446E-B584-3D0BAB54FEF3), version(1.0),
helpfile("OCXTest_CalcPi.hlp"),
helpstring("OCXTest_CalcPi ActiveX Control module"),
control ]
library OCXTEST_CALCPILib
{
importlib(STDOLE_TLB);
importlib(STDTYPE_TLB);
// Primary dispatch interface for COCXTest_CalcPiCtrl
[ uuid(2481EC56-2EBA-44A8-B7A2-1A7E27245B52),
helpstring("Dispatch interface for OCXTest_CalcPi Control"), hidden ]
dispinterface _DOCXTest_CalcPi
{
properties:
// NOTE - ClassWizard will maintain property information here.
// Use extreme caution when editing this section.
//{{AFX_ODL_PROP(COCXTest_CalcPiCtrl)
[id(1), bindable] long Digits;
[id(0)] long _Digits;
//}}AFX_ODL_PROP
methods:
// NOTE - ClassWizard will maintain method information here.
// Use extreme caution when editing this section.
//{{AFX_ODL_METHOD(COCXTest_CalcPiCtrl)
[id(2)] boolean CalcPi(BSTR* pbstrPi);
[id(3)] BSTR TestFunc();
[id(4)] BSTR TestFunc2(BSTR Text);
//}}AFX_ODL_METHOD
[id(DISPID_ABOUTBOX)] void AboutBox();
};
// Event dispatch interface for COCXTest_CalcPiCtrl
[ uuid(AFDD0CC3-BAD8-4BF9-9BC3-D35655547BF0),
helpstring("Event interface for OCXTest_CalcPi Control") ]
dispinterface _DOCXTest_CalcPiEvents
{
properties:
// Event interface has no properties
methods:
// NOTE - ClassWizard will maintain event information here.
// Use extreme caution when editing this section.
//{{AFX_ODL_EVENT(COCXTest_CalcPiCtrl)
//}}AFX_ODL_EVENT
};
// Class information for COCXTest_CalcPiCtrl
[ uuid(FD931B4C-3FBC-40AE-AC1E-4150912C712F),
helpstring("OCXTest_CalcPi Control"), control ]
coclass OCXTest_CalcPi
{
[default] dispinterface _DOCXTest_CalcPi;
[default, source] dispinterface _DOCXTest_CalcPiEvents;
};
//{{AFX_APPEND_ODL}}
//}}AFX_APPEND_ODL}}
};
/************************
COCXTest_CalcPiCtrl.cpp中
TestFunc()和TestFunc2()两个函数的实现代码
*************************/
BSTR COCXTest_CalcPiCtrl::TestFunc()
{
CString strResult;
// TODO: Add your dispatch handler code here
strResult=OLESTR("asdfasdfasdf");
return strResult.AllocSysString();
}
BSTR COCXTest_CalcPiCtrl::TestFunc2(BSTR Text)
{
CString strResult;
// TODO: Add your dispatch handler code here
strResult=OLESTR("qweqrqwerqwer");
return strResult.AllocSysString();
}
/*******************
测试此控件的html文件代码
*******************/
<HTML>
<HEAD>
<TITLE>test page for object OCXTest_CalcPi</TITLE>
</HEAD>
<BODY>
<object classid="clsid:FD931B4C-3FBC-40AE-AC1E-4150912C712F"
name=objocx
height="0" width="0" >
</object>
<script language= "VBScript">
dim RetTxt2
dim pText AS String
pText = "111"
RetTxt2 = objocx.TestFunc2 pText
document.write "Return Text by TestFunc2 is: " & RetTxt2
'following commented code can run correctly
'dim RetTxt
'RetTxt = objocx.TestFunc
'document.write "Return Text by TestFunc is: " & RetTxt
</script>
</BODY>
</HTML>
现象:
Html文件中调用OCXTest_CalcPi中的TestFunc函数,运行正常,
页面显示"Return Text by TestFunc is:asdfasdfasdf"字符串;
而调用带有BSTR参数的TestFunc2函数,页面却没有任何反映。
解决方案1:
RetTxt2 = objocx.TestFunc2 pText改成RetTxt2 = objocx.TestFunc2(pText)呢,vb要有返回值应该加括号的,vbscript应该也是这样吧
解决方案2:把BSTR换成LPCTSTR 就可以了