I noticed a new tool on CodePlex the other day called the PInvoke Interop Assistant that automatically converts between managed and unmanaged type signatures. In addition to converting API functions, it also pulls together all of the structures and types used by the API. Here's a sample showing how it handles CoCreateInstance. [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct IUnknown { /// IUnknownVtbl* public System.IntPtr lpVtbl; } [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)] public struct GUID { /// unsigned int public uint Data1; /// unsigned short public ushort Data2; /// unsigned short public ushort Data3; /// unsigned char[8] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=8)] public string Data4; } [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct IUnknownVtbl { /// IUnknownVtbl_QueryInterface public IUnknownVtbl_QueryInterface AnonymousMember1; /// IUnknownVtbl_AddRef public IUnknownVtbl_AddRef AnonymousMember2; /// IUnknownVtbl_Release public IUnknownVtbl_Release AnonymousMember3; } /// Return Type: HRESULT->LONG->int ///This: IUnknown* ///riid: IID* ///ppvObject: void** [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)] public delegate int IUnknownVtbl_QueryInterface( ref IUnknown This, ref GUID riid, ref System.IntPtr ppvObject); /// Return Type: ULONG->unsigned int ///This: IUnknown* [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)] public delegate uint IUnknownVtbl_AddRef( ref IUnknown This); /// Return Type: ULONG->unsigned int ///This: IUnknown* [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)] public delegate uint IUnknownVtbl_Release( ref IUnknown This); public partial class NativeMethods { /// Return Type: HRESULT->LONG->int ///rclsid: IID* ///pUnkOuter: LPUNKNOWN->IUnknown* ///dwClsContext: DWORD->unsigned int ///riid: IID* ///ppv: LPVOID* [System.Runtime.InteropServices.DllImportAttribute( "ole32.dll" , EntryPoint= "CoCreateInstance" , CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall)]
Read More...