CoInitializeEx (ole32)
Last changed: -5.202.143.191

.
Summary
CoInitializeEx initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required. Values for the dwCoInit parameter are taken from the COINIT enumeration. However, since pinvoke is a dotNET construct you should be aware that dotNET already does a COM initialization and therefore calling a CoInitializeEx function most likely will not do what you expect. This problem occurs when trying to instantiate a COM object from within dotNET where the COM objects threading model is different from dotNETs. Search on Common Language Runtime or CLR and COINIT_APARTMENTTHREADED to find posts on this issue.

C# Signature:

    /// <summary>Determines the concurrency model used for incoming calls to objects created by this thread. This concurrency model can be either apartment-threaded or multi-threaded.</summary>
    public enum CoInit
    {
        /// <summary>
        /// Initializes the thread for apartment-threaded object concurrency.
        /// </summary>
        MultiThreaded = 0x0,

        /// <summary>
        /// Initializes the thread for multi-threaded object concurrency.
        /// </summary>
        ApartmentThreaded = 0x2,

        /// <summary>
        /// Disables DDE for OLE1 support.
        /// </summary>
        DisableOle1Dde = 0x4,

        /// <summary>
        /// Trade memory for speed.
        /// </summary>
        SpeedOverMemory = 0x8
    }

    /// <summary>Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required.</summary>
    /// <param name="reserved">This parameter is reserved and must be NULL.</param>
    /// <param name="coInit">The concurrency model and initialization options for the thread. Values for this parameter are taken from the CoInit enumeration. Any combination of values can be used, except that the ApartmentThreaded and MultiThreaded flags cannot both be set. The default is MultiThreaded.</param>
    /// <returns>If function succeeds, it returns S_OK. Otherwise, it returns an error code.</returns>
    [DllImport(@"ole32.dll")]
    internal static extern int CoInitializeEx(IntPtr reserved, CoInit coInit);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

// Note: PreserveSig=false allows .NET interop to handle processing the returned HRESULT and throw an exception on failure

          public enum CoInit
          {
             MultiThreaded = 0x0,
             ApartmentThreaded = 0x2,
             DisableOLE1DDE = 0x4,
             SpeedOverMemory = 0x8
          }

        [DllImport( "Ole32.dll", ExactSpelling=true, EntryPoint="CoInitializeEx", CallingConvention=CallingConvention.StdCall, SetLastError=false, PreserveSig=false ) ]
        public static extern void CoInitializeEx(IntPtr pvReserved, CoInit coInit);

Alternative Managed API:

Do you know one? Please contribute it!

Documentation