C# 实现IUnknown接口(2)

本文介绍了IUnknown接口在.NET中的使用,特别关注了QueryInterface、AddRef和Release方法,以及.NET内置的垃圾回收机制如何替代COM的引用计数管理。
摘要由CSDN通过智能技术生成

IUnKnown接口定义

[ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown),

Guid("00000000-0000-0000-C000-000000000046")]

public interface IUnknown

{

[PreserveSig]

int QueryInterface(ref Guid riid, out IntPtr ppvObject);

[PreserveSig]

uint AddRef();

[PreserveSig]

uint Release();

}
Class1定义
class Class1 : IUnknown

{

public int QueryInterface(ref Guid riid, out IntPtr ppvObject)

{

throw new Exception("The method or operation is not implemented.");

}


public uint AddRef()

{

throw new Exception("The method or operation is not implemented.");

}



public uint Release(

{

throw new Exception("The method or operation is not implemented.");

}

}



或者

class Class1 : IUnknown

{

int IUnknown.QueryInterface(ref Guid riid, out IntPtr ppvObject)

{

throw new Exception("The method or operation is not implemented.");

}



uint IUnknown.AddRef()

{

throw new Exception("The method or operation is not implemented.");

}



uint IUnknown.Release()

{

throw new Exception("The method or operation is not implemented.");

}

}
主程序
IUnknown Class1 = new Class1();

在创建的时候,并不会调用AddRef()

.Net下不是这么玩的。
.Net已经有了垃圾回收机制,不需要使用COM的引用计数。
当一个类被.Net的发表为COM对象,.Net会自动帮你实现IUnknown,并帮你管理引用计数。

 
  1. [assembly: ComVisible(true)] //<--

  2. [InterfaceType( ComInterfaceType.InterfaceIsIUnknown)] //不写也可以,会默认为dual

  3. interface A

  4. {

  5. //作为COM发表后,A已经继承IUnknown了。

  6. }

woshi_ziyu 2011-12-14IUnknown::AddRef method

Increments the reference count for an interface on an object. This method should be called for every new copy of a pointer to an interface on an object.

Syntax
ULONG AddRef();

Parameters
This method has no parameters.
Return value

The method returns the new reference count. This value is intended to be used only for test purposes.

Remarks
Objects use a reference counting mechanism to ensure that the lifetime of the object includes the lifetime of references to it. You use AddRef to stabilize a copy of an interface pointer. It can also be called when the life of a cloned pointer must extend beyond the lifetime of the original pointer. The cloned pointer must be released by calling IUnknown::Release.

The internal reference counter that AddRef maintains should be a 32-bit unsigned integer.

Notes to Callers
Call this method for every new copy of an interface pointer that you make. For example, if you are passing a copy of a pointer back from a method, you must call AddRef on that pointer. You must also call AddRef on a pointer before passing it as an in-out parameter to a method; the method will call IUnknown::Release before copying the out-value on top of it.

  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注VB编程开发20年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值