The most beautiful thing about the MIDL compiler is that you can create standard marshalers for a custom interface without writing any code at all! The overall process is really very simple:
Describe your interface in an IDL file, specifying the IID as an attribute of the interface.
Write a DEF file for the proxy/stub DLL that exports DllGetClassObject, DllCanUnloadNow, and a function named GetProxyDllInfo.
Write a make file that pumps the IDL file through MIDL and compiles the resulting files.
Write a REG file containing the registry information for the compiled DLL.
For examples of using MIDL, I've included four (yes, four) relevant samples in this chapter. First is yet another variation of the EKoala server, this time EKoala4 (CHAP06\EKOALA4). This is another modification of Chapter 5's EKoala1. The only changes are two custom interfaces added to the Koala object. These interfaces are IAnimal (0002114a-0000-0000-c000-000000000046) and IKoala (0002114b-0000-0000-c000-000000000046). We'll see the member functions of these interfaces shortly. To work with EKoala4, I've included ObjectUser3 (CHAP06\OBJUSER3), which knows how to call the functions in IAnimal and IKoala. This is basically a modification of Chapter 5's ObjectUser.
The code in both of these samples is pretty similar to everything we've seen before. EKoala4 serves up the Koala object, and the Koala object provides its interfaces and implements their member functions. (Unlike EKoala3, the EKoala4 server doesn't show a main window because it has no menu items itself.) ObjectUser3 queries for those interfaces, calls their member functions, and displays the results. In fact, none of the code in either sample knows that IAnimal and IKoala are custom interfaces—it treats them as if they were OLE-defined interfaces, taking their marshaling support for granted.
How we create the marshalers for IAnimal and IKoala is of interest to us here. In the CHAP06\IANIMAL directory, you'll find only a few files: a make file, a DEF file, a REG file, and IANIMAL.IDL, the interface definition. You'll find similar files in CHAP06\IKOALA. The make files in these directories will produce IANIMAL.DLL and IKOALA.DLL, which are set with the REG files as InprocServer32 for CLSID_PSIAnimal and CLSID_PSIKoala, respectively (the same values as IID_IAnimal and IID_IKoala, for reasons we'll see later).
Let's look briefly at each step we have to take to create these necessary sources.