WineHQ

Developer Hints: Difference between revisions

(Initial import; some markup fixes)
 
(Add a link to wide-string creation utility)
(21 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This document should help new developers get started.
This document should help new developers get started.
 
== Source tree structure ==
== Source tree structure ==
''See SourceTreeStructure page.''
''See [[Source Tree Structure]] page.''


== Implementing new API calls ==
== Implementing new API calls ==
Line 71: Line 72:
<li>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>`).
<li>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>`).
<li>Create the `Makefile.in` in the `./dlls/<MyDll>/` directory. You can copy an existing `Makefile.in` from another `./dlls/` subdirectory. You need at least to change the `MODULE` and `C_SRCS` macros.
<li>Create the `Makefile.in` in the `./dlls/<MyDll>/` directory. You can copy an existing `Makefile.in` from another `./dlls/` subdirectory. You need at least to change the `MODULE` and `C_SRCS` macros.
<li>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.
<li>Create the .spec file for the DLL exported functions in your directory. Refer to '''[[Developers-Hints#Implementing_new_API_calls|Implementing new API calls]]''' earlier in this document for more information on this part.
<li> '''Important note, when using git:''' you must do '''git add dlls/<MyDll>/Makefile.in''' before you call '''./tools/make_makefiles''', or make_makefiles won't know about your dll
<li> '''Important note, when using git:''' you must do '''git add dlls/<MyDll>/Makefile.in''' before you call '''./tools/make_makefiles''', or make_makefiles won't know about your dll
<li> 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 configure.ac.
<li> 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 configure.ac.
Line 95: Line 96:
<li>'''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 winehq.org</li>
<li>'''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 winehq.org</li>
</ol>
</ol>
=== Debug channels ===
=== Debug channels ===
If you need to create a new debug channel, just add
If you need to create a new debug channel, just add


{{{
#include "wine/debug.h"
#include "wine/debug.h"
 
}}}
and macro
and macro


