C # managed code and C + + unmanaged code call each other two (C # code calls C + +)

Source: Internet
Author: User

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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.