The article mentioned that the current project wants to do the core part of the code is not decompile, and considering that team members are more familiar with C #, so the core algorithm part of C + +, while the rest of the use of C # (such as data access layer, interface layer using C # language). In the previous article, C # managed code called C + + unmanaged code was completed, and now the second part, where C + + unmanaged code calls C # managed code, is divided into two parts, first C # building COM + components, and then C + + components.
C # Building COM + components
1. In VS, create a new class library ComInterop
2. New interface in class Library: Cominteropinterface, and corresponding implementation ComInterop, ComInterop must inherit from ServicedComponent at the same time. There are two simple interfaces in the Cominteropinterface:
int Add(int a, int b);
int Minus(int a, int b);
The specific code is as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
Using System.Runtime.InteropServices;
using System.EnterpriseServices;
Namespace Cominteropdemo
{
/interface declaration
[Guid (' 7103c10a-2072-49fc-ad61-475bee1c5fbb ')]
Public interface Cominteropinterface
{
[DispId (1)]
int Add (int a, int b);
[DispId (2)]
int minus (int a, int b);
}
//For declaration of implementation class
[Guid (' 87796e96-ec28-4570-90c3-a395f4f4a7d6 ')]
[ClassInterface (Classinterfa Cetype.none)]
public class cominterop:servicedcomponent, Cominteropinterface
{
Public comint Erop () {}
public int Add (int a, int b)
{
return a + B;
public int minus (int a, int b)
{
return a-b;
}
}