Developer Hints

From WineHQ Wiki
Revision as of 20:31, 18 January 2016 by RosanneDiMesio (talk | contribs) (Initial import; some markup fixes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This document should help new developers get started.

Source tree structure

See SourceTreeStructure page.

Implementing new API calls

This is the simple version, and covers only Win32. Win16 is slightly uglier, because of the Pascal heritage and the segmented memory model. All of the Win32 APIs known to Wine are listed in the .spec file of their corresponding dll. An unimplemented call will look like (from `gdi32.spec`)

@ stub PolyPatBlt

To implement this call, you need to do the following four things:

1. Find the appropriate parameters for the call, and add a prototype to the correct header file. In this case, that means `include/wingdi.h`, and it might look like


If the function has both an ASCII and a Unicode version, you need to define both and add a `#define WINELIB_NAME_AW` declaration. See below for discussion of function naming conventions.

2. Modify the .spec file to tell Wine that the function has an implementation, what the parameters look like and what Wine function to use for the implementation. In Win32, things are simple--everything is 32-bits. However, the relay code handles pointers and pointers to strings slightly differently, so you should use 'str' and 'wstr' for strings, 'ptr' for other pointer types, and 'long' for everything else:

@ stdcall PolyPatBlt(long ptr long)

The `PolyPatBlt` at the end of the line is which Wine function to use for the implementation.

3. Implement the function as a stub. Once you add the function to the .spec file, you must add the function to the Wine source before it will link. Add a function called `PolyPatBlt` somewhere. Good things to put into a stub:

  1. a correct prototype, including the WINAPI
  2. header comments, including full documentation for the function and arguments
  3. A FIXME message and an appropriate return value are good things to put in a stub. NOTE: When submitting documentation changes, you must clearly state that when creating your patch that you did not copy the function documentation from MSDN. When implementing a new function it is fine to look at the API documentation on MSDN however the api documentation must be written in your own words.
   *                    PolyPatBlt   (GDI32.@)
   * Draw many Bezier curves.
   *   hdc   [I] Device context to draw to
   *   p     [I] Array of POINT structs
   *   count [I] Number of points in p
   *   Success: Non-zero.
   *   Failure: FALSE. Use GetLastError() to find the error cause.
   * BUGS
   *   Unimplemented
   BOOL WINAPI PolyPatBlt(HDC hdc, LPCVOID p, DWORD count)
       /* tell the user they've got a substandard implementation */
       FIXME("(%x,%p,%d): stub\n", hdc, p, count);
       /* some programs may be able to compensate,
        * if they know what happened
       return FALSE;    /* error value */

4. Implement and test the rest of the function:

  1. Add a test to the Wine test suite, when possible.
  2. Learn from the test results, what Windows does (MSDN is sometimes incomplete or wrong).
  3. It's a good idea to ask for test results for other Windows versions in wine-devel.
  4. Try to limit the supported formats/versions in a function for a single patch.
  5. Add support for additional formats/versions in the next patches, when needed.

Implementing a new DLL

Generic directions

Apart from writing the set of needed .c files, you also need to do the following:

  1. Create a directory `<MyDll>` where to store the implementation of the DLL. This directory has to be put under the `dlls/` directory. If the DLL exists under Windows also as a 16 bit DLL, use a seperate directory with `16` added to the extension for the 16 bit implementation (Example: `<MyDll.dll16>`).
  2. Create the `` in the `./dlls/<MyDll>/` directory. You can copy an existing `` from another `./dlls/` subdirectory. You need at least to change the `MODULE` and `C_SRCS` macros.
  3. Create the .spec file for the DLL exported functions in your directory. Refer to [:Developers-Hints#head-d3dc3e89d2b8a4cef2987135b97dab3aa67c337d:Implementation of new API calls] earlier in this document for more information on this part.
  4. Important note, when using git: you must do git add dlls/<MyDll>/ before you call ./tools/make_makefiles, or make_makefiles won't know about your dll
  5. Call `./tools/make_makefiles` from the top of the Wine tree. Your new dll is now integrated in the Wine build environment. Verify this by making sure the name of your dll directory appears in
  6. If you added new test(s) in, run `autoheader` to add the needed variables to include/
  7. Regenerate the `./configure` script with `autoconf`.
  8. Run `./configure` from the top of the Wine tree. You should now have a `Makefile` file in `./dlls/<MyDll>/`
  9. To include a Windows-DLL in the official Wine Tree, please submit the smallest possible implementation first:
    1. Only "DLLMain" in a single .c file
    2. the .spec file with the exported functions declared as stub
    3. your ``
    4. do not include any autogenerated source
  10. When your new DLL is in the Wine Tree, please add the DLL and a short Description at the top of SourceTreeStructure
  11. You can now start adding .c files. For the .h files, if they are standard Windows one, put them in include/. If they are linked to your implementation of the dll, put them in your newly created directory
  12. Pick only one Function or a small set of Functions for a Patch:
    1. reduced Patch-Size
    2. reduced complexity
    3. increased motivation to review
  13. Add a set of tests as explained below
  14. You should not include any autogenerated source, that was created from 'autoconf' or a tool inside 'tools/' (`make_makefiles` or `winedump` as examples), when you submit your Patch to

Debug channels

If you need to create a new debug channel, just add


  1. include "wine/debug.h"

}}} and macro

