18.3 Exposing C# Objects to COMJust as an RCW proxy wraps a COM object when you access it from C#, code that accesses a C# object as a COM object must do so through a proxy as well. When your C# object is marshaled out to COM, the runtime creates a COM Callable Wrapper (CCW). The CCW follows the same lifetime rules as other COM objects, and as long as it is alive, a CCW maintains a traceable reference to the object it wraps. This keeps the object alive when the garbage collector is run. The following example shows how you can export both a class and an interface from C# and control the Global Unique Identifiers (GUIDs) and Dispatch IDs (DISPIDs) assigned. After compiling IRunInfo and StackSnapshot , you can register both using RegAsm.exe . // IRunInfo.cs // Compile with: // csc /t:library IRunInfo.cs using System; using System.Runtime.InteropServices; [GuidAttribute("aa6b10a2-dc4f-4a24-ae5e-90362c2142c1")] public interface IRunInfo { [DispId(1)] string GetRunInfo( ); } // StackSnapshot.cs // compile with csc /t:library /r:IRunInfo.dll StackSnapShot.cs using System; using System.Runtime.InteropServices; using System.Diagnostics; [GuidAttribute("b72ccf55-88cc-4657-8577-72bd0ff767bc")] public class StackSnapshot : IRunInfo { public StackSnapshot( ) { st = new StackTrace( ); } [DispId(1)] public string GetRunInfo( ) { return st.ToString( ); } private StackTrace st; } |