Next Previous Up Top Contents Index

2.4 Naming and mapping conventions

2.4.3 Mapping C types onto Dylan classes

The multitude of integer data types in C code (int, long, unsigned, ULONG, DWORD, LRESULT, and so on) are all designated as <integer> (or some appropriate subrange thereof) in Dylan method argument types. However, a <machine-word> needs to be used to represent values that do not fit in the signed 30-bit representation of an integer.

Names such as <DWORD> should not be used in application code because they refer to the FFI designation of the C value representation, not to a Dylan data type.

The C types BOOL and BOOLEAN are both mapped to <boolean> in Dylan. Use #t and #f instead of TRUE and FALSE.

Note: Beware that some functions, such as TranslateAccelerator, though documented to return TRUE or FALSE, actually return int instead of BOOL; in such a case, you will have to compare the result to 0.

Similarly, watch out for cases where C code passes TRUE or FALSE as an integer argument. To handle one common case, the Dylan implementation of MAKELPARAM accepts either an <integer> or <boolean> as the first argument.

The C types CHAR, WCHAR, and TCHAR are all mapped to <character> in Dylan. However, UCHAR is mapped to <integer> since that is how it is actually used.

Most of the pointer types in the Windows API have several names; for example: PRECT, NPRECT, and LPRECT. In 16-bit code, these distinguished between "near" and "far" pointers, but in 32-bit code there is no difference. Rather than carry the duplicate names over into Dylan, it would be simpler to use only the basic P... prefix names. However, the LP... names seem to be used much more often, and hence may be more familiar, and the Microsoft documentation still tends to use the LP... names in most places. So the Dylan interface defines both the <P...> and <LP...> names even though they have identical values. The NP... names are not defined in Dylan since they are not as commonly used.

Values of type char* in C are represented as instances of class <C-string> in Dylan. This is a subclass of <string>, so all of the normal string operations can be used directly. C function parameters of type char* will also accept an instance of <byte-string>; a C pointer is created to point to the characters of the Dylan data, so the string does not need to be copied. (Dylan byte strings maintain a NUL character at the end in order to allow them to be used directly by C.)

in the current implementation, that involves automatically copying the string at run time, but the need for copying is intended to be removed later.

The TEXT function can also be used to coerce a string literal to a <C-string>. This usage is consistent with the Win32 TEXT macro, although the current purpose is different.

The Dylan declarations for C types will generally follow the strict alternative versions of the C declarations. This means, for example, that the various handle types such as <hmenu> and <hwnd> are disjoint subclasses of <handle>, instead of all being equivalent.


C FFI and Win 32 Reference - 31 MAR 2000

Next Previous Up Top Contents Index