Print names for Win32 errors too, plus a few more status codes.
[wine] / DEVELOPERS-HINTS
1 This document should help new developers get started. Like all of Wine, it
2 is a work in progress.
3
4
5 SOURCE TREE STRUCTURE
6 =====================
7
8 The Wine source tree is loosely based on the original Windows modules. 
9 Most of the source is concerned with implementing the Wine API, although
10 there are also various tools, documentation, sample Winelib code, and
11 code specific to the binary loader.  Note that several of the libraries
12 listed here are "stubbed out", meaning they still need to be implemented.
13
14 DLLs (under dlls/):
15 -------------------
16
17         advapi32/               - Crypto, systeminfo, security, eventlogging
18         avicap32/               - AVI capture window class
19         avifil32/               - COM object to play AVI files
20         cabinet/                - Cabinet file interface
21         capi2032/               - Wrapper library for CAPI4Linux access
22         cfgmgr32/               - Config manager
23         comcat/                 - Component category manager
24         comctl32/               - Common controls
25         commdlg/                - Common dialog boxes (both 16 & 32 bit)
26         crtdll/                 - Old C runtime library
27         crypt32/                - Cryptography
28         ctl3d/                  - 3D Effects for Common GUI Components
29         d3d8/                   - Direct3D (3D graphics)
30         d3d9/                   - Direct3D (3D graphics)
31         d3dim/                  - Direct3D Immediate Mode
32         d3dx8/                  - Direct3D (3D graphics)
33         dciman32/               - DCI Manager (graphics)
34         ddraw/                  - DirectDraw (graphics)
35         devenum/                - Device enumeration (part of DirectShow)
36         dinput/                 - DirectInput (device input)
37         dinput8/                - DirectInput (device input)
38         dmband/                 - DirectMusic
39         dmcompos/               - DirectMusic
40         dmime/                  - DirectMusic
41         dmloader/               - DirectMusic
42         dmscript/               - DirectMusic
43         dmstyle/                - DirectMusic
44         dmsynth/                - DirectMusic
45         dmusic/                 - DirectMusic
46         dmusic32/               - DirectMusic
47         dplay/                  - DirectPlay (networking)
48         dplayx/                 - DirectPlay (networking)
49         dpnhpast/               - DirectPlay NAT Helper
50         dsound/                 - DirectSound (audio)
51         gdi/                    - GDI (graphics)
52         gdi/enhmfdrv/           - Enhanced metafile driver
53         gdi/mfdrv/              - Metafile driver
54         glu32/                  - OpenGL Utility library (graphics)
55         icmp/                   - ICMP protocol (networking)
56         imagehlp/               - PE (Portable Executable) Image Helper lib
57         imm32/                  - Input Method Manager
58         iphlpapi/               - IP Helper API
59         kernel/                 - The Windows kernel
60         lzexpand/               - Lempel-Ziv compression/decompression
61         mapi32/                 - Mail interface
62         mpr/                    - Multi-Protocol Router (networking)
63         msacm/                  - Audio Compression Manager (multimedia)
64         msdmo/                  - DirectX Media Objects
65         msimg32/                - Gradient and transparency (graphics)
66         msisys/                 - System information
67         msi/                    - Microsoft Installer
68         msnet32/                - Network interface
69         msvcrt/                 - C runtime library 
70         msvcrt20/               - C runtime library version 2.0
71         msvcrtd/                - C runtime library debugging
72         msvideo/                - 16 bit video manager
73         msvideo/msrle32/        - Video codecs
74         mswsock/                - Misc networking
75         netapi32/               - Network interface
76         ntdll/                  - NT implementation of kernel calls
77         odbc32/                 - Open DataBase Connectivity driver manager
78         ole32/                  - 32 bit OLE 2.0 libraries
79         oleaut32/               - 32 bit OLE 2.0 automation
80         olecli/                 - 16 bit OLE client
81         oledlg/                 - OLE 2.0 user interface support
82         olepro32/               - 32 bit OLE 2.0 automation
83         olesvr/                 - 16 bit OLE server
84         opengl32/               - OpenGL implementation (graphics)
85         psapi/                  - Process Status interface
86         qcap/                   - DirectShow runtime
87         quartz/                 - DirectShow runtime
88         rasapi32/               - Remote Access Server interface
89         richedit/               - Rich text editing control
90         rpcrt4/                 - Remote Procedure Call runtime
91         serialui/               - Serial port property pages
92         setupapi/               - Setup interface
93         shdocvw/                - Shell document object and control
94         shfolder/               - Shell folder service
95         shell32/                - COM object implementing shell views
96         shlwapi/                - Shell Light-Weight interface
97         snmpapi/                - SNMP protocol interface (networking)
98         sti/                    - Still Image service
99         tapi32/                 - Telephone interface
100         ttydrv/                 - TTY display driver (Wine specific)
101         twain/                  - TWAIN Imaging device communications
102         url/                    - Internet shortcut shell extension
103         urlmon/                 - URL Moniker allows binding to a URL (like KIO/gnome-vfs)
104         user/                   - Window management, standard controls, etc.
105         uxtheme/                - Theme library
106         version/                - File installation library
107         win32s/                 - 32-bit function access for 16-bit systems
108         winaspi/                - 16 bit Advanced SCSI Peripheral Interface
109         winedos/                - DOS features and BIOS calls (interrupts) (wine specific)
110         wineps/                 - Postscript driver (Wine specific)
111         wininet/                - Internet extensions
112         winmm/                  - Multimedia (16 & 32 bit)
113         winmm/joystick/         - Joystick driver
114         winmm/mcianim/          - MCI animation driver
115         winmm/mciavi/           - MCI video driver
116         winmm/mcicda/           - MCI audio CD driver
117         winmm/mciseq/           - MCI MIDI driver
118         winmm/mciwave/          - MCI wave driver
119         winmm/midimap/          - MIDI mapper
120         winmm/wavemap/          - Audio mapper
121         winmm/winealsa/         - ALSA audio driver
122         winmm/winearts/         - aRts audio driver
123         winmm/wineaudioio/      - audioio audio driver
124         winmm/winejack/         - JACK audio server driver
125         winmm/winenas/          - NAS audio driver
126         winmm/wineoss/          - OSS audio driver
127         winnls/                 - National Language Support
128         winsock/                - Sockets 2.0 (networking)
129         wsock32/                - Sockets 1.1 (networking)
130         wintab32/               - Tablet device interface
131         winspool/               - Printing & Print Spooler
132         wintrust/               - Trust verification interface
133         wow32/                  - WOW subsystem
134         x11drv/                 - X11 display driver (Wine specific)
135
136 Winelib programs (under programs/):
137 -----------------------------------
138
139         avitools/               - AVI information viewer and player
140         clock/                  - Graphical clock
141         cmdlgtst/               - Common dialog tests
142         control/                - Control panel
143         expand/                 - Decompress Lempel-Ziv compressed archive
144         notepad/                - Notepad with RichEdit functionality
145         progman/                - Program manager
146         regedit/                - Registry editor
147         regsvr32/               - Register COM server
148         rpcss/                  - RPC services
149         rundll32/               - Execute DLL functions directly
150         start/                  - Replacement for start.exe
151         uninstaller/            - Remove installed programs
152         view/                   - Metafile viewer
153         wcmd/                   - Command line interface
154         wineboot/               - Wine bootstrap process
155         winecfg/                - Wine configuration utility
156         wineconsole/            - Console
157         winedbg/                - Debugger
158         winefile/               - File manager
159         winemenubuilder/        - Helper program for building Unix menu entries
160         winemine/               - Mine game
161         winepath/               - Translate between Wine and Unix paths
162         winetest/               - Wine testing shell
163         winevdm/                - Wine virtual DOS machine
164         winhelp/                - Help viewer
165         winver/                 - Windows Version Program
166
167
168 Support programs, libraries, etc:
169 ---------------------------------
170
171         documentation/          - some documentation
172         include/                - Windows standard includes
173         include/msvcrt          - MSVC compatible libc headers
174         include/wine            - Wine specific headers
175         libs/                   - the Wine libraries
176         libs/port/              - portability library
177         libs/unicode/           - Unicode support shared
178         libs/uuid/              - Windows-compatible UUID numbers
179         libs/wine/              - Wine bootstrap library
180         libs/wpp/               - C preprocessor
181         loader/                 - the main Wine loader
182         server/                 - the Wine server
183         tools/                  - relay code builder, resource compiler, etc.
184
185
186 Miscellaneous:
187 --------------
188
189 Note: these directories will ultimately get moved into their
190 respective dlls.
191
192         files/                  - KERNEL file I/O
193         misc/                   - KERNEL shell, registry, winsock, etc.
194
195         graphics/               - GDI graphics drivers
196         objects/                - GDI logical objects
197
198         controls/               - USER built-in widgets
199         windows/                - USER window management
200
201
202
203 IMPLEMENTING NEW API CALLS
204 ==========================
205
206 This is the simple version, and covers only Win32. Win16 is slightly
207 uglier, because of the Pascal heritage and the segmented memory model.
208
209 All of the Win32 APIs known to Wine are listed in the .spec file of
210 their corresponding dll. An unimplemented call will look like (from
211 gdi32.spec)
212   269 stub PolyBezierTo
213 To implement this call, you need to do the following four things.
214
215 1. Find the appropriate parameters for the call, and add a prototype to
216 the correct header file. In this case, that means [include/wingdi.h],
217 and it might look like
218   BOOL WINAPI PolyBezierTo(HDC, LPCVOID, DWORD);
219 If the function has both an ASCII and a Unicode version, you need to
220 define both and add a #define WINELIB_NAME_AW declaration. See below
221 for discussion of function naming conventions.
222   
223 2. Modify the .spec file to tell Wine that the function has an
224 implementation, what the parameters look like and what Wine function
225 to use for the implementation. In Win32, things are simple--everything
226 is 32-bits. However, the relay code handles pointers and pointers to
227 strings slightly differently, so you should use 'str' and 'wstr' for
228 strings, 'ptr' for other pointer types, and 'long' for everything else.
229   269 stdcall PolyBezierTo(long ptr long) PolyBezierTo
230 The 'PolyBezierTo' at the end of the line is which Wine function to use
231 for the implementation.
232
233 3. Implement the function as a stub. Once you add the function to the .spec
234 file, you must add the function to the Wine source before it will link.
235 Add a function called 'PolyBezierTo' somewhere. Good things to put
236 into a stub:
237   o a correct prototype, including the WINAPI
238   o header comments, including full documentation for the function and
239     arguments (see documentation/README.documentation)
240   o A FIXME message and an appropriate return value are good things to
241     put in a stub.
242
243   /************************************************************
244    *                    PolyBezierTo   (GDI32.269)  
245    *  
246    * Draw many Bezier curves.
247    *
248    * RETURNS
249    *   Success: Non-zero.
250    *   Failure: FALSE. Use GetLastError() to find the error cause.
251    *
252    * BUGS
253    *   Unimplemented
254    */
255    BOOL WINAPI PolyBezierTo(HDC hdc,     /* [In] Device context to draw to */
256                             LPCVOID p,   /* [In] Array of POINT structs */
257                             DWORD count  /* [In] Number of points in p */
258    ) 
259    {
260       /* tell the user they've got a substandard implementation */
261       FIXME(gdi, ":(%x,%p,%d): stub\n", hdc, p, count);
262
263       /* some programs may be able to compensate, 
264        * if they know what happened 
265        */
266       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);  
267       return FALSE;    /* error value */
268    }
269
270 4. Implement and test the rest of the function.
271
272
273 IMPLEMENTING A NEW DLL
274 ======================
275
276 Generic directions
277 ------------------
278
279 Apart from writing the set of needed .c files, you also need to do the 
280 following:
281
282 1.  Create a directory <MyDll> where to store the implementation of
283     the DLL. This directory has to be put under the dlls/ directory.
284     If the DLL exists under Windows as both 16 and 32 bit DLL, you
285     should have a single directory with both implementations.
286
287 2.  Create the Makefile.in in the ./dlls/<MyDll>/ directory. You can
288     copy an existing Makefile.in from another ./dlls/ subdirectory.
289     You need at least to change the MODULE and C_SRCS macros. 
290
291 3.  Add the directory in ./configure.ac (in AC_OUTPUT macro at the end
292     of the file to trigger the Makefile generation)
293
294 4.  Run ./make_dlls in the dlls directory to update Makefile.in in
295     that directory.
296
297 5.  You can now regenerate ./configure file (with 'make configure')
298     and the various Makefiles (with 'configure; make depend') (run
299     from the top of Wine's tree).
300     You should now have a Makefile file in ./dlls/<MyDll>/
301
302 6.  Create the .spec file for the DLL exported functions in your
303     directory. Refer to 'Implementation of new API calls' earlier in
304     this document for more information on this part.
305
306 7.  You can now start adding .c files. For the .h files, if they are
307     standard Windows one, put them in include/. If they are linked to
308     *your* implementation of the dll, put them in your newly created
309     directory.
310
311 Debug channels
312 --------------
313
314 If you need to create a new debug channel, just add the
315 WINE_DEFAULT_DEBUG_CHANNEL to your .c file(s), and use them. 
316 All the housekeeping will happen automatically.
317
318 Resources
319 ---------
320
321 If you also need to add resources to your DLL, then create the .rc
322 file. Add to your ./dlls/<MyDll>/Makefile.in, in the RC_SRCS macro,
323 the list of .rc files to add to the DLL. See dlls/comctl32/ for an
324 example of this.
325
326 Thunking
327 --------
328
329 If you're building a 16 & 32 bit DLLs pair, then from the 32 bit code
330 you might need to call 16 bit routine. The way to do it to add in the
331 code, fragments like:
332 /* ### Start build ### */
333 extern WORD CALLBACK <PREFIX>_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
334 /* ### stop build ### */
335 Where <PREFIX>_ is an internal prefix for your module. The first
336 parameter is always of type FARPROC16. Then, you can get the regular
337 list of parameters. The _word_wwlll indicates the type of return (long
338 or word) and the size of the parameters (here l=>long, w=>word; which
339 maps to WORD,WORD,LONG,LONG,LONG.
340 You can put several functions between the Start/Stop build pair.
341
342 You can also read the winebuild manpage for more details on this.
343
344 Then, add to ./dlls/<MyDll>/Makefile.in a line like:
345
346 EXTRA_OBJS = $(MODULE).glue.o
347
348 See dlls/winmm/ for an example of this.
349
350 MEMORY AND SEGMENTS
351 ===================
352
353 NE (Win16) executables consist of multiple segments.  The Wine loader
354 loads each segment into a unique location in the Wine processes memory
355 and assigns a selector to that segment.  Because of this, it's not
356 possible to exchange addresses freely between 16-bit and 32-bit code.
357 Addresses used by 16-bit code are segmented addresses (16:16), formed
358 by a 16-bit selector and a 16-bit offset.  Those used by the Wine code
359 are regular 32-bit linear addresses.
360
361 There are four ways to obtain a segmented pointer:
362   - Using the MapLS function (recommended).
363   - Allocate a block of memory from the global heap and use
364     WIN16_GlobalLock to get its segmented address.
365   - Declare the argument as 'segptr' instead of 'ptr' in the spec file
366     for a given API function.
367
368 Once you have a segmented pointer, it must be converted to a linear
369 pointer before you can use it from 32-bit code.  This can be done with
370 the MapSL function.  The linear pointer can then be used freely with
371 standard Unix functions like memcpy() etc. without worrying about 64k
372 boundaries.  Note: there's no easy way to convert back from a linear
373 to a segmented address.
374
375 In most cases, you don't need to worry about segmented address, as the
376 conversion is made automatically by the callback code and the API
377 functions only see linear addresses. However, in some cases it is
378 necessary to manipulate segmented addresses; the most frequent cases
379 are:
380   - API functions that return a pointer
381   - lParam of Windows messages that point to a structure
382   - Pointers contained inside structures accessed by 16-bit code.
383
384 It is usually a good practice to used the type 'SEGPTR' for segmented
385 pointers, instead of something like 'LPSTR' or 'char *'.  As SEGPTR is
386 defined as a DWORD, you'll get a compilation warning if you mistakenly
387 use it as a regular 32-bit pointer.
388
389
390 STRUCTURE PACKING
391 =================
392
393 Under Windows, data structures are tightly packed, i.e. there is no
394 padding between structure members. On the other hand, by default gcc
395 aligns structure members (e.g. WORDs are on a WORD boundary, etc.).
396 This means that a structure like
397
398 struct { BYTE x; WORD y; };
399
400 will take 3 bytes under Windows, but 4 with gcc, because gcc will add a
401 dummy byte between x and y. To have the correct layout for structures
402 used by Windows code, you need to embed the struct within two special
403 #include's which will take care of the packing for you:
404
405 #include "pshpack1.h"
406 struct { BYTE x; WORD y; };
407 #include "poppack1.h"
408
409 For alignment on a 2-byte boundary, there is a "pshpack2.h", etc.
410
411 The use of the WINE_PACKED attribute is obsolete. Please remove these 
412 in favour of the above solution. 
413 Using WINE_PACKED, you would declare the above structure like this:
414
415 struct { BYTE x; WORD y WINE_PACKED; };
416
417 You had to do this every time a structure member is not aligned
418 correctly under Windows (i.e. a WORD not on an even address, or a
419 DWORD on a address that was not a multiple of 4).
420
421
422 NAMING CONVENTIONS FOR API FUNCTIONS AND TYPES
423 ==============================================
424
425 In order to support both Win16 and Win32 APIs within the same source
426 code, the following convention must be used in naming all API
427 functions and types. If the Windows API uses the name 'xxx', the Wine
428 code must use:
429
430  - 'xxx16' for the Win16 version,
431  - 'xxx'   for the Win32 version when no ASCII/Unicode strings are
432    involved,
433  - 'xxxA'  for the Win32 version with ASCII strings,
434  - 'xxxW'  for the Win32 version with Unicode strings.
435
436 If the function has both ASCII and Unicode version, you should then
437 use the macros WINELIB_NAME_AW(xxx) or DECL_WINELIB_TYPE_AW(xxx)
438 (defined in include/windef.h) to define the correct 'xxx' function
439 or type for Winelib. When compiling Wine itself, 'xxx' is _not_
440 defined, meaning that code inside of Wine must always specify
441 explicitly the ASCII or Unicode version.
442
443 If 'xxx' is the same in Win16 and Win32, you can simply use the same
444 name as Windows, i.e. just 'xxx'.  If 'xxx' is Win16 only, you could
445 use the name as is, but it's preferable to use 'xxx16' to make it
446 clear it is a Win16 function.
447
448 Examples:
449
450 typedef struct { /* Win32 ASCII data structure */ } WNDCLASSA;
451 typedef struct { /* Win32 Unicode data structure */ } WNDCLASSW;
452 typedef struct { /* Win16 data structure */ } WNDCLASS16;
453 DECL_WINELIB_TYPE_AW(WNDCLASS);
454
455 ATOM RegisterClass16( WNDCLASS16 * );
456 ATOM RegisterClassA( WNDCLASSA * );
457 ATOM RegisterClassW( WNDCLASSW * );
458 #define RegisterClass WINELIB_NAME_AW(RegisterClass)
459
460 The Winelib user can then say:
461
462     WNDCLASS wc = { ... };
463     RegisterClass( &wc );
464
465 and this will use the correct declaration depending on the definition
466 of the UNICODE symbol.
467
468
469 NAMING CONVENTIONS FOR NON-API FUNCTIONS AND TYPES
470 ==================================================
471
472 Functions and data which are internal to your code (or at least shouldn't be
473 visible to any Winelib or Windows program) should be preceded by
474 an identifier to the module:
475
476 Examples:
477
478 ENUMPRINTERS_GetDWORDFromRegistryA()    (in dlls/winspool/info.c)
479 IAVIFile_fnRelease()                    (in dlls/avifil32/avifile.c)
480 X11DRV_CreateDC()                       (in graphics/x11drv/init.c)
481
482 if you need prototypes for these, there are a few possibilities:
483 - within same source file only:
484   put the prototypes at the top of your file and mark them as prototypes.
485 - within the same module:
486   create a header file within the subdirectory where that module resides,
487   e.g.  graphics/ddraw_private.h
488 - from a totally different module, or for use in winelib:
489   you should never do that. Only exported APIs can be called across
490   module boundaries.
491
492
493 DEBUG MESSAGES
494 ==============
495
496 To display a message only during debugging, you normally write something
497 like this:
498
499         TRACE("abc...");  or
500         FIXME("abc...");  or
501         WARN("abc...");   or
502         ERR("abc...");
503
504 depending on the seriousness of the problem. (documentation/debugging.sgml
505 explains when it is appropriate to use each of them). You need to declare
506 the debug channel name at the top of the file (after the includes) using
507 the WINE_DEFAULT_DEBUG_CHANNEL macro, like so:
508
509         WINE_DEFAULT_DEBUG_CHANNEL(win);
510
511 If your debugging code is more complex than just printf, you can use 
512 the macros:
513
514         TRACE_ON(xxx), WARN_ON(xxx), ERR_ON(xxx) and FIXME_ON(xxx) 
515
516 to test if the given channel is enabled. Thus, you can write:
517
518         if (TRACE_ON(win)) DumpSomeStructure(&str);
519
520 Don't worry about the inefficiency of the test. If it is permanently 
521 disabled (that is TRACE_ON(win) is 0 at compile time), the compiler will 
522 eliminate the dead code.
523
524 For more info about debugging messages, read:
525
526 http://www.winehq.org/site/docs/wine-devel/debugging
527
528
529 MORE INFO
530 =========
531
532 1. There is a FREE online version of the MSDN library (including
533    documentation for the Win32 API) on http://msdn.microsoft.com/
534    or http://www.msdn.com/
535
536 2. Windows apilist:  http://www.mentalis.org/apilist/apilist.php
537
538 3. http://www.sonic.net/~undoc/bookstore.html
539
540 4. In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
541
542 5. www.geocities.com/SiliconValley/4942/