Managed Interfaces

The __interface keyword can be used to create COM-style interfaces with a minimum of fuss and no IDL. This keyword is not specifically listed in the first part of this chapter because it applies to other technologies, too.

However, when used in conjunction with the __gc managed C++ extension, it creates a managed interface. A simple interface is shown in Listing 1.4.5.

Listing 1.4.5. managedif.cpp: A Simple Managed Interface
#using <mscorlib.dll>
using namespace System;

__gc __interface IDoSomething
{
    String * SayHello();
};

__gc class CDoSomething : public IDoSomething
{
    String * SayHello()
    {
        return new String("Hello");
    } }; void main() { CDoSomething* pD=new CDoSomething(); IDoSomething *pI=static_cast<IDoSomething*>(pD); ...

Get C# and the .NET Framework: The C++ Perspective now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.