Do I need to free resources when calling SHCreateI

2019-07-12 02:54发布

问题:

I have the following code:

[DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
static extern void SHCreateItemFromParsingName(
[In][MarshalAs(UnmanagedType.LPWStr)] string pszPath,
[In] IntPtr pbc,
[In][MarshalAs(UnmanagedType.LPStruct)] Guid iIdIShellItem,
[Out][MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] out IShellItem iShellItem);


[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
interface IShellItem
{
}

To use this function:

IShellItem iShellItem = null;
Guid iIdIShellItem = new Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe");
SHCreateItemFromParsingName(sourceFile, IntPtr.Zero, iIdIShellItem, out iShellItem);

It works great, and I use it to get OS icon bitmaps later. My question is:

Do I need to free any resource? Could some tell me how?

Thanks in advance.

回答1:

IShellItem is a COM interface. Your declaration of it with the [ComImport] attribute ensures that the CLR creates a wrapper for it, an RCW (Runtime Callable Wrapper). Which is a managed wrapper around the native interface. RCWs behave just like regular .NET classes, they are managed by the garbage collector. With the exact same rules, sooner or later the garbage collector sees that your program doesn't have a reference to the RCW anymore. And puts in on the finalizer queue. The RCW's finalizer calls the IUnknown::Release() method and that destroys the native COM object.

In other words, it is automatic, just like .NET objects.

You technically can hurry it along by calling Marshal.ReleaseComObject(). It is roughly equivalent to IDisposable.Dispose(), a method that RCWs don't implement because it is so difficult to see indirect references on COM interface pointers. Using it is a good way to shoot your foot though, with misbehavior somewhere between it having no effect at all because you missed a reference and crashing your program with "COM object that has been separated from its underlying RCW cannot be used". Just not needed for a simple shell item reference, especially when you consume it quickly.