Write the IDL File

An IDL file for an OLE interface uses only a small portion of IDL, which is designed for much broader RPC applications. IDL simply happens to serve us well for our own little interfaces. When you set out to describe an interface, you must include at least the IID to assign to the interface, the interface keyword and the name of the interface, the statement import "unknwn.idl", and the list of your member functions. Any single IDL file can contain as many interface definitions as you'd like, but in the case of IAnimal and IKoala only one interface is in each file:


//CHAP06\IANIMAL\IANIMAL.IDL
[uuid(0002114a-0000-0000-c000-000000000046),
object
]
interface IAnimal : IUnknown
{
import "unknwn.idl";

HRESULT Eat([in] LPTSTR pszFoodRecommended
, [in, out] LPTSTR pszFoodEaten, [in] short cchEaten);
HRESULT Sleep([in, out] short *pcMinutes);
HRESULT Procreate([out] short *pcOffspring);
HRESULT WhatKindOfAnimal([out] IID *pIID);
}


//CHAP06\IKOALA\IKOALA.IDL
[uuid(0002114b-0000-0000-c000-000000000046),
object
]
interface IKoala : IUnknown
{
import "unknwn.idl";

HRESULT ClimbEucalyptusTree([in] short iTree);
void PouchOpensDown(void);
HRESULT SleepAfterEating([in] short cMinutes);
}

You can see that much of IDL is similar to ODL, such as the format for attributes. Some of the attributes, however, don't appear in ODL. The object attribute, for example, is an IDL attribute to identify the script as MIDL-specific. Check the MIDL reference for others.

One useful member function attribute is [async], which lets you describe the call as asynchronous as opposed to the default synchronous. Most important of all, however, are the [in], [out], and [in, out] attributes on the function arguments. These determine the type of marshaling code that MIDL will write for that member function. If you make a mistake here, the MIDL-generated code might not properly copy arguments, especially the contents of strings, across the process boundary.

On the whole, a number of attributes and other statements are available in ODL. For a full list, check the documentation.