{{{
WINE_DEFAULT_DEBUG_CHANNEL(mydll)
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.
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.
See the Examples in [[Developers-Hints#Debug_messages|Debug Messages]] later in this Document.


=== Makefiles ===
=== Makefiles ===
The Makefile.in 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
The Makefile.in 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 <tt>.dlls/<MyDll>/<MyDll>.spec</tt> exports any functions then you need a
 
IMPORTLIB = <MyDll>


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


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


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


== Writing Conformance Tests ==
== 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.
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 http://www.winehq.org/site/docs/winedev-guide/testing
See http://www.winehq.org/site/docs/winedev-guide/testing


See also WritingConformanceTests for more notes.
See also [[Conformance Tests]] for more notes.


== Memory and segments ==
== Memory and segments ==
Line 153: Line 152:


There are three ways to obtain a segmented pointer:
There are three ways to obtain a segmented pointer:
<ol style="list-style-type:lower-roman">
<li> Using the MapLS function (recommended).</li>
<li>Allocate a block of memory from the global heap and use <tt>WIN16_GlobalLock</tt> to get its segmented address.</li>
<li>Declare the argument as <tt>segptr</tt> instead of 'ptr' in the spec file for a given API function.</li>
</ol>


i. Using the MapLS function (recommended).
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 <tt>MapSL</tt> function.  The linear pointer can then be used freely with standard Unix functions like <tt>memcpy()</tt> etc. without worrying about 64k boundaries.  Note: there's no easy way to convert back from a linear to a segmented address.
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:
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:
<ol style="list-style-type:lower-roman">
<li> API functions that return a pointer</li>
<li>lParam of Windows messages that point to a structure</li>
<li> Pointers contained inside structures accessed by 16-bit code.</li>
</ol>


i. API functions that return a pointer
It is usually a good practice to used the type `SEGPTR` for segmented pointers, instead of something like <tt>LPSTR</tt> or <tt>char *</tt>.  As <tt>SEGPTR</tt> is defined as a <tt>DWORD</tt>, you'll get a compilation warning if you mistakenly use it as a regular 32-bit 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 ==
== 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
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; };
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:
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:


{{{
#include "pshpack1.h"
#include "pshpack1.h"
struct { BYTE x; WORD y; };
struct { BYTE x; WORD y; };
#include "poppack1.h"
#include "poppack1.h"
 
}}}
For alignment on a 2-byte boundary, there is a <tt>pshpack2.h</tt>, etc.
For alignment on a 2-byte boundary, there is a `pshpack2.h`, etc.


== Naming conventions for API functions and types ==
== 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:
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:


. {{{
<pre>
  'xxx16'  for the Win16 version,
  'xxx16'  for the Win16 version,
  'xxx'    for the Win32 version when no strings are involved,
  'xxx'    for the Win32 version when no strings are involved,
  'xxxA'    for the Win32 version with ASCII strings,
  'xxxA'    for the Win32 version with ASCII strings,
  'xxxW'    for the Win32 version with Unicode strings.
  'xxxW'    for the Win32 version with Unicode strings.
}}}
</pre>
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 the function has both ASCII and Unicode version, you should then use the macros <tt>WINELIB_NAME_AW(xxx)</tt> or <tt>DECL_WINELIB_TYPE_AW(xxx)</tt> (defined in <tt>include/windef.h</tt>) 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.
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.
Line 196: Line 197:
For example:
For example:


{{{
<pre>
typedef struct { /* Win32 ASCII data structure */ } WNDCLASSA;
typedef struct { /* Win32 ASCII data structure */ } WNDCLASSA;
typedef struct { /* Win32 Unicode data structure */ } WNDCLASSW;
typedef struct { /* Win32 Unicode data structure */ } WNDCLASSW;
Line 205: Line 206:
ATOM RegisterClassW( WNDCLASSW * );
ATOM RegisterClassW( WNDCLASSW * );
#define RegisterClass WINELIB_NAME_AW(RegisterClass)
#define RegisterClass WINELIB_NAME_AW(RegisterClass)
}}}
</pre>
 
The Winelib user can then say:
The Winelib user can then say:


{{{
<pre>
     WNDCLASS wc = { ... };
     WNDCLASS wc = { ... };
     RegisterClass( &wc );
     RegisterClass( &wc );
}}}
</pre>
 
and this will use the correct declaration depending on the definition of the UNICODE symbol.
and this will use the correct declaration depending on the definition of the UNICODE symbol.


== Debug messages ==
== Debug messages ==
To display a message only during debugging, you normally write something like this:
To display a message only during debugging, you normally write something like this:
 
<pre>
{{{
         TRACE("abc...\n");  or
         TRACE("abc...\n");  or
         FIXME("abc...\n");  or
         FIXME("abc...\n");  or
         WARN("abc...\n");  or
         WARN("abc...\n");  or
         ERR("abc...\n");
         ERR("abc...\n");
}}}
</pre>
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:
 
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 <tt>WINE_DEFAULT_DEBUG_CHANNEL</tt> macro, like so:


{{{
         WINE_DEFAULT_DEBUG_CHANNEL(win);
         WINE_DEFAULT_DEBUG_CHANNEL(win);
}}}
 
If your debugging code is more complex than just printf, you can use  the macros:
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)
         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:
to test if the given channel is enabled. Thus, you can write:


{{{
         if (TRACE_ON(win)) DumpSomeStructure(&str);
         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.
Don't worry about the inefficiency of the test. If it is permanently  disabled (that is <tt>TRACE_ON(win)</tt> is 0 at compile time), the compiler will  eliminate the dead code.


For more info about debugging messages, http://www.winehq.org/site/docs/winedev-guide/debugging
For more info about debugging messages, http://www.winehq.org/site/docs/winedev-guide/debugging


== Translating resource files ==
== 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 [http://source.winehq.org/git/wine.git/?a=tree;f=po;hb=HEAD po/] directory. See the [[Translating]] page for more details.
== Using only C89-compliant code ==
:''See also [[Submitting Patches#Code guidelines]]''
For compatibility with a wide range of compilers (both new and old), code in Wine should be written to the original C89 standard with portability in mind:


Nowadays Wine is completely localized through standard gettext PO files. If you check out the Wine source you will find these in the [http://source.winehq.org/git/wine.git/?a=tree;f=po;hb=HEAD po/] directory. See the ["Translating"] page for more details.
* Use only C-style comments, not C++ ones (use <code>/* */</code>, never <code>//</code>)
* Use <code>void</code> when defining parameter-less functions


== Coding Conventions ==
  int foo() { }                  /* Incorrect */
Here are a couple of things to bear in mind in any patches you make.
  int foo(void) { }              /* Much better */
  * 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
* http://www.winehq.org/site/docs/winedev-guide/codingpractice
* Limit type casts as much as possible (e.g. don't cast an LPVOID to a DWORD *)


=== Using only C89/C90 compliant code ===
* Always put variable declarations at the beginning of a block
  * Use only C-style comments, not C++ ones (use /* */, never // )
<pre>
  * 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 bar1(void)
{
{
     dosomething();
     do_something();
     int number1 = 5;            /* Not C89 compliant */
     int number1 = 5;            /* Not C89 compliant */
}
}
Line 273: Line 268:
     int number2 = 17;          /* Much better */
     int number2 = 17;          /* Much better */
     int number3 = 550;          /* Ditto */
     int number3 = 550;          /* Ditto */
     dosomething();
     do_something();
}
}
}}}
</pre>
  * Avoid all compiler extensions
* Avoid all compiler extensions
{{{
 
ch += offset ?: size            /* GCC-specific extension */
ch += offset ?: size            /* GCC-specific extension */
dh += offset ? offset : size    /* Standards compliant */
dh += offset ? offset : size    /* Standards compliant */
}}}
 
  * Use types with standard definitions
* Use types with standard definitions
{{{
 
long wrongx;                   /* long is different in Win64 and Unix64 */
long wrong;                     /* long is different in Win64 and Unix64 */
LONG rightx;                   /* LONG is the same on both platforms */
LONG right;                     /* LONG is the same on both platforms */
}}}
 
  * Use WCHAR and array syntax for string literals
* Use WCHAR and array syntax for Unicode string literals ([https://gitlab.com/mywinetools/mywinetools/raw/master/wstr.py helpful tool])
{{{
<pre>
const WCHAR str1[] = L"Hello";  /* Double-quote strings use wchar_t type */
const WCHAR str1[] = L"Hello";  /* Won't compile */
const WCHAR str2[] = {          /* Tedious, but correct */
const WCHAR str2[] = {          /* Tedious, but correct */
     'H','e','l','l','o',0
     'H','e','l','l','o',0
};
};
}}}
</pre>


== More info ==
== More info ==
* Win32 API documentation on MSDN: http://msdn.microsoft.com/
* Win32 API documentation on MSDN: http://msdn.microsoft.com/
* Analyse by Geoff Chappell: http://www.geoffchappell.com/
* Analyse by Geoff Chappell: http://www.geoffchappell.com/
* http://www.sonic.net/~undoc/bookstore.html
* http://www.sonic.net/~undoc/bookstore.html
* http://www.geocities.com/SiliconValley/4942/
* http://www.geocities.com/SiliconValley/4942/
* Undocumented Windows 2000 Secrets : http://www.rawol.com/?topic=41
* Undocumented Windows 2000 Secrets : http://undocumented.rawol.com/
* In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
* In 1993 Dr. Dobbs Journal published a column called "Undocumented Corner".
* http://msdn2.microsoft.com/en-us/library/cc230273.aspx - Windows Data Types
* http://msdn2.microsoft.com/en-us/library/cc230273.aspx - Windows Data Types
 
----
----
CategoryDevelopment
[[Category:Development]]

Revision as of 14:33, 22 November 2016

This document should help new developers get started.

Source tree structure

See Source Tree Structure 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

BOOL WINAPI PolyPatBlt(HDC, LPCVOID, DWORD);

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.
   *
   * PARAMS
   *   hdc   [I] Device context to draw to
   *   p     [I] Array of POINT structs
   *   count [I] Number of points in p
   *
   * RETURNS
   *   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
        */
       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
       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 `Makefile.in` in the `./dlls/<MyDll>/` directory. You can copy an existing `Makefile.in` 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 Implementing 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>/Makefile.in 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 configure.ac.
  6. If you added new test(s) in configure.ac, run `autoheader` to add the needed variables to include/config.in.h
  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 `Makefile.in`
    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 winehq.org

Debug channels

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

#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 Debug Messages later in this Document.

Makefiles

The Makefile.in 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.

Resources

If you also need to add resources to your DLL, then create the .rc file. Add to your ./dlls/<MyDll>/Makefile.in, 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.
  2. In that tests subdirectory copy a Makefile.in 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.
  3. Write the c source files that you added to the C_SRCS entry above.
  4. Important note, when using git: Makefile.in must be added to the index before you call ./tools/make_makefiles
  5. Call ./tools/make_makefiles from the top of the Wine tree. Your new tests are now integrated in the Wine build environment
  6. Regenerate the ./configure script with autoconf.
  7. Run ./configure from the top of the Wine tree. You should now have a Makefile file in ./dlls/<MyDll>/tests/.
  8. You should now be able to run make depend and make.
  9. With a recent Cross-Compiler (MinGW), you can build your tests as Windows-Binary.
  10. Make sure, your tests works without failure on different Systems. (Windows and wine, and there is a todo_wine - macro).
  11. Ask other People for Help to run your tests.
  12. 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 winehq.org.

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 http://www.winehq.org/site/docs/winedev-guide/testing

See also Conformance Tests 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:

  1. Using the MapLS function (recommended).
  2. Allocate a block of memory from the global heap and use WIN16_GlobalLock to get its segmented address.
  3. 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:

  1. API functions that return a pointer
  2. lParam of Windows messages that point to a structure
  3. 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:

#include "pshpack1.h"
struct { BYTE x; WORD y; };
#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 * );
#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
        ERR("abc...\n");

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:

       WINE_DEFAULT_DEBUG_CHANNEL(win);

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, http://www.winehq.org/site/docs/winedev-guide/debugging

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.

Using only C89-compliant code

See also Submitting Patches#Code guidelines

For compatibility with a wide range of compilers (both new and old), code in Wine should be written to the original C89 standard with portability in mind:

  • 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)
{
    do_something();
    int number1 = 5;            /* Not C89 compliant */
}

int bar2(void)
{
    int number2 = 17;           /* Much better */
    int number3 = 550;          /* Ditto */
    do_something();
}
  • Avoid all compiler extensions
ch += offset ?: size            /* GCC-specific extension */
dh += offset ? offset : size    /* Standards compliant */
  • Use types with standard definitions
long wrong;                     /* long is different in Win64 and Unix64 */
LONG right;                     /* LONG is the same on both platforms */
  • Use WCHAR and array syntax for Unicode string literals (helpful tool)
const WCHAR str1[] = L"Hello";  /* Won't compile */
const WCHAR str2[] = {          /* Tedious, but correct */
    'H','e','l','l','o',0
};

More info


This page was last edited on 22 November 2016, at 14:33.