Debian Codegraph @

docs/main005.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="generator" content="hevea 2.35">
6 <link rel="stylesheet" type="text/css" href="main.css">
7 <title>Hints on writing IDL files</title>
8 </head>
9 <body >
10 <a href="main004.html"><img src="previous_motif.svg" alt="Previous"></a>
11 <a href="index.html"><img src="contents_motif.svg" alt="Up"></a>
12 <a href="main006.html"><img src="next_motif.svg" alt="Next"></a>
13 <hr>
14 <h2 id="sec37" class="section">5 Hints on writing IDL files</h2>
15 <h3 id="sec38" class="subsection">5.1 Writing an IDL file for a C library</h3>
16 <p>When writing an IDL file for a C library that doesn’t have an IDL interface
17 already, the include files for that library are a good starting point:
18 just copy the relevant type and functin declarations to the IDL file,
19 then annotate them with IDL attributes to describe more precisely
20 their actual behavior. The documentation of the library must be read
21 carefully to determine the mode of function parameters (<span class=machine><span class=font-tt>in</span></span>, <span class=machine><span class=font-tt>out</span></span>,
22 <span class=machine><span class=font-tt>inout</span></span>), the actual sizes of arrays, etc.</p><p>The type definitions in the IDL file need not correspond exactly with
23 those in the include files. Often, a cleaner Caml interface can be
24 obtained by omitting irrelevant struct fields, or changing their types.
25 For instance, the Unix library functions for reading library entries
26 may use the following structure:
27 </p><pre> struct dirent {
28 long int d_ino;
29 __off_t d_off;
30 unsigned short int d_reclen;
31 unsigned char d_type;
32 char d_name[256];
33 };
34 </pre><p>Of those fields, only <span class=machine><span class=font-tt>d</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>name</span></span> and <span class=machine><span class=font-tt>d</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>ino</span></span> are of interest to the
35 user; the other fields are internal information for the library
36 functions, are not specified in the POSIX specs, and therefore must
37 not be used. Thus, in the IDL file, you should declare:
38 </p><pre> struct dirent {
39 long int d_ino;
40 char d_name[256];
41 };
42 </pre><p>Thus, the Caml code will have
43 <span class=machine><span class=font-tt>type struct</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>dirent = </span></span><span class=machine><span class=font-tt>{</span></span><span class=machine><span class=font-tt>d</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>ino: int; d</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>name: string</span></span><span class=machine><span class=font-tt>}</span></span>
44 as desired. However, the generated stub code, being
45 compiled against the “true” definition of <span class=machine><span class=font-tt>struct dirent</span></span>, will find
46 those two fields at the correct offsets in the actual struct.</p><p>Special attention must be paid to integer fields or variables.
47 By default, integer IDL types are mapped to the Caml type <span class=machine><span class=font-tt>int</span></span>,
48 which is convenient to use in Caml code, but loses one bit
49 when converting from a C <span class=machine><span class=font-tt>long</span></span> integer, and may lose one bit (on
50 32-bit platforms) when converting from a C <span class=machine><span class=font-tt>int</span></span> integer. When the
51 range of values represented by the C integer is small enough, this
52 loss is acceptable. Otherwise, you should use the attributes
53 <span class=machine><span class=font-tt>nativeint</span></span>, <span class=machine><span class=font-tt>int32</span></span> or <span class=machine><span class=font-tt>int64</span></span> so that integer IDL types are mapped
54 to one of the Caml boxed integer types. (We recommend that you use
55 <span class=machine><span class=font-tt>int32</span></span> or <span class=machine><span class=font-tt>int64</span></span> for integers that are specified as being exactly 32
56 bit wide or 64 bit wide, and <span class=machine><span class=font-tt>nativeint</span></span> for unspecified <span class=machine><span class=font-tt>int</span></span> or
57 <span class=machine><span class=font-tt>long</span></span> integers.)</p><p>Yet another possibility is to declare certain integer fields or variables
58 as <span class=machine><span class=font-tt>double</span></span> in the IDL file, so that they are represented by <span class=machine><span class=font-tt>float</span></span>
59 in Caml, and all 32 bits of the integer are preserved in Caml. For
60 instance, the Unix function to get the current type is declared as
61 </p><pre> time_t time(time_t * t);
62 </pre><p>where <span class=machine><span class=font-tt>time</span></span><span class=machine><span class=font-tt>_</span></span><span class=machine><span class=font-tt>t</span></span> is usually defined as <span class=machine><span class=font-tt>long</span></span>. We can nonetheless
63 pretend (in the IDL file) that <span class=machine><span class=font-tt>time</span></span> returns a double:
64 </p><pre> double time() quote(" _res = time(NULL); ");
65 </pre><p>This way, <span class=machine><span class=font-tt>time</span></span> will have the Caml type <span class=machine><span class=font-tt>unit -&gt; float</span></span>.
66 Again, the stub code “knows” that <span class=machine><span class=font-tt>time</span></span> actually returns an integer,
67 and therefore will insert the right integer-float coercions.</p>
68 <h3 id="sec39" class="subsection">5.2 Sharing IDL files between MIDL and CamlIDL</h3>
69 <p>The Microsoft software development kit provides a number of IDL files
70 describing various libraries and components. In its current state,
71 <span class=machine><span class=font-tt>camlidl</span></span> cannot exploit those files directly: they use many
72 (often poorly documented) Microsoft IDL features that are not
73 implemented yet in <span class=machine><span class=font-tt>camlidl</span></span>; symmetrically, <span class=machine><span class=font-tt>camlidl</span></span> introduces
74 several new annotations that are not recognized by Microsoft’s <span class=machine><span class=font-tt>midl</span></span>
75 compiler. So, significant editing work on the IDL files is required.</p><p>The C preprocessor can be used to alleviate the <span class=machine><span class=font-tt>camlidl</span></span>-<span class=machine><span class=font-tt>midl</span></span>
76 incompatibilities: <span class=machine><span class=font-tt>camlidl</span></span> defines the preprocessor symbol <span class=machine><span class=font-tt>CAMLIDL</span></span>
77 when preprocessing its input files, while <span class=machine><span class=font-tt>midl</span></span> does not. Hence,
78 one can bracket incompatible definitions in
79 <span class=machine><span class=font-tt>#ifdef CAMLIDL ... #else ... #endif</span></span>. Along these lines, a C
80 preprocessor header file, <span class=machine><span class=font-tt>camlidlcompat.h</span></span>, is provided: it uses
81 <span class=machine><span class=font-tt>#define</span></span> to remove <span class=machine><span class=font-tt>camlidl</span></span>-specific attributes when compiling with
82 <span class=machine><span class=font-tt>midl</span></span>, and to remove <span class=machine><span class=font-tt>midl</span></span>-specific attributes when compiling with
83 <span class=machine><span class=font-tt>camlidl</span></span>. Thus, an IDL file compatible with both <span class=machine><span class=font-tt>midl</span></span> and
84 <span class=machine><span class=font-tt>camlidl</span></span> would look like this:
85 </p><pre> #include &lt;camlidlcompat.h&gt;
86
87 #ifndef CAMLIDL
88 import "unknwn.idl"; // imports specific to MIDL
89 import "oaidl.idl";
90 #endif
91 import "mymodule.idl"; // imports common to MIDL and CamlIDL
92
93 typedef [abstract,marshal_as(int)] void * ptr;
94
95 ...
96
97 #ifndef CAMLIDL
98 [...] library MyTypeLib {
99 importlib("stdole32.tlb");
100 [...] coclass MyComponent { [default] interface IX; }
101 }
102 #endif
103 </pre><p>Notice that since <span class=machine><span class=font-tt>camlidl</span></span> doesn’t handle type libraries, the type
104 library part of an <span class=machine><span class=font-tt>midl</span></span> file must be enclosed in <span class=machine><span class=font-tt>#ifndef CAMLIDL</span></span>.</p>
105 <h3 id="sec40" class="subsection">5.3 Dispatch interfaces and type libraries</h3>
106 <p> <a id="s-dispatch"></a></p><p>A dispatch interface, in COM lingo, is an interface that supports
107 dynamic, interpreted dispatch of method interfaces. This form of
108 interpreted dispatch is used by Visual Basic and other scripting
109 languages to perform calls to methods of COM components.</p><p>CamlIDL provides minimal support for dispatch interfaces. To equip a
110 Caml component with a dispatch interface (thus making it callable from
111 Visual Basic), you need to do the following:
112 </p><ol class="enumerate" type=1><li class="li-enumerate">
113 Use <span class=machine><span class=font-tt>IDispatch</span></span> instead of <span class=machine><span class=font-tt>IUnknown</span></span> as the super-interface of
114 the component’s interfaces.
115 </li><li class="li-enumerate">Write a type library for your component and compile it using
116 <span class=machine><span class=font-tt>midl</span></span>. A type library is a run-time representation of the interfaces
117 supported by an object. The <span class=machine><span class=font-tt>midl</span></span> compiler can generate a type
118 library from the IDL description of the component, enriched with some
119 special-purpose declarations (the <span class=machine><span class=font-tt>library</span></span> and <span class=machine><span class=font-tt>coclass</span></span>
120 statements). Refer to the documentation of <span class=machine><span class=font-tt>midl</span></span> for more
121 information.
122 </li><li class="li-enumerate">Pass the type library files (<span class=machine><span class=font-tt>.tlb</span></span> files) generated by <span class=machine><span class=font-tt>midl</span></span>
123 as extra arguments to <span class=machine><span class=font-tt>camlidldll</span></span> when generating the DLL for your
124 Caml component.
125 </li></ol>
126 <hr>
127 <a href="main004.html"><img src="previous_motif.svg" alt="Previous"></a>
128 <a href="index.html"><img src="contents_motif.svg" alt="Up"></a>
129 <a href="main006.html"><img src="next_motif.svg" alt="Next"></a>
130 </body>
131 </html>