0

I'm reading and writing Structured Storage files from C#. To open the file i call

IStorage StorageInterface;

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

This works and I can access the file. I believe I need to call Release() on the Storage object to close the file. However, I don't know how to get to Release since its implemented on IUnknown.

Can I cast StorageInterface to an object that implements IUnknown and call it that way?

thanks,

john

John Mott
  • 119
  • 1
  • 12
  • does it Inherit from IDisposible? if not then just set the Object = null, or you can try something like this to test it `((IDisposable)YourObject).Dispose();` – MethodMan Oct 17 '16 at 17:49
  • It looks like IStorage doesn't inherit from IDisposable :( – John Mott Oct 18 '16 at 01:16

1 Answers1

3

It is derived from IUnknown. Every COM object is derived from IUnknown. Just call

 StorageInterface->Release();

Maybe I was hasty. I missed the C# part... That's how you'd do it in C++.

In C#, you should be able to call like this.

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

Check the spelling...it's from memory.

Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29