Guid iid = new Guid("00020400-0000-0000-C000-000000000046"); object comObject = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")); IntPtr pIDispatch = Marshal.GetComInterfaceForObject(comObject, typeof(IDispatch)); Marshal.ReleaseComObject(comObject);
Guid iid = new Guid("00000000-0000-0000-C000-000000000046"); object comObject = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")); IntPtr pIUnknown = Marshal.GetIUnknownForObject(comObject); Marshal.ReleaseComObject(comObject);In this example, we create a new instance of the Shell application and retrieve the IUnknown interface pointer from it using the GetIUnknownForObject method. We then release the COM object using the ReleaseComObject method. The Marshal.GetComObjectData method is part of the System.Runtime.InteropServices namespace in the .NET Framework.