{{{ WINE_DEFAULT_DEBUG_CHANNEL(mydll) }}} to your .c file(s), and use them. (The prefered Name for the debug-channel is your DLL-Name) All the housekeeping will happen automatically.

See the Examples in [:Developers-Hints#head-bbbf29b5c24f7a234f1da2430a13bc79e9cd3037:Debug Messages] later in this Document.


The is a bit different to the generated Makefile. Have a good look at an example and use the right values otherwise it won't necessarily be recognised. If your `.dlls/<MyDll>/<MyDll>.spec` exports any functions then you need a

{{{ IMPORTLIB = <MyDll> }}} statement. Otherwise leave it out as you will get warnings that the dll does not export anything.


If you also need to add resources to your DLL, then create the .rc file. Add to your `./dlls/<MyDll>/`, in the `RC_SRCS` macro, the list of .rc files to add to the DLL. See `dlls/comctl32/` for an example of this.

Adding a tests directory

If you want to add tests to an existing dll and there is already a tests directory things should be clear enough. However if the dll does not yet have a tests subdirectory you will need to add one and set it up.

1. In the dll's subdirectory create a subdirectory named tests.
1. In that tests subdirectory copy a from some other tests subdirectory and edit it.
 * Change the TESTDLL to the dll you are testing
 * Change the IMPORTS to the dll you are testing and also anything it requires
 * EXTRALIBS is sometimes needed (DLL use COM as Example)
 * Change the C_SRCS entry to the list of c source files you will provide containing the tests.
 * I do not yet know what the generated.c file is all about.
1. Write the c source files that you added to the C_SRCS entry above.
1. Important note, when using git: `` must be added to the index before you call `./tools/make_makefiles`
1. Call `./tools/make_makefiles` from the top of the Wine tree. Your new tests are now integrated in the Wine build environment
1. Regenerate the `./configure` script with `autoconf`.
1. Run `./configure` from the top of the Wine tree. You should now have a `Makefile` file in `./dlls/<MyDll>/tests/`.
1. You chould now be able to run 'make depend' and `make`.
1. With a recent Cross-Compiler (MinGW), you can build your tests as Windows-Binary.
1. Make sure, your tests works without failure on different Systems. (Windows and wine, and there is a 'todo_wine' - macro).
1. Ask other People for Help to run your tests.
1. You should not include any autogenerated code, that was created from 'autoconf' or a tool inside 'tools/' (`tools/make_makefiles` as example), when you submit your Patch to

Writing Conformance Tests

Wine uses test-driven development to a large extent; we write a test, make sure it passes on Windows, and then get it to pass on Wine by fixing Wine.


See also WritingConformanceTests for more notes.

Memory and segments

NE (Win16) executables consist of multiple segments. The Wine loader loads each segment into a unique location in the Wine processes memory and assigns a selector to that segment. Because of this, it's not possible to exchange addresses freely between 16-bit and 32-bit code. Addresses used by 16-bit code are segmented addresses (16:16), formed by a 16-bit selector and a 16-bit offset. Those used by the Wine code are regular 32-bit linear addresses.

There are three ways to obtain a segmented pointer:

i. Using the MapLS function (recommended).
i. Allocate a block of memory from the global heap and use `WIN16_GlobalLock` to get its segmented address.
i. Declare the argument as 'segptr' instead of 'ptr' in the spec file for a given API function.

Once you have a segmented pointer, it must be converted to a linear pointer before you can use it from 32-bit code. This can be done with the `MapSL` function. The linear pointer can then be used freely with standard Unix functions like `memcpy()` etc. without worrying about 64k boundaries. Note: there's no easy way to convert back from a linear to a segmented address.

In most cases, you don't need to worry about segmented address, as the conversion is made automatically by the callback code and the API functions only see linear addresses. However, in some cases it is necessary to manipulate segmented addresses; the most frequent cases are:

i. API functions that return a pointer
i. lParam of Windows messages that point to a structure
i. Pointers contained inside structures accessed by 16-bit code.

It is usually a good practice to used the type `SEGPTR` for segmented pointers, instead of something like `LPSTR` or `char *`. As `SEGPTR` is defined as a `DWORD`, you'll get a compilation warning if you mistakenly use it as a regular 32-bit pointer.

Structure packing

By default both Microsoft and gcc compilers align structure members (e.g. WORDs are on a WORD boundary, etc.). This means that a structure like

{{{ struct { BYTE x; WORD y; }; }}} will take 4 bytes, because a compiler will add a dummy byte between x and y. Sometimes to have the correct layout for structures used by Windows code, you need to embed the struct within two special #include's which will take care of the packing for you:


  1. include "pshpack1.h"

struct { BYTE x; WORD y; };

  1. include "poppack1.h"

}}} For alignment on a 2-byte boundary, there is a `pshpack2.h`, etc.

