[ComImport] [Guid("....")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IMyInterface { void Func(); }
[ComImport] interface IA { void FuncA(); } [ComImport] interface IB:IA { void new FuncA(); void FuncB(); }
[ComImport]
public class MyRcw:IMyInterface { [MethodImplAttribute(MethodImplOptions.InternalCall)] public extern void MyFunc(); }
不會創建com 組件的請參考csdn上一位大牛的帖子 上面有詳細的建立流程在此感謝大牛的分享 ,這裏在分享一篇有心人收集的對應表 com 我我的感受用的是Windows數據類型 本身參考查看 對應表html
c#代碼片斷c++
class Program { [ComImport, Guid("31E95758-B52C-4252-B4E0-F33547F9B55A")] public interface IMyATLClass { [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(1)] void Add([In] int para1, [In] int para2); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(2)] void PopupDialog(string text); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(3)] void Sum([In] int para1, [In] int para2, out int Sum); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(4)] void Messg([In] string test); } [ComImport, CoClass(typeof(MyATLClassClass)), Guid("31E95758-B52C-4252-B4E0-F33547F9B55A")] public interface MyATLClass : IMyATLClass { } [ComImport, Guid("D14CE5C7-9648-427B-BEAC-504E1A91DDAE")] public class MyATLClassClass : IMyATLClass, MyATLClass { [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(1)] public virtual extern void Add([In] int para1, [In] int para2); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(2)] public virtual extern void PopupDialog([MarshalAs(UnmanagedType.BStr)] string text); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(3)] public virtual extern void Sum([In] int para1, [In] int para2, out int Sum); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(4)] public virtual extern void Messg([In] string test); } static void Main(string[] args) { // MyATLComLib.IMyATLClass aa = new MyATLComLib.MyATLClass(); //int i; //aa.Sum(1, 1, out i); //aa.Add(1, 1); //IntPtr sb = Marshal.StringToCoTaskMemAnsi("123"); IMyATLClass aa = new MyATLClass(); int i; aa.Sum(1, 1, out i); //aa.Add(1, 1); // IntPtr sb = Marshal.StringToCoTaskMemUni("21"); string a = "a"; IntPtr aPtr = Marshal.StringToHGlobalAnsi(a); IntPtr helloPtr = Marshal.StringToHGlobalAnsi("aaaa"); // aa.PopupDialog("a1"); aa.Messg("哈哈哈哈"); Console.Write(i); Console.Read(); // int i; // DemoObjectLib.IMyCOMDemo aaaa= new DemoObjectLib.MyCOMDemoClass(); } }
c++代碼片斷c#
// MyATLClass.cpp : CMyATLClass 的實現 #include "stdafx.h" #include "MyATLClass.h" // CMyATLClass STDMETHODIMP CMyATLClass::Add(LONG para1, LONG para2) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); LONG a = para1 + para2; AfxMessageBox(a); // TODO: 在此添加實現代碼 return S_OK; } STDMETHODIMP CMyATLClass::PopupDialog(CHAR* text) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // TODO: 在此添加實現代碼 AfxMessageBox((LPCTSTR)text); return S_OK; } STDMETHODIMP CMyATLClass::Sum(LONG para1, LONG para2, LONG* sum) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // TODO: 在此添加實現代碼 *sum = para1 + para2; return S_OK; } STDMETHODIMP CMyATLClass::Messg(BSTR test) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // TODO: 在此添加實現代碼 AfxMessageBox(test); return S_OK; }