来自:http://www.vckbase.com/index.php/video/listview/fid/2/sid/15
本节内容:
1、用ATL制作简单对象(Simple Object)
2、在网页中测试简单对象
3、在MFC中测试简单对象
4、在C#中测试简单对象
1、用ATL制作简单对象(Simple Object)
项目创建向导
SysFreeString(m_bstrDescription);
//释放旧的字符串
SysFreeString(*pVal);
//申请新的字符串
*pVal = SysAllocStringLen(m_bstrDescription,SysStringLen(m_bstrDescription));
return S_OK;
//释放旧的字符串
SysFreeString(m_bstrDescription);
//申请新的字符串
m_bstrDescription = SysAllocStringLen(newVal,SysStringLen(newVal));
return S_OK;
其中的CLSID要从你们的idl文件中复制过来,不能用我这边写的CLSID。
<script>
functionOnTestAddMethord()
{
//
}
functionOnReadDescriptionProp()
{
//
}
functionOnWriteDescriptionProp()
{
//
}
</script>
<buttontype="button"οnclick="OnTestAddMethord()">测试简单对象的加法方法</button>
<buttontype="button"οnclick="OnReadDescriptionProp()">读取简单对象的描述属性</button>
<buttonbyte="button"οnclick="OnWriteDescriptionProp()">写入简单对象的描述属性</button>
function OnTestAddMethord()
{
varspo =document.getElementById("HelloSimpleObjectTest");
varslr =spo.SumLong(9,99);
alert(slr);
}
function OnReadDescriptionProp()
{
varspo =document.getElementById("HelloSimpleObjectTest");
vardesp =spo.Description;
alert(desp);
}
function OnWriteDescriptionProp()
{
varspo =document.getElementById("HelloSimpleObjectTest");
spo.Description = "我已通过网页修改了简单对象的描述属性!这是新描述!";
alert("修改属性完成!点击中间的按钮测试!");
}
//简单对象的测试
HRESULT hr = E_FAIL;
hr = CoInitialize(NULL); //使用com组件对象之前要添加这一句,这句一般可以放在CMFCTestSection4Demo1App::InitInstance()里
if (SUCCEEDED(hr))
{
IHelloSimpleObject *pIHelloSimpleObject = NULL;
hr = CoCreateInstance(CLSID_HelloSimpleObject, NULL, CLSCTX_INPROC_SERVER, IID_IHelloSimpleObject, (void**)&pIHelloSimpleObject);
if (SUCCEEDED(hr))
{
//加法方法测试
LONG lSum = 0;
hr = pIHelloSimpleObject->SumLong(9, 99, &lSum);
//读描述属性
BSTR bstrDescriptionA = SysAllocString(L"");
hr = pIHelloSimpleObject->get_Description(&bstrDescriptionA);
SysFreeString(bstrDescriptionA);
bstrDescriptionA = NULL;
//写描述属性
BSTR bstrDescriptionB = SysAllocString(L"我运行在MFC里,我是新的描述属性!");
hr = pIHelloSimpleObject->put_Description(bstrDescriptionB);
SysFreeString(bstrDescriptionB);
bstrDescriptionB = NULL;
//读描述属性
BSTR bstrDescriptionC = SysAllocString(L"");
hr = pIHelloSimpleObject->get_Description(&bstrDescriptionC);
SysFreeString(bstrDescriptionC);
bstrDescriptionC = NULL;
}
pIHelloSimpleObject->Release(); //不使用这个接口,引用计数减一
}
CoUninitialize(); //不使用com组件对象时添加这一句,这句一般可以放在CMFCTestSection4Demo1App::ExitInstance()里。
//注:输出BSTR指可以用 MessageBoxW(NULL, bstrDescriptionA, NULL, 0);