Naming conventions for API functions and types

In order to support both Win16 and Win32 APIs within the same source code, the following convention must be used in naming all API functions and types. If the Windows API uses the name 'xxx', the Wine code must use:

. {{{
'xxx16'   for the Win16 version,
'xxx'     for the Win32 version when no strings are involved,
'xxxA'    for the Win32 version with ASCII strings,
'xxxW'    for the Win32 version with Unicode strings.

}}} If the function has both ASCII and Unicode version, you should then use the macros `WINELIB_NAME_AW(xxx)` or `DECL_WINELIB_TYPE_AW(xxx)` (defined in `include/windef.h`) to define the correct 'xxx' function or type for Winelib. When compiling Wine itself, 'xxx' is not defined, meaning that code inside of Wine must always specify explicitly the ASCII or Unicode version.

If 'xxx' is the same in Win16 and Win32, you can simply use the same name as Windows, i.e. just 'xxx'. If 'xxx' is Win16 only, you could use the name as is, but it's preferable to use 'xxx16' to make it clear it is a Win16 function.

For example:

{{{ typedef struct { /* Win32 ASCII data structure */ } WNDCLASSA; typedef struct { /* Win32 Unicode data structure */ } WNDCLASSW; typedef struct { /* Win16 data structure */ } WNDCLASS16; DECL_WINELIB_TYPE_AW(WNDCLASS); ATOM RegisterClass16( WNDCLASS16 * ); ATOM RegisterClassA( WNDCLASSA * ); ATOM RegisterClassW( WNDCLASSW * );

  1. define RegisterClass WINELIB_NAME_AW(RegisterClass)

}}} The Winelib user can then say:


   WNDCLASS wc = { ... };
   RegisterClass( &wc );

}}} and this will use the correct declaration depending on the definition of the UNICODE symbol.

Debug messages

To display a message only during debugging, you normally write something like this:


       TRACE("abc...\n");  or
       FIXME("abc...\n");  or
       WARN("abc...\n");   or

}}} depending on the seriousness of the problem. You need to declare the debug channel name at the top of the file (after the includes) using the `WINE_DEFAULT_DEBUG_CHANNEL` macro, like so:



}}} If your debugging code is more complex than just printf, you can use the macros:


       TRACE_ON(xxx), WARN_ON(xxx), ERR_ON(xxx) and FIXME_ON(xxx)

}}} to test if the given channel is enabled. Thus, you can write:


       if (TRACE_ON(win)) DumpSomeStructure(&str);

}}} Don't worry about the inefficiency of the test. If it is permanently disabled (that is `TRACE_ON(win)` is 0 at compile time), the compiler will eliminate the dead code.

For more info about debugging messages,

Translating resource files

Nowadays Wine is completely localized through standard gettext PO files. If you check out the Wine source you will find these in the po/ directory. See the ["Translating"] page for more details.

Coding Conventions

Here are a couple of things to bear in mind in any patches you make.

* Don't add any application-specific hacks
* Try to keep line widths under 80 characters
* Avoid using string literals in "wide" functions for proper UnicodeSupport
* Limit type casts as much as possible (e.g. don't cast an LPVOID to a DWORD *)

Using only C89/C90 compliant code

 * Use only C-style comments, not C++ ones (use /* */, never // )
 * Use `void` when defining parameter-less functions

{{{ int foo() { } /* Incorrect */ int foo(void) { } /* Much better */ }}}

 * Always put variable declarations at the beginning of a block

{{{ int bar1(void) {

   int number1 = 5;            /* Not C89 compliant */


int bar2(void) {

   int number2 = 17;           /* Much better */
   int number3 = 550;          /* Ditto */

} }}}

 * Avoid all compiler extensions

{{{ ch += offset ?: size /* GCC-specific extension */ dh += offset ? offset : size /* Standards compliant */ }}}

 * Use types with standard definitions

{{{ long wrongx; /* long is different in Win64 and Unix64 */ LONG rightx; /* LONG is the same on both platforms */ }}}

 * Use WCHAR and array syntax for string literals

{{{ const WCHAR str1[] = L"Hello"; /* Double-quote strings use wchar_t type */ const WCHAR str2[] = { /* Tedious, but correct */


}; }}}

More info

* Win32 API documentation on MSDN:
* Analyse by Geoff Chappell:
* Undocumented Windows 2000 Secrets :
* In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
* - Windows Data Types