com组件接口继承的实现
这是根据”dr. gui and atl” 的文章自己做的例子/doc/">程序,主要是在原有接口上继承新的接口,这样在保留原接口的基础上可以扩展新的功能. 字串4
功能介绍: 字串6
1.创建calculate/com/">组件,添加ifun接口(注意:本例我们在attributes中我们选择apartment,custom,yes).在ifun接口中添加函数add实现两数相加的功能(参数为[in] long n1, [in] long n2, [out, retval] long *pval).代码如下: 字串1
stdmethodimp ccalculate::add(long n1, long n2, long *pval) 字串5
{
字串5
// todo: add your implementation code here
*pval = n1 + n2;
return s_ok; 字串7
}
2.添加ifun2接口,继承ifun接口.步骤如下: 字串1
(1)修改.idl文件: 字串2
在 字串2
interface ifun : iunknown
{ 字串4
[helpstring("method add")] hresult add([in] long n1, [in] long n2, [out, retval] long *pval); 字串3
};
下添加代码: 字串2
[
字串1
object,
字串6
uuid(b9d75722-1950-405b-8bb6-9788a6db1db2), //是接口ifun2的标志数,可由vs的guidgen.exe产生
字串3
helpstring("ifun2 interface"),
字串4
pointer_default(unique) 字串5
]
字串2
interface ifun2 : ifun 字串3
{
字串3
}; 字串8
同时将末尾coclass calculate函数改为
字串4
coclass calculate
字串1
{
字串9
interface ifun; 字串8
[default] interface ifun2; 字串3
}; 字串9
(2) 修改calculate.h文件 字串8
将
字串3
class atl_no_vtable ccalculate :
public ccomobjectrootex,
字串2
public ccomcoclass, 字串8
public ifun
字串8
改为
字串8
class atl_no_vtable ccalculate : 字串3
public ccomobjectrootex, 字串6
public ccomcoclass,
字串4
public ifun2 字串8
在 字串8
begin_com_map(ccalculate) 字串8
com_interface_entry(ifun) 字串4
end_com_map() 字串3
中添加 字串5
com_interface_entry(ifun2) 字串5
(3) 在ifun2接口中添加sub函数,方法同添加add函数不再重复,只是实现时改为
字串2
*pval = n1 - n2; // 返回两数差. 字串3
(4) 编写客户端代码,调用ifun2接口的add和sub函数. 字串7
开发环境: vc6 windows 2000/xp
测试环境:windows 2000 windowsxp


文章评论
共有 0位编程爱好者发表了评论 查看完整内容