using System;
using System.Runtime.InteropServices;
namespace AddCom
{
[Guid("8F5710FD-528A-4474-A673-47717F93EA88")]
public interface AddComInterface
{
[DispId(1)]
int Add(int a, int b);
}
[Guid("FFA2EB4B-1BBE-4332-8D3E-5E9F619A3FC9")]
[ClassInterface(ClassInterfaceType.None)]
public class AddComClass : AddComInterface
{
public int Add(int a, int b)
{
return a + b;
}
}
}
4. C++调用示例代码
#include<iostream>#include<Windows.h>#import"../../AddCom/AddCom/bin/Debug/AddCom.tlb"usingnamespace AddCom;intmain(){CoInitialize(0);
AddCom::AddComInterfacePtr pAddCom(__uuidof(AddComClass));int a = pAddCom->Add(1,2);
std::cout << a << std::endl;CoUninitialize();return0;}