日期:2014-05-18 浏览次数:20906 次
using System; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; [ComImport] [Guid("A4E23A7E-C932-4A31-8455-2F137015DCB2")] public interface INakeTTClass { [DispId(1)] int Add(int a, int b, out int c); [DispId(2)] int GetAge(); } [ComImport] [Guid("73269D6A-0BB4-46B8-8062-7C8DCDEE81D3")] public class NakeTTClass : INakeTTClass { [DispId(1)] [PreserveSig] //<--------------- [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] public extern int Add(int a, int b, out int c); [DispId(2)] [PreserveSig] //<--------------- [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] public extern int GetAge(); } class Program { static void Main() { NakeTTClass nake = new NakeTTClass(); int sum; int hresult = nake.Add(3, 4, out sum); // hresult == S_OK == 0 // sum == 7 hresult = nake.GetAge(); // hresult = 123 } }