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

CoCreateInstance (ole32)
 
.
Summary
Summary

C# Signature:

[DllImport("ole32.dll", CharSet=CharSet.Unicode, ExactSpelling=true, PreserveSig=false)]
static extern void CoCreateInstanceEx(
   [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
   [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,
   CLSCTX dwClsCtx,
   IntPtr pServerInfo,
   uint cmq,
   [In, Out] MULTI_QI[] pResults);

User-Defined Types:

CLSCTX, MULTI_QI

[DllImport("ole32.dll", ExactSpelling=true, PreserveSig=false)]

[return: MarshalAs(UnmanagedType.Interface)]

static extern object CoCreateInstance(

[In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,

[MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,

CLSCTX dwClsContext,

[In, MarshalAs(UnmanagedType.LPStruct)] Guid riid

);

Notes:

None.

C# Alternative Signature:

[DllImport("ole32.dll", EntryPoint = "CoCreateInstance", CallingConvention = CallingConvention.StdCall)]

static extern UInt32 CoCreateInstance(

[In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,

IntPtr pUnkOuter,

UInt32 dwClsContext,

[In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
   out IntPtr ppv

);

Tips & Tricks:

Please add some!

VB Signature:

<DllImport("ole32.dll", ExactSpelling:=True, PreserveSig:=False)> _
Function CoCreateInstance( _
    ByRef clsid As Guid, _
    <MarshalAs(UnmanagedType.Interface)> ByVal punkOuter As Object, _
    ByVal context As Integer, _
    ByRef iid As Guid) _
    As <MarshalAs(UnmanagedType.Interface)> Object
End Function

Sample Code:

Please add some!

User-Defined Types:

CLSCTX

Alternative Managed API:

Have a look at System.Activator.CreateInstance and its various overloads. Sample:

Guid CLSID_ShellDesktop = new Guid("00021400-0000-0000-C000-000000000046");
Type shellDesktopType = Type.GetTypeFromCLSID(CLSID_ShellDesktop, true);
object shellDesktop = Activator.CreateInstance(shellDesktopType);

Notes:

None.

If you just want to create an instance on a specific server, you can pass the server name into GetTypeFromCLSID:

Tips & Tricks:

Sample:

Guid CLSID_ShellDesktop = new Guid("00021400-0000-0000-C000-000000000046");
Type shellDesktopType = Type.GetTypeFromCLSID(CLSID_ShellDesktop, "REMOTESERVER", true);
Type shellDesktopType = Type.GetTypeFromCLSID(CLSID_ShellDesktop, true);
object shellDesktop = Activator.CreateInstance(shellDesktopType);

Sample Code:

Please add some!

Documentation

Alternative Managed API:

Have a look at System.Activator.CreateInstance and its various overloads.

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