C # interaction between managed code and C ++ unmanaged code (C ++ calls C # code)

Source: Internet
Author: User

Now complete the second part, that is, the C ++ unmanaged code calls the C # managed code, which is divided into two parts. First, C # creates the COM + component, second, C ++ calls the COM + component.

C # create COM + Components

1. In vs, create a class library cominterop.

2. added the cominteropinterface interface in the class library and the corresponding implementation cominterop. cominterop must also inherit from servicedcomponent. Cominteropinterface has two simple interfaces:

 int Add(int a, int b);

 int Minus(int a, int b);

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

// Declare the implementation class
[GUID ("87796e96-ec28-4570-90c3-a395f4f4a7d6")]
[Classinterface (classinterfacetype. None)]
Public class cominterop: servicedcomponent, cominteropinterface
{
Public cominterop (){}

        public int Add(int a, int b)
        {
            return a + b;
        }

        public int Minus(int a, int b)
        {
            return a - b;
        }
    }
}

3. Use the regasm command to export the virtual table. When re-compiling the production DLL, use the regasm/u command to deregister the previous DLL.

    REGASM  ComInteropDemo.dll /tlb ComInteropDemo.tlb

    REGASM  /u ComInteropDemo.dll

Note the following points for writing COM + components:

2. Methods and attributes must be declared in the interface, and events must also be declared in the event interface.

Otherwise, it cannot be called in VC. The declaration in the interface is mainly for the vtab in COM.

3. You must declare [dispid (1)] before the method, attribute, and event in the interface.

5. The project must be com InterOP with a strong name.

6. The comvisible attribute of the component must be true. The reason highlighted here is that the default value in vs is false.

C ++ calls the C # COM + component

Steps:

1. Create a C ++ project cpploader, select Win32 as the project type, and set the console application

2. Import the Type Library TLB to the header file

    #import "../Debug/ComInteropDemo.tlb"

3. initialize COM and generate smart pointers (generally, when you need to call the methods provided in the COM component, You need to generate a smart pointer pointing to this interface)

4. Call the add method in COM

5. Release the environment. The Code is as follows:

#include "stdafx.h"
#include <iostream>
using namespace std;

# Import "../debug/cominteropdemo. TLB"
// The path must be correct.

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr;

    //ComInteropDemo::ComInterop *p;

// Initialize com
Coinitialize (null );

// Create a smart pointer cominteropdemo: cominteropinterface
Cominteropdemo: cominteropinterfaceptr PTR;

// Create an instance
HR = PTR. createinstance (_ uuidof (cominteropdemo: cominterop ));

    if(hr == S_OK)
    {
        cout << ptr->Add (1.0, 2.0);
    }       

    CoUninitialize ();
    return 0;
}

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.