在 C# 中,如何在实现 IStorage 的返回对象上调用 Release?

From C#, how do I call Release on a returned object that implements IStorage?

我正在使用 C# 读写结构化存储文件。打开我调用的文件

IStorage StorageInterface;

int result = StgOpenStorage(filename, null, STGM.READWRITE | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out StorageInterface);

这有效,我可以访问该文件。我相信我需要在 Storage 对象上调用 Release() 来关闭文件。但是,我不知道如何去发布,因为它是在 IUnknown 上实现的。

我可以将 StorageInterface 转换为实现 IUnknown 的对象并以这种方式调用它吗?

谢谢,

约翰

它来源于IUnknown。每个 COM 对象都派生自 IUnknown。直接打电话

 StorageInterface->Release();

可能是我太草率了。我错过了 C# 部分......这就是你在 C++ 中的做法。

在C#中,你应该可以这样调用。

System.Runtime.InteropServices.Marshal.ReleaseComObject(StorageInterface);

检查拼写...凭记忆。