CoFreeUnusedLibraries (ole32)
Last changed: anonymous

.
Summary

Unloads any DLLs that are no longer in use and whose unload delay has expired.

     unloadDelay - The delay in milliseconds between the time that the DLL
     has stated it can be unloaded until it becomes a candidate to unload.
     Setting unloadDelay=INFINITE uses the system default delay (10 minutes).
     Setting unloadDelay=0 forces the unloading of any DLLs without any delay.
     reserved - Reserved for future use; must be zero.

C# Signature:

[DllImport("ole32.dll")]
public static extern void CoFreeUnusedLibrariesEx(UInt32 unloadDelay, UInt32 reserved);

VB Signature:

Declare Function CoFreeUnusedLibrariesEx Lib "ole32.dll" (TODO) As TODO

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Setting the delay too short (or zero) can have unintended consequences, as some DLL's will return control before they have finished cleaning up resources, or they will continue executing in the background.

Tips & Tricks:

Please add some!

Sample Code:

public static void FreeDllsNow(UInt32 pause)

{

    try
    {
        CoFreeUnusedLibrariesEx(pause, (UInt32)0);
        System.Threading.Thread.Sleep((int)pause);
        CoUninitialize();
    }
    catch (Exception ex)
    {
        string exStr = ex.ToString();
        //Console.WriteLine(exStr);
    }

}

Documentation