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