Sdílet prostřednictvím


Defining the Marshaling Type

Before you can build a marshaler, you must define the managed and unmanaged interfaces being marshaled. These interfaces commonly perform the same function, but are exposed differently to managed and unmanaged objects.

A managed compiler produces a managed interface from metadata, and the resulting interface looks like any other managed interface. The following code example shows a typical interface:

#using <mscorlib.dll>
interface INew {
    void NewMethod();
}

You define the unmanaged type in Interface Definition Language (IDL) and compile it with the MIDL compiler. You define the interface within a library statement and assign it an interface ID with the universal unique identifier (UUID) attribute.

In Old.idl

[uuid(9B2BAADA-0705-11D3-A0CD-00C04FA35826)]
library OldLib {
     [uuid(9B2BAADD-0705-11D3-A0CD-00C04FA35826)]
     interface IOld : IUnknown
         HRESULT OldMethod();
}

The MIDL compiler produces several output files. If the interface is defined in Old.idl, the output file Old_i.c defines a const variable with the interface identifier (IID) of the interface:

In Old_i.c

const IID IID_IOld = {0x9B2BAADD,0x0705,0x11D3,{0xA0,0xCD,0x00,0xC0,0x4F,0xA3,0x58,0x26}};

The Old.h file is also produced by MIDL. It contains a C++ definition of the interface that can be #included into your C++ source code.

See Also

Concepts

Implementing the ICustomMarshaler Interface

Using a Substitute Marshaler

Other Resources

Custom Marshaling