1 This is intended to be a document to help new developers get started.
2 Existing developers should feel free to add their comments.
7 NE (Win16) executables consist of multiple segments. The Wine loader
8 loads each segment into a unique location in the Wine processes memory
9 and assigns a selector to that segment. Because of this, it's not
10 possible to exchange addresses freely between 16-bit and 32-bit code.
11 Addresses used by 16-bit code are segmented addresses (16:16), formed
12 by a 16-bit selector and a 16-bit offset. Those used by the Wine code
13 are regular 32-bit linear addresses.
15 There are four ways to obtain a segmented pointer:
16 - Use the SEGPTR_* macros in include/heap.h (recommended).
17 - Allocate a block of memory from the global heap and use
18 WIN16_GlobalLock to get its segmented address.
19 - Allocate a block of memory from a local heap, and build the
20 segmented address from the local heap selector (see the
21 USER_HEAP_* macros for an example of this).
22 - Declare the argument as 'segptr' instead of 'ptr' in the spec file
23 for a given API function.
25 Once you have a segmented pointer, it must be converted to a linear
26 pointer before you can use it from 32-bit code. This can be done with
27 the PTR_SEG_TO_LIN() and PTR_SEG_OFF_TO_LIN() macros. The linear
28 pointer can then be used freely with standard Unix functions like
29 memcpy() etc. without worrying about 64k boundaries. Note: there's no
30 easy way to convert back from a linear to a segmented address.
32 In most cases, you don't need to worry about segmented address, as the
33 conversion is made automatically by the callback code and the API
34 functions only see linear addresses. However, in some cases it is
35 necessary to manipulate segmented addresses; the most frequent cases
37 - API functions that return a pointer
38 - lParam of Windows messages that point to a structure
39 - Pointers contained inside structures accessed by 16-bit code.
41 It is usually a good practice to used the type 'SEGPTR' for segmented
42 pointers, instead of something like 'LPSTR' or 'char *'. As SEGPTR is
43 defined as a DWORD, you'll get a compilation warning if you mistakenly
44 use it as a regular 32-bit pointer.
50 Under Windows, data structures are tightly packed, i.e. there is no
51 padding between structure members. On the other hand, by default gcc
52 aligns structure members (e.g. WORDs are on a WORD boundary, etc.).
53 This means that a structure like
55 struct { BYTE x; WORD y; };
57 will take 3 bytes under Windows, but 4 with gcc, because gcc will add a
58 dummy byte between x and y. To have the correct layout for structures
59 used by Windows code, you need to use the WINE_PACKED attribute; so you
60 would declare the above structure like this:
62 struct { BYTE x; WORD y WINE_PACKED; };
64 You have to do this every time a structure member is not aligned
65 correctly under Windows (i.e. a WORD not on an even address, or a
66 DWORD on a address that is not a multiple of 4).
69 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
70 ==============================================
72 In order to support both Win16 and Win32 APIs within the same source
73 code, as well as share the include files between the emulator and the
74 library, the following convention must be used in naming all API
75 functions and types. If the Windows API uses the name 'xxx', the Wine
78 - 'xxx16' for the 16-bit version,
79 - 'xxx32' for the 32-bit version when no ASCII/Unicode strings are
81 - 'xxx32A' for the 32-bit version with ASCII strings,
82 - 'xxx32W' for the 32-bit version with Unicode strings.
84 You should then use the macros WINELIB_NAME[_AW](xxx) or
85 DECL_WINELIB_TYPE[_AW](xxx) (defined in include/wintypes.h) to define
86 the correct 'xxx' function or type for Winelib. When compiling the
87 emulator, 'xxx' is _not_ defined, meaning that you must always specify
88 explicitly whether you want the 16-bit or 32-bit version.
90 Note: if 'xxx' is the same in Win16 and Win32, you can simply use the
97 DECL_WINELIB_TYPE(INT);
99 typedef struct { /* Win32 ASCII data structure */ } WNDCLASS32A;
100 typedef struct { /* Win32 Unicode data structure */ } WNDCLASS32W;
101 typedef struct { /* Win16 data structure */ } WNDCLASS16;
102 DECL_WINELIB_TYPE_AW(WNDCLASS);
104 ATOM RegisterClass16( WNDCLASS16 * );
105 ATOM RegisterClass32A( WNDCLASS32A * );
106 ATOM RegisterClass32W( WNDCLASS32W * );
107 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
109 The Winelib user can then say:
112 WNDCLASS wc = { ... };
113 RegisterClass( &wc );
115 and this will use the correct declaration depending on the definition
116 of the symbols WINELIB16, WINELIB32 and UNICODE.
122 Because Win16 programs use a 16-bit stack and because they can only
123 call 16:16 addressed functions, all API entry points must be at low
124 address offsets and must have the arguments translated and moved to
125 Wines 32-bit stack. This task is handled by the code in the "if1632"
126 directory. To define a new API entry point handler you must place a
127 new entry in the appropriate API specification file. These files are
128 named *.spec. For example, the API specification file for the USER
129 DLL is contained in the file user.spec. These entries are processed
130 by the "build" program to create an assembly file containing the entry
131 point code for each API call. The format of the *.spec files is
132 documented in the file "tools/build-spec.txt".
138 To display a message only during debugging, you normally write something
145 You can write this shorter (and better) in this way:
147 dprintf_win(stddeb,"abc...");
149 All symbols of the form dprintf_xxxx are macros defined in include/debug.h .
150 The macro-definitions are generated by the shell-script tools/make_debug. It
151 scans the source code for symbols of this forms and puts the necessary
152 macro definitions in include/debug.h and include/stddebug.h . These macros
153 test for the symbol DEBUG_XXXX (e.g. dprintf_win refers to DEBUG_WIN) being
154 defined and thus decided whether to actually display the text. If you want
155 to enable specific types of messages, simply put the corresponding
156 #define DEBUG_XXXX in include/stddebug.h . If you want to enable or disable
157 a specific type of message in just one c-source-file, put the corresponding
158 #define DEBUG_XXXX or #undefine DEBUG_XXXX between #include<stddebug.h> and
159 #include <debug.h> in that specific file. In addition you can change the
160 types of displayed messages by supplying the "-debugmsg" option to Wine.
161 If your debugging code is more complex than just printf, you can use the
162 symbols debugging_XXX as well. These are true when XXX is enabled, either
163 permanent or in the command line. So instead of writing
166 DumpSomeStructure(&str);
170 if(debugging_win)DumpSomeStructure(&str);
171 Don't worry about the inefficiency of the test. If it is permanently
172 disabled (thus debugging_win is 0 at compile time), the compiler will
173 eliminate the dead code.
175 The file handle "stddeb" is intended for displaying standard informational
176 messages, whereas "stdnimp" is intended for displaying messages concerning
177 not yet implemented functions.
179 You have to start tools/make_debug only if you introduced a new macro,
180 e.g. dprintf_win32s - not if you just changed one of the #define
181 DEBUG_XXX's in include/stddebug.h or in a specific file.