Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

CoInitializeEx (ole32)
 
.
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:

    /// <returns>If function succeeds, it returns 0(S_OK). Otherwise, it returns an error code.</returns>
    [DllImport("ole32.dll", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
    public static extern int CoInitializeEx(
        [In, Optional] IntPtr pvReserved,
        [In]  COINIT dwCoInit //DWORD
        );

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 : uint //tagCOINIT
    {
        COINIT_MULTITHREADED = 0x0, //Initializes the thread for multi-threaded object concurrency.
        COINIT_APARTMENTTHREADED = 0x2, //Initializes the thread for apartment-threaded object concurrency
        COINIT_DISABLE_OLE1DDE = 0x4, //Disables DDE for OLE1 support
        COINIT_SPEED_OVER_MEMORY = 0x8, //Trade memory for speed
    }

Alternative Managed API:

Do you know one? Please contribute it!

    /// <returns>If function succeeds, it returns 0(S_OK). Otherwise, it returns an error code.</returns>
    [DllImport("ole32.dll", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
    public static extern HRESULT CoInitializeEx(
        [In, Optional]  LPVOID pvReserved,
        [In]  COINIT dwCoInit //DWORD
        );

Documentation

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

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions