To invoke the managed class library method implemented by C # in C + + using the IDispatch::Invoke function

Source: Internet
Author: User

CLR Interop is simply a technique for making unmanaged and managed code callable to each other. This technology enables developers to reuse existing managed or unmanaged builds and, depending on their needs, weighs the simplicity of managed code and the flexibility of unmanaged code, choosing a programming language that is appropriate for its own reality, without having to think too much about the language in which the reusable component is developed. Interop Chinese means interoperability, since there are two different ways in which code can be invoked. This article is about using COM interop technology to invoke managed code in unmanaged code.

1. Creating a Managed server

Let's first create a C # Class library (class Libraries) project in Visual Studio 2008, named Mymanagedserver, in which we declare and implement an interface.

The interface declaration code is as follows:

For simplicity, there is only one method in this interface to print some information. The ComVisible property is critical, and the interface is visible to COM when its value is true.

View Plaincopy to Clipboardprint?

1.using System;
2.using System.Runtime.InteropServices;
3.
4.namespace Mymanagedserver
5.{
6. [ComVisible (True),
7. Guid ("79edda1c-f243-47c5-8954-5def01fa3d44"),
8. InterfaceType (cominterfacetype.interfaceisdual)]
9. Public Interface Imanagedfooclass
Ten. {
[PreserveSig, DispId (1)]
void Printfoo ();
. }
14.}

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.