<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Wine Developer's Guide</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"></HEAD
><BODY
CLASS="BOOK"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="BOOK"
><A
NAME="INDEX"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="TITLE"
><A
NAME="AEN2"
>Wine Developer's Guide</A
></H1
><HR></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
>I. <A
HREF="#PART-ONE"
>Developing Wine</A
></DT
><DD
><DL
><DT
><A
HREF="#DEBUGGER"
>Debugging Wine</A
></DT
><DD
><DL
><DT
><A
HREF="#DBG-INTRO"
>Introduction</A
></DT
><DT
><A
HREF="#DBG-MODES"
>WineDbg's modes of invocation</A
></DT
><DT
><A
HREF="#WINE-DEBUGGER"
>Using the Wine Debugger</A
></DT
><DT
><A
HREF="#MEMORY-ADDRESSES"
>Useful memory addresses</A
></DT
><DT
><A
HREF="#DBG-CONFIG"
>Configuration</A
></DT
><DT
><A
HREF="#DBG-EXPR"
>WineDbg Expressions and Variables</A
></DT
><DT
><A
HREF="#DBG-COMMANDS"
>WineDbg Command Reference</A
></DT
><DT
><A
HREF="#DBG-OTHERS"
>Other debuggers</A
></DT
></DL
></DD
><DT
><A
HREF="#DEBUGGING"
>Debug Logging</A
></DT
><DD
><DL
><DT
><A
HREF="#DBG-CLASSES"
>Debugging classes</A
></DT
><DT
><A
HREF="#DBG-CHANNELS"
>Debugging channels</A
></DT
><DT
><A
HREF="#DBG-CHECKING"
>Are we debugging?</A
></DT
><DT
><A
HREF="#DBG-HELPERS"
>Helper functions</A
></DT
><DT
><A
HREF="#DBG-CONTROL"
>Controlling the debugging output</A
></DT
><DT
><A
HREF="#DBG-COMPILING"
>Compiling Out Debugging Messages</A
></DT
><DT
><A
HREF="#DBG-NOTES"
>A Few Notes on Style</A
></DT
></DL
></DD
><DT
><A
HREF="#OTHERDEBUG"
>Other debugging techniques</A
></DT
><DD
><DL
><DT
><A
HREF="#HARDWARE-TRACE"
>Doing A Hardware Trace</A
></DT
><DT
><A
HREF="#UNDOC-FUNC"
>Understanding undocumented APIs</A
></DT
><DT
><A
HREF="#AEN1348"
>How to do regression testing using Git</A
></DT
><DT
><A
HREF="#AEN1374"
>Which code has been tested?</A
></DT
></DL
></DD
><DT
><A
HREF="#CODINGPRACTICE"
>Coding Practice</A
></DT
><DD
><DL
><DT
><A
HREF="#PATCH-FORMAT"
>Patch Format</A
></DT
><DT
><A
HREF="#STYLE-NOTES"
>Some notes about style</A
></DT
><DT
><A
HREF="#PATCH-QUALITY"
>Quality Assurance</A
></DT
><DT
><A
HREF="#PORTING"
>Porting Wine to new Platforms</A
></DT
><DT
><A
HREF="#ADDING-LANGUAGES"
>Adding New Languages</A
></DT
></DL
></DD
><DT
><A
HREF="#TESTING"
>Writing Conformance tests</A
></DT
><DD
><DL
><DT
><A
HREF="#TESTING-INTRO"
>Introduction</A
></DT
><DT
><A
HREF="#TESTING-WHAT"
>What to test for?</A
></DT
><DT
><A
HREF="#TESTING-WINE"
>Running the tests in Wine</A
></DT
><DT
><A
HREF="#CROSS-COMPILING-TESTS"
>Cross-compiling the tests with MinGW</A
></DT
><DT
><A
HREF="#TESTING-WINDOWS"
>Building and running the tests on Windows</A
></DT
><DT
><A
HREF="#TESTING-TEST"
>Inside a test</A
></DT
><DT
><A
HREF="#TESTING-ERROR-MESSAGES"
>Writing good error messages</A
></DT
><DT
><A
HREF="#TESTING-PLATFORMS"
>Handling platform issues</A
></DT
></DL
></DD
><DT
><A
HREF="#DOCUMENTATION"
>Documenting Wine</A
></DT
><DD
><DL
><DT
><A
HREF="#DOC-OVERVIEW"
>An Overview Of Wine Documentation</A
></DT
><DT
><A
HREF="#API-DOCS"
>Writing Wine API Documentation</A
></DT
><DT
><A
HREF="#WINE-DOCBOOK"
>The Wine DocBook System</A
></DT
></DL
></DD
></DL
></DD
><DT
>II. <A
HREF="#PART-TWO"
>Wine Architecture</A
></DT
><DD
><DL
><DT
><A
HREF="#ARCHITECTURE"
>Overview</A
></DT
><DD
><DL
><DT
><A
HREF="#BASIC-OVERVIEW"
>Wine Overview</A
></DT
><DT
><A
HREF="#AEN2563"
>Standard Windows Architectures</A
></DT
><DT
><A
HREF="#AEN2584"
>Wine architecture</A
></DT
></DL
></DD
><DT
><A
HREF="#AEN2768"
>Kernel modules</A
></DT
><DD
><DL
><DT
><A
HREF="#INITIALIZATION"
>The Wine initialization process</A
></DT
><DT
><A
HREF="#AEN2853"
>Detailed memory management</A
></DT
><DT
><A
HREF="#AEN2884"
>Multi-processing in Wine</A
></DT
><DT
><A
HREF="#THREADING"
>Multi-threading in Wine</A
></DT
><DT
><A
HREF="#SEH"
>Structured Exception Handling</A
></DT
><DT
><A
HREF="#AEN3062"
>File management</A
></DT
><DT
><A
HREF="#NTDLL"
><TT
CLASS="FILENAME"
>NTDLL</TT
> module</A
></DT
><DT
><A
HREF="#AEN3500"
><TT
CLASS="FILENAME"
>KERNEL32</TT
> Module</A
></DT
></DL
></DD
><DT
><A
HREF="#AEN3750"
>Graphical modules</A
></DT
><DD
><DL
><DT
><A
HREF="#AEN3752"
>GDI Module</A
></DT
></DL
></DD
><DT
><A
HREF="#AEN3764"
>Windowing system</A
></DT
><DD
><DL
><DT
><A
HREF="#AEN3766"
>USER Module</A
></DT
><DT
><A
HREF="#AEN3913"
>X Windows System interface</A
></DT
></DL
></DD
><DT
><A
HREF="#OLE"
>COM in Wine</A
></DT
><DD
><DL
><DT
><A
HREF="#COM-WRITING"
>Writing COM Components for Wine</A
></DT
><DT
><A
HREF="#DCOM-1"
>A brief introduction to DCOM in Wine</A
></DT
></DL
></DD
><DT
><A
HREF="#OPENGL"
>Wine and OpenGL</A
></DT
><DD
><DL
><DT
><A
HREF="#OPENGL-REQUIRED"
>What is needed to have OpenGL support in Wine</A
></DT
><DT
><A
HREF="#OPENGL-WORKS"
>How it all works</A
></DT
><DT
><A
HREF="#OPENGL-PROBLEMS"
>Known problems</A
></DT
></DL
></DD
><DT
><A
HREF="#DDRAW"
>Outline of DirectDraw Architecture</A
></DT
><DD
><DL
><DT
><A
HREF="#DDINHERITANCE"
>DirectDraw inheritance tree</A
></DT
><DT
><A
HREF="#DDSURFACEINHERITANCE"
>DirectDrawSurface inheritance tree</A
></DT
><DT
><A
HREF="#INTERFACETHUNKS"
>Interface Thunks</A
></DT
><DT
><A
HREF="#LOGICALOBJECTLAYOUT"
>Logical Object Layout</A
></DT
><DT
><A
HREF="#CREATINGOBJECT"
>Creating Objects</A
></DT
></DL
></DD
><DT
><A
HREF="#MULTIMEDIA"
>Wine and Multimedia</A
></DT
><DD
><DL
><DT
><A
HREF="#MM-OVERVIEW"
>Overview</A
></DT
><DT
><A
HREF="#MM-ARCH"
>Multimedia architecture</A
></DT
><DT
><A
HREF="#MM-LOW"
>Low level layers</A
></DT
><DT
><A
HREF="#MM-MCI"
>Mid level drivers (MCI)</A
></DT
><DT
><A
HREF="#MM-HIGH"
>High level layers</A
></DT
><DT
><A
HREF="#MSACM"
>MS ACM Dlls</A
></DT
><DT
><A
HREF="#MSVFW32"
>MS Video Dlls</A
></DT
><DT
><A
HREF="#MM-CONF"
>Multimedia configuration</A
></DT
></DL
></DD
></DL
></DD
></DL
></DIV
><DIV
CLASS="PART"
><A
NAME="PART-ONE"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="TITLE"
>I. Developing Wine</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="#DEBUGGER"
>Debugging Wine</A
></DT
><DT
><A
HREF="#DEBUGGING"
>Debug Logging</A
></DT
><DT
><A
HREF="#OTHERDEBUG"
>Other debugging techniques</A
></DT
><DT
><A
HREF="#CODINGPRACTICE"
>Coding Practice</A
></DT
><DT
><A
HREF="#TESTING"
>Writing Conformance tests</A
></DT
><DT
><A
HREF="#DOCUMENTATION"
>Documenting Wine</A
></DT
></DL
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="DEBUGGER"
></A
>Debugging Wine</H1
><DIV
CLASS="SECT1"
><H2
CLASS="SECT1"
><A
NAME="DBG-INTRO"
>Introduction</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN10"
>Processes and threads: in underlying OS and in Windows</A
></H3
><P
>          Before going into the depths of debugging in Wine, here's
          a small overview of process and thread handling in Wine.
          It has to be clear that there are two different beasts:
          processes/threads from the Unix point of view and
          processes/threads from a Windows point of view.
        </P
><P
>          Each Windows' thread is implemented as a Unix thread, meaning
          that all threads of a same Windows' process share the same
          (unix) address space.
        </P
><P
>          In the following:
        </P
><P
></P
><UL
><LI
><P
>              <CODE
CLASS="VARNAME"
>W-process</CODE
> means a process in Windows' terminology
            </P
></LI
><LI
><P
>              <CODE
CLASS="VARNAME"
>U-process</CODE
> means a process in Unix' terminology
            </P
></LI
><LI
><P
>              <CODE
CLASS="VARNAME"
>W-thread</CODE
> means a thread in Windows' terminology
            </P
></LI
></UL
><P
>          A <CODE
CLASS="VARNAME"
>W-process</CODE
> is made of one or several
          <CODE
CLASS="VARNAME"
>W-threads</CODE
>.  Each
          <CODE
CLASS="VARNAME"
>W-thread</CODE
> is mapped to one and only one
          <CODE
CLASS="VARNAME"
>U-process</CODE
>.  All
          <CODE
CLASS="VARNAME"
>U-processes</CODE
> of a same
          <CODE
CLASS="VARNAME"
>W-process</CODE
> share the same address space.
        </P
><P
>          Each Unix process can be identified by two values:
        </P
><P
></P
><UL
><LI
><P
>              the Unix process id (<CODE
CLASS="VARNAME"
>upid</CODE
> in the following)
            </P
></LI
><LI
><P
>              the Windows's thread id (<CODE
CLASS="VARNAME"
>tid</CODE
>)
            </P
></LI
></UL
><P
>          Each Windows' process has also a Windows' process id
          (<CODE
CLASS="VARNAME"
>wpid</CODE
> in the following).  It must be clear
          that <CODE
CLASS="VARNAME"
>upid</CODE
> and <CODE
CLASS="VARNAME"
>wpid</CODE
> are
          different and shall not be used instead of the other.
        </P
><P
>          <CODE
CLASS="VARNAME"
>Wpid</CODE
> and <CODE
CLASS="VARNAME"
>tid</CODE
> are
          defined (Windows) system wide.  They must not be confused
          with process or thread handles which, as any handle, is an
          indirection to a system object (in this case process or
          thread).  A same process can have several different handles
          on the same kernel object.  The handles can be defined as
          local (the values is only valid in a process), or system
          wide (the same handle can be used by any
          <CODE
CLASS="VARNAME"
>W-process</CODE
>).
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN48"
>Wine, debugging and WineDbg</A
></H3
><P
>          When talking of debugging in Wine, there are at least two
          levels to think of:
        </P
><P
></P
><UL
><LI
><P
>              the Windows' debugging API.
            </P
></LI
><LI
><P
>              the Wine integrated debugger, dubbed <B
CLASS="COMMAND"
>winedbg</B
>.
            </P
></LI
></UL
><P
>          Wine implements most of the Windows' debugging API.  The
	  first part of the debugging APIs (in
	  <TT
CLASS="FILENAME"
>KERNEL32.DLL</TT
>) allows a W-process, called
	  the debugger, to control the execution of another W-process,
	  the debuggee.  To control means stopping/resuming execution,
	  enabling/disabling single stepping, setting breakpoints,
	  reading/writing debuggee memory...  Another part of the
	  debugging APIs resides in <TT
CLASS="FILENAME"
>DBGHELP.DLL</TT
>
	  (and its ancestor <TT
CLASS="FILENAME"
>IMAGEHLP.DLL</TT
>) and lets
	  a debugger look into symbols and types from any module (if
	  the module has been compiled with the proper options).
        </P
><P
>          <B
CLASS="COMMAND"
>winedbg</B
> is a Winelib application making
          use of these APIs (<TT
CLASS="FILENAME"
>KERNEL32.DLL</TT
>'s
	  debugging API and <TT
CLASS="FILENAME"
>DBGHELP.DLL</TT
>) to allow
	  debugging both any Wine or Winelib applications as well as
	  Wine itself (kernel and all DLLs).
        </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-MODES"
>WineDbg's modes of invocation</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN67"
>Starting a process</A
></H3
><P
>          Any application (either a Windows' native executable, or a
          Winelib application) can be run through
          <B
CLASS="COMMAND"
>winedbg</B
>.  Command line options and tricks
          are the same as for wine:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>winedbg telnet.exe
winedbg hl.exe -windowed
        </PRE
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN72"
>Attaching</A
></H3
><P
>          <B
CLASS="COMMAND"
>winedbg</B
> can also be launched without any
          command line argument: <B
CLASS="COMMAND"
>winedbg</B
> is started
          without any attached process.  You can get a list of running
          <CODE
CLASS="VARNAME"
>W-processes</CODE
> (and their
          <CODE
CLASS="VARNAME"
>wpid</CODE
>'s) using the <B
CLASS="COMMAND"
>info
            process</B
> command, and then, with the
          <B
CLASS="COMMAND"
>attach</B
> command, pick up the
          <CODE
CLASS="VARNAME"
>wpid</CODE
> of the <CODE
CLASS="VARNAME"
>W-process</CODE
>
          you want to debug.  This is a neat feature as it allows you
	  to debug an already started application.
	 </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="DBG-ON-EXCEPTION"
>On exceptions</A
></H3
><P
>          When something goes wrong, Windows tracks this as an
          exception.  Exceptions exist for segmentation violation,
          stack overflow, division by zero, etc.
        </P
><P
>          When an exception occurs, Wine checks if the
	  <CODE
CLASS="VARNAME"
>W-process</CODE
> is debugged.  If so, the
	  exception event is sent to the debugger, which takes care of
	  it: end of the story.  This mechanism is part of the standard
	  Windows' debugging API.
        </P
><P
>          If the <CODE
CLASS="VARNAME"
>W-process</CODE
> is not debugged, Wine
          tries to launch a debugger.  This debugger (normally
          <B
CLASS="COMMAND"
>winedbg</B
>, see III Configuration for more
          details), at startup, attaches to the
          <CODE
CLASS="VARNAME"
>W-process</CODE
> which generated the exception
          event.  In this case, you are able to look at the causes of
          the exception, and either fix the causes (and continue
          further the execution) or dig deeper to understand what went
          wrong.
        </P
><P
>          If <B
CLASS="COMMAND"
>winedbg</B
> is the standard debugger, the
          <B
CLASS="COMMAND"
>pass</B
> and <B
CLASS="COMMAND"
>cont</B
> commands
          are the two ways to let the process go further for the
          handling of the  exception event.
        </P
><P
>          To be more precise on the way Wine (and Windows) generates
          exception events, when a fault occurs (segmentation
          violation, stack overflow...), the event is first sent to
          the debugger (this is known as a first chance exception).
          The debugger can give two answers:
        </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>continue</DT
><DD
><P
>                the debugger had the ability to correct what's
                generated the exception, and is now able to continue
                process execution.
              </P
></DD
><DT
>pass</DT
><DD
><P
>                the debugger couldn't correct the cause of the
                first chance exception.  Wine will now try to walk
                the list of exception handlers to see if one of them
                can handle the exception.  If no exception handler is
                found, the exception is sent once again to the
                debugger to indicate the failure of the exception
                handling.
              </P
></DD
></DL
></DIV
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>            since some of Wine's code uses exceptions and
            <TT
CLASS="FUNCTION"
>try/catch</TT
> blocks to provide some
            functionality, <B
CLASS="COMMAND"
>winedbg</B
> can be entered
            in such cases with segv exceptions.  This happens, for
            example, with <TT
CLASS="FUNCTION"
>IsBadReadPtr</TT
> function.
            In that case, the <B
CLASS="COMMAND"
>pass</B
> command shall be
            used, to let the handling of the exception to be done by
            the <TT
CLASS="FUNCTION"
>catch</TT
> block in
            <TT
CLASS="FUNCTION"
>IsBadReadPtr</TT
>.
          </P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="INTERRUPT"
>Interrupting</A
></H3
><P
>	  You can stop the debugger while it's running by hitting
	  Ctrl-C in its window.  This will stop the debugged process,
	  and let you manipulate the current context.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN117"
>Quitting</A
></H3
><P
>	  Wine supports the new XP APIs, allowing for a debugger to
	  detach from a program being debugged (see
	  <B
CLASS="COMMAND"
>detach</B
> command).
        </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="WINE-DEBUGGER"
>Using the Wine Debugger</A
></H2
><P
>        This section describes where to start debugging Wine.  If at any
        point you get stuck and want to ask for help, please read the
        <I
CLASS="EMPHASIS"
>How to Report A Bug</I
> section of the 
        <I
CLASS="EMPHASIS"
>Wine Users Guide</I
> for information on how to write
        useful bug reports.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN126"
>Crashes</A
></H3
><P
>          These usually show up like this:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>|Unexpected Windows program segfault - opcode = 8b
|Segmentation fault in Windows program 1b7:c41.
|Loading symbols from ELF file /root/wine/wine...
|....more Loading symbols from ...
|In 16 bit mode.
|Register dump:
| CS:01b7 SS:016f DS:0287 ES:0000
| IP:0c41 SP:878a BP:8796 FLAGS:0246
| AX:811e BX:0000 CX:0000 DX:0000 SI:0001 DI:ffff
|Stack dump:
|0x016f:0x878a:  0001 016f ffed 0000 0000 0287 890b 1e5b
|0x016f:0x879a:  01b7 0001 000d 1050 08b7 016f 0001 000d
|0x016f:0x87aa:  000a 0003 0004 0000 0007 0007 0190 0000
|0x016f:0x87ba:
|
|0050: sel=0287 base=40211d30 limit=0b93f (bytes) 16-bit rw-
|Backtrace:
|0 0x01b7:0x0c41 (PXSRV_FONGETFACENAME+0x7c)
|1 0x01b7:0x1e5b (PXSRV_FONPUTCATFONT+0x2cd)
|2 0x01a7:0x05aa
|3 0x01b7:0x0768 (PXSRV_FONINITFONTS+0x81)
|4 0x014f:0x03ed (PDOXWIN_@SQLCURCB$Q6CBTYPEULN8CBSCTYPE+0x1b1)
|5 0x013f:0x00ac
|
|0x01b7:0x0c41 (PXSRV_FONGETFACENAME+0x7c):  movw        %es:0x38(%bx),%dx
        </PRE
></TD
></TR
></TABLE
><P
>          Steps to debug a crash.  You may stop at any step, but please
          report the bug and provide as much of the information
          gathered to the bug report as feasible.
        </P
><P
></P
><OL
TYPE="1"
><LI
><P
>              Get the reason for the crash.  This is usually an access to
              an invalid selector, an access to an out of range address
              in a valid selector, popping a segment register from the
              stack or the like.  When reporting a crash, report this
              <I
CLASS="EMPHASIS"
>whole</I
> crashdump even if it doesn't
              make sense to you.
            </P
><P
>              (In this case it is access to an invalid selector, for
              <SPAN
CLASS="SYSTEMITEM"
>%es</SPAN
> is <TT
CLASS="LITERAL"
>0000</TT
>, as
              seen in the register dump).
            </P
></LI
><LI
><P
>              Determine the cause of the crash.  Since this is usually
              a primary/secondary reaction to a failed or misbehaving
              Wine function, rerun Wine with the <TT
CLASS="PARAMETER"
><I
>WINEDEBUG=+relay</I
></TT
>
              environment variable set.  This will
              generate quite a lot of output, but usually the reason is
              located in the last call(s).  Those lines usually look like
              this:
            </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>|Call KERNEL.90: LSTRLEN(0227:0692 "text") ret=01e7:2ce7 ds=0227
      ^^^^^^^^^  ^       ^^^^^^^^^ ^^^^^^      ^^^^^^^^^    ^^^^
      |          |       |         |           |            |Datasegment
      |          |       |         |           |Return address
      |          |       |         |textual parameter
      |          |       |
      |          |       |Argument(s).  This one is a win16 segmented pointer.
      |          |Function called.
      |The module, the function is called in.  In this case it is KERNEL.

|Ret  KERNEL.90: LSTRLEN() retval=0x0004 ret=01e7:2ce7 ds=0227
                                  ^^^^^^
				  |Returnvalue is 16 bit and has the value 4.
            </PRE
></TD
></TR
></TABLE
></LI
><LI
><P
>              If you have found a misbehaving Wine function, try to find out
              why it misbehaves.  Find the function in the source code.
              Try to make sense of the arguments passed.  Usually there is
              a <TT
CLASS="FUNCTION"
>WINE_DEFAULT_DEBUG_CHANNEL(&#60;channel&#62;);</TT
>
              at the beginning of the source file.  Rerun wine with the
              <TT
CLASS="PARAMETER"
><I
>WINEDEBUG=+xyz,+relay</I
></TT
> environment variable set.
            </P
><P
>              Occasionally there are additional debug channels defined at the 
              beginning of the source file in the form
              <TT
CLASS="FUNCTION"
>WINE_DECLARE_DEBUG_CHANNEL(&#60;channel&#62;);</TT
>
              If so the offending function may also uses one of these alternate
              channels.  Look through the the function for  
             <TT
CLASS="FUNCTION"
>TRACE_(&#60;channel&#62;)(" ... /n");</TT
> and add any
             additional channels to the commandline.
            </P
></LI
><LI
><P
>              Additional information on how to debug using the internal
              debugger can be  found in
              <TT
CLASS="FILENAME"
>programs/winedbg/README</TT
>.
            </P
></LI
><LI
><P
>              If this information isn't clear enough or if you want to
              know more about what's happening in the function itself,
              try running wine with <TT
CLASS="PARAMETER"
><I
>WINEDEBUG=+all</I
></TT
>,
              which dumps ALL included debug information in wine.
              It is often necessary to limit the debug output produced.
              That can be done by piping the output through <B
CLASS="COMMAND"
>grep</B
>,
              or alternatively with registry keys.  See
              <A
HREF="#CFG-DBG-RELAY"
>the Section called <I
>Configuring +relay behaviour</I
></A
> for more information.
            </P
></LI
><LI
><P
>              If even that isn't enough, add more debug output for yourself 
              into the functions you find relevant.  See The section on Debug 
              Logging in this guide for more information.  You might
              also try to run the program in <B
CLASS="COMMAND"
>gdb</B
>
              instead of using the Wine debugger.  If you do that, use
              <TT
CLASS="PARAMETER"
><I
>handle SIGSEGV nostop noprint</I
></TT
> to
              disable the handling of seg faults inside
              <B
CLASS="COMMAND"
>gdb</B
> (needed for Win16).
            </P
></LI
><LI
><P
>              You can also set a breakpoint for that function.  Start wine 
              using <B
CLASS="COMMAND"
>winedbg</B
> instead of 
              <B
CLASS="COMMAND"
>wine</B
>.  Once the debugger is running enter 
              <B
CLASS="COMMAND"
>break</B
> <TT
CLASS="PARAMETER"
><I
>KERNEL_LSTRLEN</I
></TT
>
              (replace by function you want to debug, CASE IS RELEVANT) 
              to set a breakpoint.  Then
              use <B
CLASS="COMMAND"
>continue</B
> to start normal
              program-execution.  Wine will stop if it reaches the
              breakpoint.  If the program isn't yet at the crashing call
              of that function, use <B
CLASS="COMMAND"
>continue</B
> again
              until you are about to enter that function.  You may now
              proceed with single-stepping the function until you reach
              the point of crash.  Use the other debugger commands to
              print registers and the like.
            </P
></LI
></OL
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN170"
>Program hangs, nothing happens</A
></H3
><P
>          Start the program with <B
CLASS="COMMAND"
>winedbg</B
> instead of
          <B
CLASS="COMMAND"
>wine</B
>.  When the program locks up switch to the 
          winedbg's terminal and press 
          <B
CLASS="KEYCAP"
>Ctrl</B
>-<B
CLASS="KEYCAP"
>C</B
>.  This
          will stop the program and let you debug the program as you would for
          a crash.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN178"
>Program reports an error with a Messagebox</A
></H3
><P
>          Sometimes programs are reporting failure using more or
          less nondescript messageboxes.  We can debug this using the
          same method as Crashes, but there is one problem...  For
          setting up a message box the program also calls Wine
          producing huge chunks of debug code.
        </P
><P
>          Since the failure happens usually directly before setting up
          the Messagebox you can start winedbg and set a
          breakpoint at <TT
CLASS="FUNCTION"
>MessageBoxA</TT
> (called by win16
          and win32 programs) and proceed with
          <B
CLASS="COMMAND"
>continue</B
>.  With <TT
CLASS="PARAMETER"
><I
>WINEDEBUG=+all</I
></TT
>
          Wine will now stop directly before setting
          up the Messagebox.  Proceed as explained above.
        </P
><P
>          You can also run wine using <B
CLASS="COMMAND"
>WINEDEBUG=+relay wine
            program.exe 2&#62;&#38;1 | less -i</B
> and in
          <B
CLASS="COMMAND"
>less</B
> search for <SPAN
CLASS="QUOTE"
>"MessageBox"</SPAN
>.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN189"
>Disassembling programs</A
></H3
><P
>          You may also try to disassemble the offending program to
          check for undocumented features and/or use of them.
        </P
><P
>          The best, freely available, disassembler for Win16 programs is
          <TT
CLASS="APPLICATION"
>Windows Codeback</TT
>, archive name
          <TT
CLASS="FILENAME"
>wcbxxx.zip</TT
> (e.g. <TT
CLASS="FILENAME"
>wcb105a.zip</TT
>).
        </P
><P
>          Disassembling win32 programs is possible using
          eg. <TT
CLASS="APPLICATION"
>GoVest</TT
> by Ansgar Trimborn.  It can be found
          <A
HREF="http://www.geocities.com/GoVest/"
TARGET="_top"
>here</A
>.
        </P
><P
>          You can also use the newer and better
          <TT
CLASS="APPLICATION"
>Interactive Disassembler</TT
> (IDA) from DataRescue.
          Take a look in the
          <A
HREF="http://appdb.winehq.org/appview.php?appId=565"
TARGET="_top"
>AppDB</A
>
          for links to various versions of IDA.
        </P
><P
>          Another popular disassembler is
          <TT
CLASS="APPLICATION"
>Windows Disassembler 32</TT
> from URSoft.
          Look for a file called <TT
CLASS="FILENAME"
>w32dsm87.zip</TT
> (or similar) on
          <A
HREF="http://www.winsite.com/"
TARGET="_top"
>winsite.com</A
> or
          <A
HREF="http://www.softpedia.com/"
TARGET="_top"
>softpedia.com</A
>.
          It seems that Windows Disassembler 32 currently has problems
          working correctly under Wine, so use IDA or GoVest.
        </P
><P
>          Also of considerable fame amongst disassemblers is
          <TT
CLASS="APPLICATION"
>SoftIce</TT
> from NuMega.  That software has since been
          acquired by CompuWare (http://www.compuware.com/) and made part of
          their Windows driver development suite.  Newer versions of SoftIce
          needs to run as a Windows Service and therefore won't currently
          work under Wine.
        </P
><P
>          If nothing works for you, you might try one of the disassemblers found in
          <A
HREF="http://directory.google.com/Top/Computers/Programming/Disassemblers/DOS_and_Windows/"
TARGET="_top"
>Google's directory</A
>.
        </P
><P
>          Understanding disassembled code is mostly a question of exercise.
          Most code out there uses standard C function entries (for it
          is usually  written in C).  Win16 function entries usually
          look like that:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>push bp
mov bp, sp
... function code ..
retf XXXX 	&#60;--------- XXXX is number of bytes of arguments
        </PRE
></TD
></TR
></TABLE
><P
>          This is a <TT
CLASS="FUNCTION"
>FAR</TT
> function with no local
          storage.  The arguments usually start at
          <TT
CLASS="LITERAL"
>[bp+6]</TT
> with increasing offsets.  Note, that
          <TT
CLASS="LITERAL"
>[bp+6]</TT
> belongs to the
          <I
CLASS="EMPHASIS"
>rightmost</I
> argument, for exported win16
          functions use the PASCAL calling convention.  So, if we use
          <TT
CLASS="FUNCTION"
>strcmp(a,b)</TT
> with <TT
CLASS="PARAMETER"
><I
>a</I
></TT
>
          and <TT
CLASS="PARAMETER"
><I
>b</I
></TT
> both 32 bit variables
          <TT
CLASS="PARAMETER"
><I
>b</I
></TT
> would be at <TT
CLASS="LITERAL"
>[bp+6]</TT
>
          and <TT
CLASS="PARAMETER"
><I
>a</I
></TT
> at <TT
CLASS="LITERAL"
>[bp+10]</TT
>.
        </P
><P
>          Most functions make also use of local storage in the stackframe:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>enter 0086, 00
... function code ...
leave
retf XXXX
        </PRE
></TD
></TR
></TABLE
><P
>          This does mostly the same as above, but also adds
          <TT
CLASS="LITERAL"
>0x86</TT
> bytes of stackstorage, which is
          accessed using <TT
CLASS="LITERAL"
>[bp-xx]</TT
>.  Before calling a
          function, arguments are pushed on the stack using something
          like this:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>push word ptr [bp-02]	&#60;- will be at [bp+8]
push di			&#60;- will be at [bp+6]
call KERNEL.LSTRLEN
        </PRE
></TD
></TR
></TABLE
><P
>          Here first the selector and then the offset to the passed
          string are pushed.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN232"
>Sample debugging session</A
></H3
><P
>          Let's debug the infamous Word <TT
CLASS="FILENAME"
>SHARE.EXE</TT
>
          messagebox:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>|marcus@jet $ wine winword.exe
|            +---------------------------------------------+
|            | !  You must leave Windows and load SHARE.EXE|
|            |    before starting Word.                    |
|            +---------------------------------------------+
        </PRE
></TD
></TR
></TABLE
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>|marcus@jet $ WINEDEBUG=+relay,-debug wine winword.exe
|CallTo32(wndproc=0x40065bc0,hwnd=000001ac,msg=00000081,wp=00000000,lp=00000000)
|Win16 task 'winword': Breakpoint 1 at 0x01d7:0x001a
|CallTo16(func=0127:0070,ds=0927)
|Call WPROCS.24: TASK_RESCHEDULE() ret=00b7:1456 ds=0927
|Ret  WPROCS.24: TASK_RESCHEDULE() retval=0x8672 ret=00b7:1456 ds=0927
|CallTo16(func=01d7:001a,ds=0927)
|     AX=0000 BX=3cb4 CX=1f40 DX=0000 SI=0000 DI=0927 BP=0000 ES=11f7
|Loading symbols: /home/marcus/wine/wine...
|Stopped on breakpoint 1 at 0x01d7:0x001a
|In 16 bit mode.
|Wine-dbg&#62;break MessageBoxA                          &#60;---- Set Breakpoint
|Breakpoint 2 at 0x40189100 (MessageBoxA [msgbox.c:190])
|Wine-dbg&#62;c                                            &#60;---- Continue
|Call KERNEL.91: INITTASK() ret=0157:0022 ds=08a7
|     AX=0000 BX=3cb4 CX=1f40 DX=0000 SI=0000 DI=08a7 ES=11d7 EFL=00000286
|CallTo16(func=090f:085c,ds=0dcf,0x0000,0x0000,0x0000,0x0000,0x0800,0x0000,0x0000,0x0dcf)
|...                                                   &#60;----- Much debugoutput
|Call KERNEL.136: GETDRIVETYPE(0x0000) ret=060f:097b ds=0927
                               ^^^^^^ Drive 0 (A:)
|Ret  KERNEL.136: GETDRIVETYPE() retval=0x0002 ret=060f:097b ds=0927
                                        ^^^^^^  DRIVE_REMOVEABLE
						(It is a floppy diskdrive.)

|Call KERNEL.136: GETDRIVETYPE(0x0001) ret=060f:097b ds=0927
                               ^^^^^^ Drive 1 (B:)
|Ret  KERNEL.136: GETDRIVETYPE() retval=0x0000 ret=060f:097b ds=0927
                                        ^^^^^^  DRIVE_CANNOTDETERMINE
						(I don't have drive B: assigned)

|Call KERNEL.136: GETDRIVETYPE(0x0002) ret=060f:097b ds=0927
                               ^^^^^^^ Drive 2 (C:)
|Ret  KERNEL.136: GETDRIVETYPE() retval=0x0003 ret=060f:097b ds=0927
                                        ^^^^^^ DRIVE_FIXED
                                               (specified as a harddisk)

|Call KERNEL.97: GETTEMPFILENAME(0x00c3,0x09278364"doc",0x0000,0927:8248) ret=060f:09b1 ds=0927
                                 ^^^^^^           ^^^^^        ^^^^^^^^^
                                 |                |            |buffer for fname
                                 |                |temporary name ~docXXXX.tmp
                                 |Force use of Drive C:.

|Warning: GetTempFileName returns 'C:~doc9281.tmp', which doesn't seem to be writable.
|Please check your configuration file if this generates a failure.
        </PRE
></TD
></TR
></TABLE
><P
>          Whoops, it even detects that something is wrong!
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>|Ret  KERNEL.97: GETTEMPFILENAME() retval=0x9281 ret=060f:09b1 ds=0927
                                          ^^^^^^ Temporary storage ID

|Call KERNEL.74: OPENFILE(0x09278248"C:~doc9281.tmp",0927:82da,0x1012) ret=060f:09d8 ds=0927
                                    ^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^
                                    |filename        |OFSTRUCT |open mode:

                                       OF_CREATE|OF_SHARE_EXCLUSIVE|OF_READWRITE
        </PRE
></TD
></TR
></TABLE
><P
>          This fails, since my <I
CLASS="MEDIALABEL"
>C:</I
> drive is in
          this case mounted readonly.
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>|Ret  KERNEL.74: OPENFILE() retval=0xffff ret=060f:09d8 ds=0927
                                   ^^^^^^ HFILE_ERROR16, yes, it failed.

|Call USER.1: MESSAGEBOX(0x0000,0x09278376"You must close Windows and load SHARE.EXE before you start Word.",0x00000000,0x1030) ret=060f:084f ds=0927
        </PRE
></TD
></TR
></TABLE
><P
>          And MessageBox'ed.
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>|Stopped on breakpoint 2 at 0x40189100 (MessageBoxA [msgbox.c:190])
|190     {		&#60;- the sourceline
In 32 bit mode.
Wine-dbg&#62;
        </PRE
></TD
></TR
></TABLE
><P
>          The code seems to find a writable harddisk and tries to create
          a file there.   To work around this bug, you can define
          <I
CLASS="MEDIALABEL"
>C:</I
> as a networkdrive, which is ignored
          by the code above.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN247"
>Debugging Tips</A
></H3
><P
>          Here are some additional debugging tips:
        </P
><P
></P
><UL
><LI
><P
>              If you have a program crashing at such an early loader phase that you can't
              use the Wine debugger normally, but Wine already executes the program's
              start code, then you may use a special trick.  You should do a
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>WINEDEBUG=+relay wine program
              </PRE
></TD
></TR
></TABLE
>
              to get a listing of the functions the program calls in its start function.
              Now you do a
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>winedbg winfile.exe
              </PRE
></TD
></TR
></TABLE
>
            </P
><P
>              This way, you get into <B
CLASS="COMMAND"
>winedbg</B
>.  Now you
              can set a breakpoint on any function the program calls in
              the start function and just type <KBD
CLASS="USERINPUT"
>c</KBD
>
              to bypass the eventual calls of Winfile to this function
              until you are finally at the place where this function gets
              called by the crashing start function.  Now you can proceed
              with your debugging as usual.
            </P
></LI
><LI
><P
>              If you try to run a program and it quits after showing an error messagebox,
              the problem can usually be identified in the return value of one of the
              functions executed before <TT
CLASS="FUNCTION"
>MessageBox()</TT
>.
              That's why you should re-run the program with e.g.
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>WINEDEBUG=+relay wine  &#60;program name&#62; &#38;&#62;relmsg
              </PRE
></TD
></TR
></TABLE
>
              Then do a <B
CLASS="COMMAND"
>more relmsg</B
> and search for the
              last occurrence of a call to the string "MESSAGEBOX".  This is a line like
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>Call USER.1: MESSAGEBOX(0x0000,0x01ff1246 "Runtime error 219 at 0004:1056.",0x00000000,0x1010) ret=01f7:2160 ds=01ff
              </PRE
></TD
></TR
></TABLE
>
              In my example the lines before the call to
              <TT
CLASS="FUNCTION"
>MessageBox()</TT
> look like that:
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>Call KERNEL.96: FREELIBRARY(0x0347) ret=01cf:1033 ds=01ff
CallTo16(func=033f:0072,ds=01ff,0x0000)
Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:1033 ds=01ff
Call KERNEL.96: FREELIBRARY(0x036f) ret=01cf:1043 ds=01ff
CallTo16(func=0367:0072,ds=01ff,0x0000)
Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:1043 ds=01ff
Call KERNEL.96: FREELIBRARY(0x031f) ret=01cf:105c ds=01ff
CallTo16(func=0317:0072,ds=01ff,0x0000)
Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:105c ds=01ff
Call USER.171: WINHELP(0x02ac,0x01ff05b4 "COMET.HLP",0x0002,0x00000000) ret=01cf:1070 ds=01ff
CallTo16(func=0117:0080,ds=01ff)
Call WPROCS.24: TASK_RESCHEDULE() ret=00a7:0a2d ds=002b
Ret  WPROCS.24: TASK_RESCHEDULE() retval=0x0000 ret=00a7:0a2d ds=002b
Ret  USER.171: WINHELP() retval=0x0001 ret=01cf:1070 ds=01ff
Call KERNEL.96: FREELIBRARY(0x01be) ret=01df:3e29 ds=01ff
Ret  KERNEL.96: FREELIBRARY() retval=0x0000 ret=01df:3e29 ds=01ff
Call KERNEL.52: FREEPROCINSTANCE(0x02cf00ba) ret=01f7:1460 ds=01ff
Ret  KERNEL.52: FREEPROCINSTANCE() retval=0x0001 ret=01f7:1460 ds=01ff
Call USER.1: MESSAGEBOX(0x0000,0x01ff1246 "Runtime error 219 at 0004:1056.",0x00000000,0x1010) ret=01f7:2160 ds=01ff
              </PRE
></TD
></TR
></TABLE
>
            </P
><P
>              I think that the call to <TT
CLASS="FUNCTION"
>MessageBox()</TT
>
              in this example is <I
CLASS="EMPHASIS"
>not</I
> caused by a
              wrong result value of some previously executed function
              (it's happening quite often like that), but instead the
              messagebox complains about a runtime error at
              <TT
CLASS="LITERAL"
>0x0004:0x1056</TT
>.
            </P
><P
>              As the segment value of the address is only
              <TT
CLASS="LITERAL"
>4</TT
>, I think that that is only an internal
              program value.  But the offset address reveals something
              quite interesting: Offset <TT
CLASS="LITERAL"
>1056</TT
> is
              <I
CLASS="EMPHASIS"
>very</I
> close to the return address of
              <TT
CLASS="FUNCTION"
>FREELIBRARY()</TT
>:
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>Call KERNEL.96: FREELIBRARY(0x031f) ret=01cf:105c ds=01ff
                                             ^^^^
              </PRE
></TD
></TR
></TABLE
>
            </P
><P
>              Provided that segment <TT
CLASS="LITERAL"
>0x0004</TT
> is indeed segment
              <TT
CLASS="LITERAL"
>0x1cf</TT
>, we now we can use IDA to disassemble
              the part that caused the error.  We just have to find the
              address of the call to <TT
CLASS="FUNCTION"
>FreeLibrary()</TT
>.  Some
              lines before that the runtime error occurred.  But be careful!
              In some cases you don't have to disassemble the main program,
              but instead some DLL called by it in order to find the correct
              place where the runtime error occurred.  That can be determined
              by finding the origin of the segment value (in this case
              <TT
CLASS="LITERAL"
>0x1cf</TT
>).
            </P
></LI
><LI
><P
>              If you have created a relay file of some crashing
              program and want to set a breakpoint at a certain
              location which is not yet available as the program loads
              the breakpoint's segment during execution, you may set a
              breakpoint to <TT
CLASS="FUNCTION"
>GetVersion16/32</TT
> as
              those functions are called very often.
            </P
><P
>              Then do a <KBD
CLASS="USERINPUT"
>c</KBD
> until you are able to
              set this breakpoint without error message.
            </P
></LI
></UL
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN286"
>Some basic debugger usages</A
></H3
><P
>          After starting your program with
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>winedbg myprog.exe
        </PRE
></TD
></TR
></TABLE
><P
>          the program loads and you get a prompt at the program
          starting point.  Then you can set breakpoints:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>  b RoutineName      (by routine name) OR
  b *0x812575        (by address)
        </PRE
></TD
></TR
></TABLE
><P
>          Then you hit <B
CLASS="COMMAND"
>c</B
> (continue) to run the
          program.  It stops at the breakpoint.  You can type
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>  step               (to step one line) OR
  stepi              (to step one machine instruction at a time;
                      here, it helps to know the basic 386
                      instruction set)
  info reg           (to see registers)
  info stack         (to see hex values in the stack)
  info local         (to see local variables)
  list &#60;line number&#62; (to list source code)
  x &#60;variable name&#62;  (to examine a variable; only works if code
                      is not compiled with optimization)
  x 0x4269978        (to examine a memory location)
  ?                  (help)
  q                  (quit)
        </PRE
></TD
></TR
></TABLE
><P
>          By hitting <B
CLASS="KEYCAP"
>Enter</B
>, you repeat the last
          command.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="USEFUL-PROGRAMS"
>Useful programs</A
></H3
><P
>          Some useful programs:
        </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="APPLICATION"
>GoVest</TT
>:
              <TT
CLASS="FILENAME"
>govest.zip</TT
> is available from
              <A
HREF="http://www.geocities.com/GoVest/"
TARGET="_top"
>http://www.geocities.com/GoVest/</A
>.</DT
><DD
><P
>                Simple win32 disassembler that works well with Wine.
              </P
></DD
><DT
><TT
CLASS="APPLICATION"
>IDA</TT
>:</DT
><DD
><P
>                IDA Pro is highly recommended, but is not free.
                DataRescue does however make trial versions available.
              </P
><P
>                Take a look in the
                <A
HREF="http://appdb.winehq.org/appview.php?appId=565"
TARGET="_top"
>AppDB</A
>
                for links to various versions of IDA.
              </P
></DD
><DT
><TT
CLASS="APPLICATION"
>XRAY</TT
>:
              <TT
CLASS="FILENAME"
>                <A
HREF="http://garbo.uwasa.fi/pub/pc/sysinfo/xray15.zip"
TARGET="_top"
>                 http://garbo.uwasa.fi/pub/pc/sysinfo/xray15.zip</A
>
              </TT
></DT
><DD
><P
>                Traces DOS calls (Int 21h, DPMI, ...).  Use it with
                Windows to correct file management problems etc.
              </P
></DD
><DT
><TT
CLASS="APPLICATION"
>pedump</TT
>:
              <TT
CLASS="FILENAME"
>                <A
HREF="ftp://ftp.simtel.net/pub/simtelnet/win95/prog/pedump.zip"
TARGET="_top"
>                 ftp://ftp.simtel.net/pub/simtelnet/win95/prog/pedump.zip</A
>
              </TT
></DT
><DD
><P
>                Dumps the imports and exports of a PE (Portable
                Executable) DLL.
              </P
></DD
><DT
><TT
CLASS="APPLICATION"
>winedump</TT
>: (included in wine tree)</DT
><DD
><P
>                Dumps the imports and exports of a PE (Portable
                Executable) DLL.
              </P
></DD
></DL
></DIV
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MEMORY-ADDRESSES"
>Useful memory addresses</A
></H2
><P
>        Wine uses several different kinds of memory addresses.
      </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>Win32/"normal" Wine addresses/Linux: linear addresses.</DT
><DD
><P
>              Linear addresses can be everything from 0x0 up to
              0xffffffff.  In Wine on Linux they are often around
              e.g. 0x08000000, 0x00400000 (std. Win32 program load
              address), 0x40000000.  Every Win32 process has its own
              private 4GB address space (that is, from 0x0 up to
              0xffffffff).
            </P
></DD
><DT
>Win16 "enhanced mode": segmented addresses.</DT
><DD
><P
>              These are the "normal" Win16 addresses, called SEGPTR.
              They have a segment:offset notation, e.g. 0x01d7:0x0012.
              The segment part usually is a "selector", which
	      <I
CLASS="EMPHASIS"
>always</I
>
              has the lowest 3 bits set.  Some sample selectors are
              0x1f7, 0x16f, 0x8f.  If these bits are set except for
              the lowest bit, as e.g. with 0x1f6,xi then it might be a
              handle to global memory.  Just set the lowest bit to get
              the selector in these cases.  A selector kind of
              "points" to a certain linear (see above) base address.
              It has more or less three important attributes: segment
              base address, segment limit, segment access rights.
            </P
><P
>              Example:
            </P
><P
>              Selector 0x1f7 (0x40320000, 0x0000ffff, r-x) So 0x1f7
              has a base address of 0x40320000, the segment's last
              address is 0x4032ffff (limit 0xffff), and it's readable
              and executable.  So an address of 0x1f7:0x2300 would be
              the linear address of 0x40322300.
            </P
></DD
><DT
>DOS/Win16 "standard mode"</DT
><DD
><P
>              They, too, have a segment:offset notation.  But they are
              completely different from "normal" Win16 addresses, as
              they just represent at most 1MB of memory: The segment
              part can be anything from 0 to 0xffff, and it's the same
              with the offset part.
            </P
><P
>              Now the strange thing is the calculation that's behind
              these addresses: Just calculate segment*16 + offset in
              order to get a "linear DOS" address.  So
              e.g. 0x0f04:0x3628 results in 0xf040 + 0x3628 = 0x12668.
              And the highest address you can get is 0xfffff (1MB), of
              course.  In Wine, this "linear DOS" address of 0x12668
              has to be added to the linear base address of the
              corresponding DOS memory allocated for dosmod in order
              to get the true linear address of a DOS seg:offs
              address.  And make sure that you're doing this in the
              correct process with the correct linear address space,
              of course ;-)
            </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-CONFIG"
>Configuration</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN356"
>Windows Debugging configuration</A
></H3
><P
>          The Windows' debugging API uses a registry entry to know
          which debugger to invoke when an unhandled exception occurs
          (see <A
HREF="#DBG-ON-EXCEPTION"
><I
>On exceptions</I
></A
> for some details).  Two
          values in key
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>[MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug]
        </PRE
></TD
></TR
></TABLE
><P
>          Determine the behavior:
        </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>Debugger</DT
><DD
><P
>                This is the command line used to launch the debugger
                (it uses two <TT
CLASS="FUNCTION"
>printf</TT
> formats
                (<TT
CLASS="LITERAL"
>%ld</TT
>) to pass context dependent
                information to the debugger).  You should put here a
                complete path to your debugger
                (<B
CLASS="COMMAND"
>winedbg</B
> can of course be used, but
                any other Windows' debugging API aware debugger will do).
                The path to the debugger you choose to use must be reachable
                via one of the DOS drives configured under /dosdevices in
                your $WINEPREFIX or ~/.wine folder.
              </P
></DD
><DT
>Auto</DT
><DD
><P
>                If this value is zero, a message box will ask the
                user if he/she wishes to launch the debugger when an
                unhandled exception occurs.  Otherwise, the debugger
                is automatically started.
              </P
></DD
></DL
></DIV
><P
>          A regular Wine registry looks like:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>[MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug] 957636538
"Auto"=dword:00000001
"Debugger"="winedbg %ld %ld"
        </PRE
></TD
></TR
></TABLE
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
><B
>Note 1</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>            Creating this key is mandatory.  Not doing so will not
            fire the debugger when an exception occurs.
          </P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
><B
>Note 2</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>            <B
CLASS="COMMAND"
>wineinstall</B
> (available in Wine source)
	    sets up this correctly.
            However, due to some limitation of the registry installed,
            if a previous Wine installation exists, it's safer to
            remove the whole
          </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>[MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug]
          </PRE
></TD
></TR
></TABLE
><P
>            key before running again <B
CLASS="COMMAND"
>wineinstall</B
> to
            regenerate this key.
          </P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN386"
>WineDbg configuration</A
></H3
><P
>          <B
CLASS="COMMAND"
>winedbg</B
> can be configured through a number
          of options.  Those options are stored in the registry, on a
          per user basis.  The key is (in <I
CLASS="EMPHASIS"
>my</I
> registry)
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>[HKCU\\Software\\Wine\\WineDbg]
        </PRE
></TD
></TR
></TABLE
><P
>          Those options can be read/written while inside
          <B
CLASS="COMMAND"
>winedbg</B
>, as part of the debugger
          expressions.  To refer to one of these options, its name must
          be  prefixed by a <TT
CLASS="LITERAL"
>$</TT
> sign.  For example,
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>set $BreakAllThreadsStartup = 1
        </PRE
></TD
></TR
></TABLE
><P
>          sets the option <CODE
CLASS="VARNAME"
>BreakAllThreadsStartup</CODE
> to
          <TT
CLASS="LITERAL"
>TRUE</TT
>.
        </P
><P
>          All the options are read from the registry when
          <B
CLASS="COMMAND"
>winedbg</B
> starts (if no corresponding value
          is found, a default value is used), and are written back to
          the registry when <B
CLASS="COMMAND"
>winedbg</B
> exits (hence,
          all modifications to those options are automatically saved
          when <B
CLASS="COMMAND"
>winedbg</B
> terminates).
        </P
><P
>          Here's the list of all options:
        </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><CODE
CLASS="VARNAME"
>BreakAllThreadsStartup</CODE
></DT
><DD
><P
>		Set to <TT
CLASS="LITERAL"
>TRUE</TT
> if at all threads
		start-up the debugger stops  set to
		<TT
CLASS="LITERAL"
>FALSE</TT
> if only at the first thread
		startup of a given process the debugger
		stops.  <TT
CLASS="LITERAL"
>FALSE</TT
> by default.
	      </P
></DD
><DT
><CODE
CLASS="VARNAME"
>BreakOnCritSectTimeOut</CODE
></DT
><DD
><P
>		Set to <TT
CLASS="LITERAL"
>TRUE</TT
> if the debugger stops
		when a critical section times out (5 minutes);
		<TT
CLASS="LITERAL"
>TRUE</TT
> by default.
	      </P
></DD
><DT
><CODE
CLASS="VARNAME"
>BreakOnAttach</CODE
></DT
><DD
><P
>		Set to <TT
CLASS="LITERAL"
>TRUE</TT
> if when
		<B
CLASS="COMMAND"
>winedbg</B
> attaches to an existing
		process after an unhandled exception,
		<B
CLASS="COMMAND"
>winedbg</B
> shall be entered on the
		first attach event.  Since the attach event is
		meaningless in the context of an exception event (the
		next event  which is the exception event is of course
		relevant), that option is likely to be
		<TT
CLASS="LITERAL"
>FALSE</TT
>.
	      </P
></DD
><DT
><CODE
CLASS="VARNAME"
>BreakOnFirstChance</CODE
></DT
><DD
><P
>		An exception can generate two debug events.  The first
		one is passed to the debugger (known as a first
		chance) just after the exception.  The debugger can
		then decide either to resume execution (see
		<B
CLASS="COMMAND"
>winedbg</B
>'s <B
CLASS="COMMAND"
>cont</B
>
		command) or pass the exception up to the exception
		handler chain in the program (if it exists)
		(<B
CLASS="COMMAND"
>winedbg</B
> implements this through
		the <B
CLASS="COMMAND"
>pass</B
> command).  If none of the
		exception handlers takes care of the exception, the
		exception event is sent again to the debugger (known
		as last chance exception).  You cannot pass on a last
		exception.  When the
		<CODE
CLASS="VARNAME"
>BreakOnFirstChance</CODE
> exception is
		<TT
CLASS="LITERAL"
>TRUE</TT
>, then winedbg is entered for
		both first and last chance execptions (to
		<TT
CLASS="LITERAL"
>FALSE</TT
>, it's only entered for last
		chance exceptions).
	      </P
></DD
><DT
><CODE
CLASS="VARNAME"
>AlwaysShowThunk</CODE
></DT
><DD
><P
>		Set to <TT
CLASS="LITERAL"
>TRUE</TT
> if the debugger, when
		looking up for a symbol from its name, displays all
		the thunks with that name.  The default value
		(<TT
CLASS="LITERAL"
>FALSE</TT
>) allows not to have to
		choose between a symbol and all the import thunks
		from all the DLLs using that symbols.
	      </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="CFG-DBG-RELAY"
>Configuring +relay behaviour</A
></H3
><P
>          When setting <TT
CLASS="PARAMETER"
><I
>WINEDEBUG</I
></TT
> to +relay and debugging,
          you might get a lot of output.  You can limit the output by
          configuring the value RelayExclude in the registry, located
          under the key:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>          [HKCU\\Software\\Wine\\Debug]
        </PRE
></TD
></TR
></TABLE
><P
>          Set the value of RelayExclude to a semicolon-separated list of
          calls to exclude.  Example:
          "RtlEnterCriticalSection;RtlLeaveCriticalSection;kernel32.97;kernel32.98".
        </P
><P
>          RelayInclude is an option similar to RelayExclude, except that
          functions listed here will be the only ones included in the output.
          Also see section 3.4.3.5 for more options.
        </P
><P
>          If your application runs too slow with +relay to get meaningful
          output and you're stuck with multi-GB relay log files, but
          you're not sure what to exclude, here's a trick to get you started.
          First, run your application for a minute or so, piping it's output
          to a file on disk:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>          WINEDEBUG=+relay wine &#60;appname.exe&#62; &#38;&#62;relay.log
        </PRE
></TD
></TR
></TABLE
><P
>          Then run this command to see which calls are performed the most:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>          awk -F'(' '{print $1}' &#60; relay.log | awk '{print $2}' | sort | uniq -c | sort
        </PRE
></TD
></TR
></TABLE
><P
>          Exclude the bottom-most calls with RelayExclude after making
          sure that they are irrelevant, then run your application again.
        </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-EXPR"
>WineDbg Expressions and Variables</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN462"
>Expressions</A
></H3
><P
>	  Expressions in Wine Debugger are mostly written in a C
	  form.  However, there are a few discrepancies:
	  <P
></P
><UL
><LI
><P
>		Identifiers can take a '!' in their names.  This allow
		mainly to access symbols from different DLLs like
		<TT
CLASS="FUNCTION"
>USER32!CreateWindowExA</TT
>.
	      </P
></LI
><LI
><P
>	        In cast operation, when specifying a structure or an
		union, you must use the <SPAN
CLASS="TYPE"
>struct</SPAN
> or
		<SPAN
CLASS="TYPE"
>union</SPAN
> keyword (even if your program uses a typedef).
	      </P
></LI
></UL
>
	</P
><P
>	  When specifying an identifier by its name, if several
	  symbols with the same name exist, the debugger will prompt
	  for the symbol you want to use.  Pick up the one you want
	  from its number.
	</P
><P
>	  In lots of cases, you can also use regular expressions for
	  looking for a symbol.
	</P
><P
>	  <B
CLASS="COMMAND"
>winedbg</B
> defines its own set of
	  variables.  The configuration variables from above are part
	  of them.  Some others include:
          <P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><CODE
CLASS="VARNAME"
>$ThreadId</CODE
></DT
><DD
><P
>                  ID of the <CODE
CLASS="VARNAME"
>W-thread</CODE
> currently
                  examined by the debugger
                </P
></DD
><DT
><CODE
CLASS="VARNAME"
>$ProcessId</CODE
></DT
><DD
><P
>                  ID of the <CODE
CLASS="VARNAME"
>W-thread</CODE
> currently
                  examined by the debugger
                </P
></DD
><DT
>&#60;registers&#62;</DT
><DD
><P
>                  All CPU registers are also available, using $ as a
		  prefix.  You can use <B
CLASS="COMMAND"
>info regs</B
> to
		  get a list of avaible CPU registers
                </P
></DD
></DL
></DIV
>
	</P
><P
>	  The <CODE
CLASS="VARNAME"
>$ThreadId</CODE
> and
	  <CODE
CLASS="VARNAME"
>$ProcessId</CODE
> variables can be handy to set
	  conditional breakpoints on a given thread or process.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-COMMANDS"
>WineDbg Command Reference</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN500"
>Misc</A
></H3
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN503"
></A
><P
><B
>Table 1. WineDbg's misc. commands</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
><B
CLASS="COMMAND"
>abort</B
></TD
><TD
>aborts the debugger</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>quit</B
></TD
><TD
>exits the debugger</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>attach N</B
></TD
><TD
>		    attach to a W-process (N is its ID, numeric
		    or hexadecimal (0xN)).  IDs can be obtained using
		    the info process command.  Note the info process
		    command returns hexadecimal values.
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>detach</B
></TD
><TD
>detach from a W-process.</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>help</B
></TD
><TD
>prints some help on the commands</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>help info</B
></TD
><TD
>prints some help on info commands</TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN531"
>Flow control</A
></H3
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN534"
></A
><P
><B
>Table 2. WineDbg's flow control commands</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
>		    <B
CLASS="COMMAND"
>cont</B
>, <B
CLASS="COMMAND"
>c</B
>
		  </TD
><TD
>continue execution until next breakpoint or
		    exception.</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>pass</B
></TD
><TD
>pass the exception event up to the filter
		    chain.</TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>step</B
>, <B
CLASS="COMMAND"
>s</B
>
		  </TD
><TD
>		    continue execution until next 'C' line of code
		    (enters function call)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>next</B
>, <B
CLASS="COMMAND"
>n</B
>
		  </TD
><TD
>		    continue execution until next 'C' line of code
		    (doesn't enter function call)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>stepi</B
>, <B
CLASS="COMMAND"
>si</B
>
		  </TD
><TD
>		    execute next assembly instruction (enters function
		    call)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>nexti</B
>, <B
CLASS="COMMAND"
>ni</B
>
		  </TD
><TD
>		    execute next assembly instruction (doesn't enter
		    function call)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>finish</B
>, <B
CLASS="COMMAND"
>f</B
>
		  </TD
><TD
>execute until current function is exited</TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
><P
>          <B
CLASS="COMMAND"
>cont</B
>, <B
CLASS="COMMAND"
>step</B
>,
	  <B
CLASS="COMMAND"
>next</B
>, <B
CLASS="COMMAND"
>stepi</B
>,
	  <B
CLASS="COMMAND"
>nexti</B
> can be postfixed by a number (N),
	  meaning that the command must be executed N times.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN602"
>Breakpoints, watch points</A
></H3
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN605"
></A
><P
><B
>Table 3. WineDbg's break &#38; watch points</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
><B
CLASS="COMMAND"
>enable N</B
></TD
><TD
>enables (break|watch)point #N</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>disable N</B
></TD
><TD
>disables (break|watch)point #N</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>delete N</B
></TD
><TD
>deletes (break|watch)point #N</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>cond N</B
></TD
><TD
>		    removes any existing condition to
		    (break|watch)point N
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>cond N &#60;expr&#62;</B
>
		  </TD
><TD
>		    adds condition &#60;expr&#62; to (break|watch)point
		    N.  &#60;expr&#62; will be evaluated each time the
		    breakpoint is hit.  If the result is a zero value,
		    the breakpoint isn't triggered
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>break * N</B
></TD
><TD
>adds a breakpoint at address N</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>break &#60;id&#62;</B
></TD
><TD
>		    adds a breakpoint at the address of symbol
		    &#60;id&#62;
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>break &#60;id&#62; N</B
></TD
><TD
>		    adds a breakpoint at the address of symbol
		    &#60;id&#62; (N ?)
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>break N</B
></TD
><TD
>		    adds a breakpoint at line N of current source file
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>break</B
></TD
><TD
>		    adds a breakpoint at current $PC address
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>watch * N</B
></TD
><TD
>		    adds a watch command (on write) at address N (on 4
		    bytes)
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>watch &#60;id&#62;</B
></TD
><TD
>		    adds a watch command (on write) at the address of
		    symbol &#60;id&#62;
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info break</B
></TD
><TD
>		    lists all (break|watch)points (with state)
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
><P
>          You can use the symbol <I
CLASS="EMPHASIS"
>EntryPoint</I
> to stand for
          the entry point of the Dll.
        </P
><P
>          When setting a break/watch-point by &#60;id&#62;, if the
	  symbol cannot be found (for example, the symbol is contained
	  in a not yet loaded module), winedbg will recall the name of
	  the symbol and will try to set the breakpoint each time a
	  new module is loaded (until it succeeds).
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN664"
>Stack manipulation</A
></H3
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN667"
></A
><P
><B
>Table 4. WineDbg's stack manipulation</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
><B
CLASS="COMMAND"
>bt</B
></TD
><TD
>print calling stack of current thread</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>bt N</B
></TD
><TD
>		    print calling stack of thread of ID N (note: this
		    doesn't change the position of the current frame
		    as manipulated by the <B
CLASS="COMMAND"
>up</B
> and
		    <B
CLASS="COMMAND"
>dn</B
> commands)
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>up</B
></TD
><TD
>		    goes up one frame in current thread's stack
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>up N</B
></TD
><TD
>		    goes up N frames in current thread's stack
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>dn</B
></TD
><TD
>		    goes down one frame in current thread's stack
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>dn N</B
></TD
><TD
>		    goes down N frames in current thread's stack
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>frame N</B
></TD
><TD
>		    set N as the current frame for current thread's
		    stack
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info local</B
></TD
><TD
>		    prints information on local variables for current
		    function frame
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN705"
>Directory &#38; source file manipulation</A
></H3
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN708"
></A
><P
><B
>Table 5. WineDbg's directory &#38; source file manipulation</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
><B
CLASS="COMMAND"
>show dir</B
></TD
><TD
>		    prints the list of dir:s where source files are
		    looked for
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>dir &#60;pathname&#62;</B
>
		  </TD
><TD
>		    adds &#60;pathname&#62; to the list of dir:s
		    where to look for source files
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>dir</B
></TD
><TD
>		    deletes the list of dir:s where to look for source
		    files
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      symbolfile &#60;pathname&#62;
		    </B
>
		  </TD
><TD
>loads external symbol definition</TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      symbolfile &#60;pathname&#62; N
		    </B
>
		  </TD
><TD
>		    loads external symbol definition (applying an
		    offset of N to addresses)
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>list</B
></TD
><TD
>		    lists 10 source lines forwards from current
		    position
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>list -</B
></TD
><TD
>		    lists 10 source lines backwards from current
		    position
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>list N</B
></TD
><TD
>		    lists 10 source lines from line N in current file
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>list &#60;path&#62;:N</B
>
		  </TD
><TD
>		    lists 10 source lines from line N in file
		    &#60;path&#62;
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>list &#60;id&#62;</B
></TD
><TD
>		    lists 10 source lines of function &#60;id&#62;
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>list * N</B
></TD
><TD
>lists 10 source lines from address N</TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
><P
>          You can specify the end target (to change the 10 lines
          value) using the ','.  For example:
	  <DIV
CLASS="TABLE"
><A
NAME="AEN757"
></A
><P
><B
>Table 6. WineDbg's list command examples</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
><B
CLASS="COMMAND"
>list 123, 234</B
></TD
><TD
>		    lists source lines from line 123 up to line 234 in
		    current file
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>list foo.c:1, 56</B
></TD
><TD
>		    lists source lines from line 1 up to 56 in file
		    foo.c
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN769"
>Displaying</A
></H3
><P
>          A display is an expression that's evaluated and printed
          after the execution of any <B
CLASS="COMMAND"
>winedbg</B
>
          command.
        </P
><P
>	  <B
CLASS="COMMAND"
>winedbg</B
> will automatically detect if the
	  expression you entered contains a local variable.  If so,
	  display will only be shown if the context is still in the
	  same function as the one the debugger was in when the
	  display expression was entered.
	</P
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN776"
></A
><P
><B
>Table 7. WineDbg's displays</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
>		    <B
CLASS="COMMAND"
>info display</B
>
		  </TD
><TD
>lists the active displays</TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>display</B
>
		  </TD
><TD
>		    print the active displays' values (as done each
		    time the debugger stops)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>display &#60;expr&#62;</B
>
		  </TD
><TD
>		    adds a display for expression &#60;expr&#62;
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      display /fmt &#60;expr&#62;
		    </B
>
		  </TD
><TD
>		    adds a display for expression
		    &#60;expr&#62;.  Printing evaluated &#60;expr&#62; is
		    done using the given format (see
		    <B
CLASS="COMMAND"
>print</B
> command for more on
		    formats)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>del display N</B
>, <B
CLASS="COMMAND"
>undisplay N</B
>
		  </TD
><TD
>deletes display #N</TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN806"
>Disassembly</A
></H3
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN809"
></A
><P
><B
>Table 8. WineDbg's dissassembly</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
><B
CLASS="COMMAND"
>disas</B
></TD
><TD
>disassemble from current position</TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>disas &#60;expr&#62;</B
>
		  </TD
><TD
>		    disassemble from address &#60;expr&#62;
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      disas &#60;expr&#62;,&#60;expr&#62;
		    </B
>
		  </TD
><TD
>		    disassembles code between addresses specified by
		    the two &#60;expr&#62;
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN825"
>Memory (reading, writing, typing)</A
></H3
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN828"
></A
><P
><B
>Table 9. WineDbg's memory management</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
>		    <B
CLASS="COMMAND"
>x &#60;expr&#62;</B
>
		  </TD
><TD
>		    examines memory at &#60;expr&#62; address
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      x /fmt &#60;expr&#62;
		    </B
>
		  </TD
><TD
>		    examines memory at &#60;expr&#62; address using
		    format /fmt
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      print &#60;expr&#62;
		    </B
>
		  </TD
><TD
>		    prints the value of &#60;expr&#62; (possibly using
		    its type)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      print /fmt &#60;expr&#62;
		    </B
>
		  </TD
><TD
>prints the value of &#60;expr&#62; (possibly
		    using its type)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      set &#60;lval&#62; = &#60;expr&#62;
		    </B
>
		  </TD
><TD
>		    writes the value of &#60;expr&#62; in &#60;lval&#62;
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      whatis &#60;expr&#62;
		    </B
>
		  </TD
><TD
>		    prints the C type of expression &#60;expr&#62;
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
><P
>          <TT
CLASS="FILENAME"
>/fmt</TT
> is either <TT
CLASS="FILENAME"
>/&#60;letter&#62;</TT
> or
          <TT
CLASS="FILENAME"
>/&#60;count&#62;&#60;letter&#62;</TT
> letter can be
	  <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
>s</TD
><TD
>an ASCII string</TD
></TR
><TR
><TD
>u</TD
><TD
>an Unicode UTF16 string</TD
></TR
><TR
><TD
>i</TD
><TD
>instructions (disassemble)</TD
></TR
><TR
><TD
>x</TD
><TD
>32 bit unsigned hexadecimal integer</TD
></TR
><TR
><TD
>d</TD
><TD
>32 bit signed decimal integer</TD
></TR
><TR
><TD
>w</TD
><TD
>16 bit unsigned hexadecimal integer</TD
></TR
><TR
><TD
>c</TD
><TD
>character (only printable 0x20-0x7f are actually printed)</TD
></TR
><TR
><TD
>b</TD
><TD
>8 bit unsigned hexadecimal integer</TD
></TR
><TR
><TD
>g</TD
><TD
>GUID</TD
></TR
></TBODY
></TABLE
><P
></P
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN879"
>Information on Wine's internals</A
></H3
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN882"
></A
><P
><B
>Table 10. WineDbg's Win32 objects management</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
><B
CLASS="COMMAND"
>info class</B
></TD
><TD
>		    lists all Windows' classes registered in Wine
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      info class &#60;id&#62;
		    </B
>
		  </TD
><TD
>		    prints information on Windows's class &#60;id&#62;
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info share</B
></TD
><TD
>		    lists all the dynamic libraries loaded in the
		    debugged program (including .so files, NE and PE
		    DLLs)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>		      info share &#60;N&#62;
		    </B
>
		  </TD
><TD
>		    prints information on module at address &#60;N&#62;
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info regs</B
></TD
><TD
>		    prints the value of the CPU registers
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info all-regs</B
></TD
><TD
>		    prints the value of the CPU and Floating Point registers
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>info segment &#60;N&#62;</B
>
		  </TD
><TD
>		    prints information on segment &#60;N&#62; (i386
		    only)
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>info segment</B
>
		  </TD
><TD
>		    lists all allocated segments (i386 only)
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info stack</B
></TD
><TD
>		    prints the values on top of the stack
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info map</B
></TD
><TD
>		    lists all virtual mappings used by the debugged
		    program
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>info map &#60;N&#62;</B
>
		  </TD
><TD
>		    lists all virtual mappings used by the program of
		    pid &#60;N&#62;
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>info wnd &#60;N&#62;</B
>
		  </TD
><TD
>		    prints information of Window of handle &#60;N&#62;
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info wnd</B
></TD
><TD
>		    lists all the window hierarchy starting from the
		    desktop window
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info process</B
></TD
><TD
>		    lists all w-processes in Wine session
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info thread</B
></TD
><TD
>lists all w-threads in Wine session</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>info exception</B
></TD
><TD
>		    lists the exception frames (starting from current
		    stack frame)
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="WINEDBG-DBG-CHAN"
>Debug channels</A
></H3
><P
>	  It is possible to turn on and off debug messages as you
	  are debugging using the set command.
	  See <A
HREF="#DEBUGGING"
>the Chapter called <I
>Debug Logging</I
></A
> for more details on debug
	  channels.
	</P
><P
>	  <DIV
CLASS="TABLE"
><A
NAME="AEN955"
></A
><P
><B
>Table 11. WineDbg's debug channels' management</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
>		    <B
CLASS="COMMAND"
>set + warn win</B
>
		  </TD
><TD
>turn on warn on 'win' channel</TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>set + win</B
>
		  </TD
><TD
>		    turn on warn/fixme/err/trace on 'win' channel
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>set - win</B
>
		  </TD
><TD
>		    turn off warn/fixme/err/trace on 'win' channel
		  </TD
></TR
><TR
><TD
>		    <B
CLASS="COMMAND"
>set - fixme</B
>
		  </TD
><TD
>turn off the 'fixme' class</TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-OTHERS"
>Other debuggers</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN977"
>GDB mode</A
></H3
><P
>	  WineDbg can act as a remote monitor for GDB.  This allows to
	  use all the power of GDB, but while debugging wine and/or
	  any Win32 application.  To enable this mode, just add
	  <TT
CLASS="PARAMETER"
><I
>--gdb</I
></TT
> to winedbg command line.  You'll
	  end up on a GDB prompt.  You'll have to use the GDB commands
	  (not WineDbg's).
	</P
><P
>	  However, some limitation in GDB while debugging wine (see
	  below) don't appear in this mode:
	  <P
></P
><UL
><LI
><P
>		GDB will correctly present Win32 thread
		information and breakpoint behavior
	      </P
></LI
><LI
><P
>		Moreover, it also provides support for the Dwarf II
		debug format (which became the default format (instead
		of stabs) in gcc 3.1).
	      </P
></LI
></UL
>
	</P
><P
>	  A few Wine extensions available through the monitor command.
	  <DIV
CLASS="TABLE"
><A
NAME="AEN988"
></A
><P
><B
>Table 12. WineDbg's debug channels' management</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
><B
CLASS="COMMAND"
>monitor wnd</B
></TD
><TD
>lists all window in the Wine session</TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>monitor proc</B
></TD
><TD
>		    lists all processes in the Wine session
		  </TD
></TR
><TR
><TD
><B
CLASS="COMMAND"
>monitor mem</B
></TD
><TD
>		    displays memory mapping of debugged process
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1004"
>Graphical frontends to gdb</A
></H3
><P
>          This section will describe how you can debug Wine using the
          GDB mode of winedbg and some graphical front ends to GDB for
          those of you who really like graphical debuggers.
        </P
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN1007"
>DDD</A
></H4
><P
>            Use the following steps, in this order:
	    <P
></P
><OL
TYPE="1"
><LI
><P
>		  Start the Wine debugger with a command line like:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	winedbg --gdb --no-start &#60;name_of_exe_to_debug.exe&#62;</PRE
></TD
></TR
></TABLE
>
		</P
></LI
><LI
><P
>                  Start ddd
                </P
></LI
><LI
><P
>		  In ddd, use the 'Open File' or 'Open Program' to
                  point to the Wine executable (which is either
		  wine-pthread or wine-kthread depending on your
		  settings).
		</P
></LI
><LI
><P
>		  In the output of 1/, there's a line like 
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	target remote localhost:32878</PRE
></TD
></TR
></TABLE
>
		  copy that line and paste into ddd command pane (the
		  one with the (gdb) prompt)
		</P
></LI
></OL
>
	    The program should now be loaded and up and running.  If
	    you want, you can also add in 1/ after the name of the
	    exec all the needed parameters
          </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN1021"
>kdbg</A
></H4
><P
>            Use the following steps, in this order:
            <P
></P
><OL
TYPE="1"
><LI
><P
>                  Start the Wine debugger with a command line like:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	winedbg --gdb --no-start &#60;name_of_exe_to_debug.exe&#62;</PRE
></TD
></TR
></TABLE
></P
></LI
><LI
><P
>		  In the output of 1/, there's a line like 
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	target remote localhost:32878</PRE
></TD
></TR
></TABLE
>
		  Start kdbg with
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>kdbg -r localhost:32878 wine</PRE
></TD
></TR
></TABLE
>
		  localhost:32878 is not a fixed value, but has been
		  printed in step 1/. 'wine' should also be the full
		  path to the Wine executable (which is either
		  wine-pthread or wine-kthread depending on your settings).
		</P
></LI
></OL
>
	    The program should now be loaded and up and running.  If
	    you want, you can also add in 1/ after the name of the
	    exec all the needed parameters
          </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1032"
>Using other Unix debuggers</A
></H3
><P
>          You can also use other debuggers (like
          <B
CLASS="COMMAND"
>gdb</B
>), but you must be aware of a few
          items:
        </P
><P
>          You need to attach the unix debugger to the correct unix
          process (representing the correct windows thread) (you can
          "guess" it from a <B
CLASS="COMMAND"
>ps fax</B
> for example:
          When running the emulator, usually the first two
          <CODE
CLASS="VARNAME"
>upids</CODE
> are for the Windows' application
          running the desktop, the first thread of the application is
          generally the third <CODE
CLASS="VARNAME"
>upid</CODE
>; when running a
          Winelib program, the first thread of the application is
          generally the first <CODE
CLASS="VARNAME"
>upid</CODE
>)
        </P
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>	    If you plan to used <B
CLASS="COMMAND"
>gdb</B
> for a
	    multi-threaded Wine application (native or Winelib), then
	    <B
CLASS="COMMAND"
>gdb</B
> will be able to handle the multiple
	    threads directly only if:
	    <P
></P
><UL
><LI
><P
>		  Wine is running on the pthread model (it won't work
		  in the kthread one).  See the Wine architecture
		  documentation for further details.
		</P
></LI
><LI
><P
>		  <B
CLASS="COMMAND"
>gdb</B
> supports the multi-threading
		  (you need gdb at least 5.0 for that).
		</P
></LI
></UL
>
	    In the unfortunate case (no direct thread support in
	    <B
CLASS="COMMAND"
>gdb</B
> because one of the above conditions
	    is false), you'll have to spawn a different
	    <B
CLASS="COMMAND"
>gdb</B
> session for each Windows' thread
	    you wish to debug (which means no synchronization for
	    debugging purposes between the various threads).
          </P
></TD
></TR
></TABLE
></DIV
><P
>          Here's how to get info about the current execution status of a
          certain Wine process:
        </P
><P
>          Change into your Wine source dir and enter:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>$ gdb wine
        </PRE
></TD
></TR
></TABLE
><P
>          Switch to another console and enter <B
CLASS="COMMAND"
>ps ax | grep
            wine</B
> to find all wine processes.  Inside
          <B
CLASS="COMMAND"
>gdb</B
>, repeat for all Wine processes:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>(gdb) attach <KBD
CLASS="USERINPUT"
>PID</KBD
>
        </PRE
></TD
></TR
></TABLE
><P
>          with <KBD
CLASS="USERINPUT"
>PID</KBD
> being the process ID of one of
          the Wine processes.  Use
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>(gdb) bt
        </PRE
></TD
></TR
></TABLE
><P
>          to get the backtrace of the current Wine process, i.e. the
          function call history.  That way you can find out what the
          current process is doing right now.  And then you can use
          several times:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>(gdb) n
        </PRE
></TD
></TR
></TABLE
><P
>          or maybe even
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>(gdb) b <KBD
CLASS="USERINPUT"
>SomeFunction</KBD
>
        </PRE
></TD
></TR
></TABLE
><P
>          and
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>(gdb) c
        </PRE
></TD
></TR
></TABLE
><P
>          to set a breakpoint at a certain function and continue up to
          that function.  Finally you can enter
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>(gdb) detach
        </PRE
></TD
></TR
></TABLE
><P
>          to detach from the Wine process.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1074"
>Using other Windows debuggers</A
></H3
><P
>          You can use any Windows' debugging API compliant debugger
          with Wine.  Some reports have been made of success with
          VisualStudio debugger (in remote mode, only the hub runs
          in Wine).  GoVest fully runs in Wine.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1077"
>Main differences between winedbg and regular Unix debuggers</A
></H3
><DIV
CLASS="TABLE"
><A
NAME="AEN1079"
></A
><P
><B
>Table 13. Debuggers comparison</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><TBODY
><TR
><TD
>WineDbg</TD
><TD
>gdb</TD
></TR
><TR
><TD
>		  WineDbg debugs a Windows' process: the various
		  threads will be handled by the same WineDbg session,
		  and a breakpoint will be triggered for any thread of
		  the W-process 
                </TD
><TD
>		  gdb debugs a Windows' thread: a separate gdb session
		  is needed for each thread of a Windows' process and
		  a breakpoint will be triggered only for the w-thread
		  debugged 
                </TD
></TR
><TR
><TD
>		  WineDbg supports debug information from stabs
		  (standard Unix format) and Microsoft's C, CodeView,
		  .DBG
                </TD
><TD
>		  GDB supports debug information from stabs (standard
		  Unix format) and Dwarf II.
                </TD
></TR
></TBODY
></TABLE
></DIV
></DIV
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="DEBUGGING"
></A
>Debug Logging</H1
><P
>          To better manage the large volume of debugging messages that
	  Wine can generate, we divide the messages on a component basis, 
	  and classify them based on the severity of the reported problem.
	  Therefore a message belongs to a <I
CLASS="EMPHASIS"
>channel</I
>
	  and a <I
CLASS="EMPHASIS"
>class</I
> respectively.
	</P
><P
>	  This section will describe the debugging classes, how you can 
	  create a new debugging channel, what the debugging API is,
	  and how you can control the debugging output. A picture is
	  worth a thousand words, so here are a few examples of the
	  debugging API in action:
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>ERR("lock_count == 0 ... please report\n");
FIXME("Unsupported RTL style!\n");
WARN(": file seems to be truncated!\n");
TRACE("[%p]: new horz extent = %d\n", hwnd, extent );
MESSAGE( "Could not create graphics driver '%s'\n", buffer );
	  </PRE
></TD
></TR
></TABLE
>
        </P
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-CLASSES"
>Debugging classes</A
></H2
><P
>	  A debugging class categorizes a message based on the severity
	  of the reported problem. There is a fixed set of classes, and
	  you must carefully choose the appropriate one for your messages.
          There are five classes of messages:
        </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>FIXME</TT
></DT
><DD
><P
>                Messages in this class are meant to signal unimplemented
		features, known bugs, etc. They serve as a constant and
		active reminder of what needs to be done.
              </P
></DD
><DT
><TT
CLASS="LITERAL"
>ERR</TT
></DT
><DD
><P
>                Messages in this class indicate serious errors in
                Wine, such as as conditions that should never happen
		by design.
              </P
></DD
><DT
><TT
CLASS="LITERAL"
>WARN</TT
></DT
><DD
><P
>                These are warning messages. You should report a
                warning when something unwanted happens, and the
		function cannot deal with the condition. This
		is seldomly used since proper functions can usually
		report failures back to the caller. Think twice before
		making the message a warning.
              </P
></DD
><DT
><TT
CLASS="LITERAL"
>TRACE</TT
></DT
><DD
><P
>                These are detailed debugging messages that are mainly
                useful to debug a component. These are turned off unless
		explicitly enabled.
              </P
></DD
><DT
><TT
CLASS="LITERAL"
>MESSAGE</TT
></DT
><DD
><P
>		There messages are intended for the end user. They do not
		belong to any channel. As with warnings, you will seldomly
		need to output such messages.
              </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-CHANNELS"
>Debugging channels</A
></H2
><P
>          Each component is assigned a debugging channel. The
          identifier of the channel must be a valid C identifier 
	  (reserved word like <SPAN
CLASS="TYPE"
>int</SPAN
> or <SPAN
CLASS="TYPE"
>static</SPAN
>
	  are premitted). To use a new channel, simply use it in
	  your code. It will be picked up automatically by the build process.
        </P
><P
>	Typically, a file contains code pertaining to only one component,
	and as such, there is only one channel to output to. You can declare
	a default chanel for the file using the 
	<SPAN
CLASS="SYMBOL"
>WINE_DEFAULT_DEBUG_CHANNEL()</SPAN
> macro:
        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(xxx);
...

    FIXME("some unimplemented feature", ...);
...
    if (zero != 0)
        ERR("This should never be non-null: %d", zero);
...
        </PRE
></TD
></TR
></TABLE
>
	</P
><P
>	  In rare situations there is a need to output to more than one
	  debug channel per file. In such cases, you need to declare
	  all the additional channels at the top of the file, and
	  use the _-version of the debugging macros:
        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(xxx);
WINE_DECLARE_DEBUG_CHANNEL(yyy);
WINE_DECLARE_DEBUG_CHANNEL(zzz);
...

    FIXME("this one goes to xxx channel");
...
    FIXME_(yyy)("Some other msg for the yyy channel");
...
    WARN_(zzz)("And yet another msg on another channel!");
...
        </PRE
></TD
></TR
></TABLE
>
	</P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-CHECKING"
>Are we debugging?</A
></H2
><P
>          To test whether the debugging channel <TT
CLASS="LITERAL"
>xxx</TT
> is
          enabled, use the <SPAN
CLASS="SYMBOL"
>TRACE_ON</SPAN
>, <SPAN
CLASS="SYMBOL"
>WARN_ON</SPAN
>,
	  <SPAN
CLASS="SYMBOL"
>FIXME_ON</SPAN
>, or <SPAN
CLASS="SYMBOL"
>ERR_ON</SPAN
> macros. For
	  example:
        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>if(TRACE_ON(atom)){
    ...blah...
}
        </PRE
></TD
></TR
></TABLE
>
          You should normally need to test only if <TT
CLASS="LITERAL"
>TRACE_ON</TT
>, 
	  all the others are very seldomly used. With careful coding, you
	  can avoid the use of these macros, which is generally desired.
        </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-HELPERS"
>Helper functions</A
></H2
><P
>          Resource identifiers can be either strings or numbers. To
          make life a bit easier for outputting these beasts (and to
          help you avoid the need to build the message in memory), I
          introduced a new function called <TT
CLASS="FUNCTION"
>debugres</TT
>.
        </P
><P
>          The function is defined in <TT
CLASS="FILENAME"
>wine/debug.h</TT
>
          and has the following prototype:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>LPSTR debugres(const void *id);
        </PRE
></TD
></TR
></TABLE
><P
>          It takes a pointer to the resource id and returns a nicely
          formatted string of the identifier (which can be a string or
	  a number, depending on the value of the high word).
	  Numbers are formatted as such:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#xxxx
        </PRE
></TD
></TR
></TABLE
><P
>	  while strings as:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>'some-string'
        </PRE
></TD
></TR
></TABLE
><P
>	  Simply use it in your code like this:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#include "wine/debug.h"

...

   TRACE("resource is %s", debugres(myresource));
        </PRE
></TD
></TR
></TABLE
><P
>	Many times strings need to be massaged before output:
	they may be <TT
CLASS="LITERAL"
>NULL</TT
>, contain control 
	characters, or they may be too long. Similarly, Unicode
	strings need to be converted to ASCII for usage with
	the debugging API. For all this, you can use the
	<TT
CLASS="FUNCTION"
>debugstr_[aw]n?</TT
> familly of functions:
          <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>HANDLE32 WINAPI YourFunc(LPCSTR s)
{
    FIXME("(%s): stub\n", debugstr_a(s));
}
          </PRE
></TD
></TR
></TABLE
>
        </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-CONTROL"
>Controlling the debugging output</A
></H2
><P
>	It is possible to turn on and off debugging output from
	within the debugger using the set command. Please see the
	WineDbg Command Reference section 
	(<A
HREF="#WINEDBG-DBG-CHAN"
>the Section called <I
>Debug channels</I
> in the Chapter called <I
>Debugging Wine</I
></A
>) for how to do this.
      </P
><P
>        You can do the same using the task manager
	(<B
CLASS="COMMAND"
>taskmgr</B
>) and selecting your application in
	the application list. Right clicking on the application, and
	selecting the debug option in the popup menu, will let you
	select the modifications you want on the debug channels.
      </P
><P
>	  Another way to conditionally log debug output (e.g. in case of
	  very large installers which may create gigabytes of log
	  output) is to create a pipe:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	<TT
CLASS="PROMPT"
>$</TT
> <KBD
CLASS="USERINPUT"
>mknod /tmp/debug_pipe p</KBD
>
	</PRE
></TD
></TR
></TABLE
><P
>	  and then to run wine like that:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	<TT
CLASS="PROMPT"
>$</TT
> <KBD
CLASS="USERINPUT"
>WINEDEBUG=+relay,+snoop wine setup.exe &#38;&#62;/tmp/debug_pipe</KBD
>
	</PRE
></TD
></TR
></TABLE
><P
>	  Since the pipe is initially blocking (and thus wine as a whole),
	  you have to activate it by doing:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	<TT
CLASS="PROMPT"
>$</TT
> <KBD
CLASS="USERINPUT"
>cat /tmp/debug_pipe</KBD
>
	</PRE
></TD
></TR
></TABLE
><P
>	  (press Ctrl-C to stop pasting the pipe content)
	</P
><P
>	  Once you are about to approach the problematic part of the program,
	  you just do:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	<TT
CLASS="PROMPT"
>$</TT
> <KBD
CLASS="USERINPUT"
>cat /tmp/debug_pipe &#62;/tmp/wine.log</KBD
>
	</PRE
></TD
></TR
></TABLE
><P
>	  to capture specifically the part that interests you from the
	  pipe without wasting excessive amounts of HDD space and
	  slowing down installation considerably.
	</P
><P
>          The <TT
CLASS="PARAMETER"
><I
>WINEDEBUG</I
></TT
> environment variable
          controls the output of the debug messages.
	  It has the following syntax:
	  <TT
CLASS="PARAMETER"
><I
>WINEDEBUG= [yyy]#xxx[,[yyy1]#xxx1]*</I
></TT
>
        </P
><P
></P
><UL
><LI
><P
>	      where
              <TT
CLASS="LITERAL"
>#</TT
> is either <TT
CLASS="LITERAL"
>+</TT
> or
              <TT
CLASS="LITERAL"
>-</TT
>
            </P
></LI
><LI
><P
>              when the optional class argument (<TT
CLASS="LITERAL"
>yyy</TT
>)
              is not present, then the statement will
              enable(<TT
CLASS="LITERAL"
>+</TT
>)/disable(<TT
CLASS="LITERAL"
>-</TT
>)
              all messages for the given channel (<TT
CLASS="LITERAL"
>xxx</TT
>)
              on all classes. For example:
            </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>WINEDEBUG=+reg,-file
            </PRE
></TD
></TR
></TABLE
><P
>              enables all messages on the <TT
CLASS="LITERAL"
>reg</TT
>
              channel and disables all messages on the
              <TT
CLASS="LITERAL"
>file</TT
> channel.
            </P
></LI
><LI
><P
>              when the optional class argument (<TT
CLASS="LITERAL"
>yyy</TT
>)
              is present,  then the statement will enable
              (<TT
CLASS="LITERAL"
>+</TT
>)/disable(<TT
CLASS="LITERAL"
>-</TT
>)
              messages for the given channel (<TT
CLASS="LITERAL"
>xxx</TT
>)
              only on the given class. For example:
            </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>WINEDEBUG=trace+reg,warn-file
            </PRE
></TD
></TR
></TABLE
><P
>              enables trace messages on the <TT
CLASS="LITERAL"
>reg</TT
>
              channel and disables warning messages on the
              <TT
CLASS="LITERAL"
>file</TT
> channel.
            </P
></LI
><LI
><P
>              also, the pseudo-channel all is also supported and it
              has the  intuitive semantics:
            </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    WINEDEBUG=+all      -- enables all debug messages
    WINEDEBUG=-all      -- disables all debug messages
    WINEDEBUG=yyy+all   -- enables debug messages for class yyy on all
                           channels.
    WINEDEBUG=yyy-all   -- disables debug messages for class yyy on all
                           channels.
            </PRE
></TD
></TR
></TABLE
><P
>              So, for example:
            </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    WINEDEBUG=warn-all  -- disables all warning messages.
            </PRE
></TD
></TR
></TABLE
></LI
></UL
><P
>          Also, note that at the moment:
        </P
><P
></P
><UL
><LI
><P
>              the <TT
CLASS="LITERAL"
>FIXME</TT
> and <TT
CLASS="LITERAL"
>ERR</TT
>
              classes are enabled by default
            </P
></LI
><LI
><P
>              the <TT
CLASS="LITERAL"
>TRACE</TT
> and <TT
CLASS="LITERAL"
>WARN</TT
>
              classes are disabled by default
            </P
></LI
></UL
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-COMPILING"
>Compiling Out Debugging Messages</A
></H2
><P
>          To compile out the debugging messages, provide
          <B
CLASS="COMMAND"
>configure</B
> with the following options:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    --disable-debug      -- turns off TRACE, WARN, and FIXME (and DUMP).
    --disable-trace      -- turns off TRACE only.
        </PRE
></TD
></TR
></TABLE
><P
>          This will result in an executable that, when stripped, is
          about 15%-20% smaller.  Note, however, that you will not be
          able to effectively debug Wine without these messages.
        </P
><P
>          This feature has not been extensively tested--it may subtly
          break some things.
        </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DBG-NOTES"
>A Few Notes on Style</A
></H2
><P
>          This new scheme makes certain things more consistent but
          there is still room for improvement by using a common style
          of debug messages. Before I continue, let me note that the
          output format is the following:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>yyy:xxx:fff &#60;message&#62;

where:
  yyy = the class (fixme, err, warn, trace)
  xxx = the channel (atom, win, font, etc)
  fff = the function name
        </PRE
></TD
></TR
></TABLE
><P
>          these fields are output automatically. All you have to
          provide is the &#60;message&#62; part.
        </P
><P
>          So here are some ideas:
        </P
><P
></P
><UL
><LI
><P
>              do not include the name of the function: it is included automatically
            </P
></LI
><LI
><P
>              if you want to output the parameters of the function, do
              it as the first thing and include them in parentheses,
              like this:
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>TRACE("(%d, %p, ...)\n", par1, par2, ...);
              </PRE
></TD
></TR
></TABLE
>
            </P
></LI
><LI
><P
>              if you want to name a parameter, use <TT
CLASS="LITERAL"
>=</TT
> :
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>TRACE("(fd=%d, file=%s): stub\n", fd, name);
              </PRE
></TD
></TR
></TABLE
>
            </P
></LI
><LI
><P
>              for stubs, you should output a <TT
CLASS="LITERAL"
>FIXME</TT
>
              message. I suggest this style:
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>FIXME("(%x, %d, ...): stub\n", par1, par2, ...);
              </PRE
></TD
></TR
></TABLE
>
            </P
></LI
><LI
><P
>              try to output one line per message. That is, the format
              string should contain only one <TT
CLASS="LITERAL"
>\n</TT
> and it
              should always appear at the end of the string. 
            </P
></LI
><LI
><P
>	      if the output string needs to be dynamically constructed,
              render it in memory before outputting it:
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>char buffer[128] = "";

if (flags &#38; FLAG_A) strcat(buffer, "FLAG_A ");
if (flags &#38; FLAG_B) strcat(buffer, "FLAG_B ");
if (flags &#38; FLAG_C) strcat(buffer, "FLAG_C ");
TRACE("flags = %s\n", buffer);
              </PRE
></TD
></TR
></TABLE
>
              Most of the time however, it is better to create a helper
              function that renders to a temporary buffer:
              <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>static const char *dbgstr_flags(int flags)
{
    char buffer[128] = "";

    if (flags &#38; FLAG_A) strcat(buffer, "FLAG_A ");
    if (flags &#38; FLAG_B) strcat(buffer, "FLAG_B ");
    if (flags &#38; FLAG_C) strcat(buffer, "FLAG_C ");
    return wine_dbg_sprintf("flags = %s\n\n", buffer);
}

...

TRACE("flags = %s\n", dbgstr_flags(flags));
              </PRE
></TD
></TR
></TABLE
>
            </P
></LI
></UL
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="OTHERDEBUG"
></A
>Other debugging techniques</H1
><DIV
CLASS="SECT1"
><H2
CLASS="SECT1"
><A
NAME="HARDWARE-TRACE"
>Doing A Hardware Trace</A
></H2
><P
>        The primary reason to do this is to reverse engineer a
        hardware device for which you don't have documentation, but
        can get to work under Wine.
      </P
><P
>        This lot is aimed at parallel port devices, and in particular
        parallel port scanners which are now so cheap they are
        virtually being given away. The problem is that few
        manufactures will release any programming information which
        prevents drivers being written for Sane, and the traditional
        technique of using DOSemu to produce the traces does not work
        as the scanners invariably only have drivers for Windows.
      </P
><P
>        Presuming that you have compiled and installed wine the first
        thing to do is to enable direct hardware access to your
        parallel port. To do this edit <TT
CLASS="FILENAME"
>config</TT
>
        (usually in <TT
CLASS="FILENAME"
>~/.wine/</TT
>) and in the
        ports section add the following two lines
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>read=0x378,0x379,0x37a,0x37c,0x77a
write=0x378,x379,0x37a,0x37c,0x77a
      </PRE
></TD
></TR
></TABLE
><P
>        This adds the necessary access required for SPP/PS2/EPP/ECP
        parallel port on LPT1. You will need to adjust these number
        accordingly if your parallel port is on LPT2 or LPT0.
      </P
><P
>        When starting wine use the following command line, where
        <TT
CLASS="LITERAL"
>XXXX</TT
> is the program you need to run in
        order to access your scanner, and <TT
CLASS="LITERAL"
>YYYY</TT
> is
        the file your trace will be stored in:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>WINEDEBUG=+io wine XXXX 2&#62; &#62;(sed 's/^[^:]*:io:[^ ]* //' &#62; YYYY)
      </PRE
></TD
></TR
></TABLE
><P
>        You will need large amounts of hard disk space (read hundreds
        of megabytes if you do a full page scan), and for reasonable
        performance a really fast processor and lots of RAM.
      </P
><P
>	You will need to postprocess the output into a more manageable
	format, using the <B
CLASS="COMMAND"
>shrink</B
> program. First
        you need to compile the source (which is located at the end of
	this section):
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>cc shrink.c -o shrink
      </PRE
></TD
></TR
></TABLE
>
      </P
><P
>        Use the <B
CLASS="COMMAND"
>shrink</B
> program to reduce the
        physical size of the raw log as follows:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>cat log | shrink &#62; log2
      </PRE
></TD
></TR
></TABLE
><P
>        The trace has the basic form of
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>XXXX &#62; YY @ ZZZZ:ZZZZ
      </PRE
></TD
></TR
></TABLE
><P
>        where <TT
CLASS="LITERAL"
>XXXX</TT
> is the port in hexadecimal being
        accessed, <TT
CLASS="LITERAL"
>YY</TT
> is the data written (or read)
        from the port, and <TT
CLASS="LITERAL"
>ZZZZ:ZZZZ</TT
> is the address
        in memory of the instruction that accessed the port. The
        direction of the arrow indicates whether the data was written
        or read from the port.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#62; data was written to the port
&#60; data was read from the port
      </PRE
></TD
></TR
></TABLE
><P
>        My basic tip for interpreting these logs is to pay close
        attention to the addresses of the IO instructions. Their
        grouping and sometimes proximity should reveal the presence of
        subroutines in the driver. By studying the different versions
        you should be able to work them out. For example consider the
        following section of trace from my UMAX Astra 600P
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>0x378 &#62; 55 @ 0297:01ec
0x37a &#62; 05 @ 0297:01f5
0x379 &#60; 8f @ 0297:01fa
0x37a &#62; 04 @ 0297:0211
0x378 &#62; aa @ 0297:01ec
0x37a &#62; 05 @ 0297:01f5
0x379 &#60; 8f @ 0297:01fa
0x37a &#62; 04 @ 0297:0211
0x378 &#62; 00 @ 0297:01ec
0x37a &#62; 05 @ 0297:01f5
0x379 &#60; 8f @ 0297:01fa
0x37a &#62; 04 @ 0297:0211
0x378 &#62; 00 @ 0297:01ec
0x37a &#62; 05 @ 0297:01f5
0x379 &#60; 8f @ 0297:01fa
0x37a &#62; 04 @ 0297:0211
0x378 &#62; 00 @ 0297:01ec
0x37a &#62; 05 @ 0297:01f5
0x379 &#60; 8f @ 0297:01fa
0x37a &#62; 04 @ 0297:0211
0x378 &#62; 00 @ 0297:01ec
0x37a &#62; 05 @ 0297:01f5
0x379 &#60; 8f @ 0297:01fa
0x37a &#62; 04 @ 0297:0211
      </PRE
></TD
></TR
></TABLE
><P
>        As you can see there is a repeating structure starting at
        address <TT
CLASS="LITERAL"
>0297:01ec</TT
> that consists of four io
        accesses on the parallel port. Looking at it the first io
        access writes a changing byte to the data port the second
        always writes the byte <TT
CLASS="LITERAL"
>0x05</TT
> to the control
        port, then a value which always seems to
        <TT
CLASS="LITERAL"
>0x8f</TT
> is read from the status port at which
        point a byte <TT
CLASS="LITERAL"
>0x04</TT
> is written to the control
        port. By studying this and other sections of the trace we can
        write a C routine that emulates this, shown below with some
        macros to make reading/writing on the parallel port easier to
        read.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#define r_dtr(x)        inb(x)
#define r_str(x)        inb(x+1)
#define r_ctr(x)        inb(x+2)
#define w_dtr(x,y)      outb(y, x)
#define w_str(x,y)      outb(y, x+1)
#define w_ctr(x,y)      outb(y, x+2)

/* Seems to be sending a command byte to the scanner */
int udpp_put(int udpp_base, unsigned char command)
{
    int loop, value;

    w_dtr(udpp_base, command);
    w_ctr(udpp_base, 0x05);

    for (loop=0; loop &#60; 10; loop++)
        if ((value = r_str(udpp_base)) &#38; 0x80) 
	{
            w_ctr(udpp_base, 0x04);
            return value &#38; 0xf8;
        }

    return (value &#38; 0xf8) | 0x01;
}
      </PRE
></TD
></TR
></TABLE
><P
>        For the UMAX Astra 600P only seven such routines exist (well
        14 really, seven for SPP and seven for EPP). Whether you
        choose to disassemble the driver at this point to verify the
        routines is your own choice. If you do, the address from the
        trace should help in locating them in the disassembly.
      </P
><P
>        You will probably then find it useful to write a script/perl/C
        program to analyse the logfile and decode them futher as this
        can reveal higher level grouping of the low level routines.
        For example from the logs from my UMAX Astra 600P when decoded
        further reveal (this is a small snippet)
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>start:
put: 55 8f
put: aa 8f
put: 00 8f
put: 00 8f
put: 00 8f
put: c2 8f
wait: ff
get: af,87
wait: ff
get: af,87
end: cc
start:
put: 55 8f
put: aa 8f
put: 00 8f
put: 03 8f
put: 05 8f
put: 84 8f
wait: ff
      </PRE
></TD
></TR
></TABLE
><P
>        From this it is easy to see that <CODE
CLASS="VARNAME"
>put</CODE
>
        routine is often grouped together in five successive calls
        sending information to the scanner. Once these are understood
        it should be possible to process the logs further to show the
        higher level routines in an easy to see format. Once the
        highest level format that you can derive from this process is
        understood, you then need to produce a series of scans varying
        only one parameter between them, so you can discover how to
        set the various parameters for the scanner.
      </P
><P
>        The following is the <TT
CLASS="FILENAME"
>shrink.c</TT
> program:
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>/* Copyright David Campbell &#60;campbell@torque.net&#62; */
#include &#60;stdio.h&#62;
#include &#60;string.h&#62;

int main (void)
{
    char buff[256], lastline[256] = "";
    int count = 0;

    while (!feof (stdin))
    {
        fgets (buff, sizeof (buff), stdin);
        if (strcmp (buff, lastline))
	{
	    if (count &#62; 1)
	        printf ("# Last line repeated %i times #\n", count);
	    printf ("%s", buff);
	    strcpy (lastline, buff);
	    count = 1;
	}
        else count++;
    }
    return 0;
}
      </PRE
></TD
></TR
></TABLE
>
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="UNDOC-FUNC"
>Understanding undocumented APIs</A
></H2
><P
>	Some background:  On the i386 class of machines, stack entries are
	usually dword (4 bytes) in size, little-endian. The stack grows
	downward in memory. The stack pointer, maintained in the 
	<TT
CLASS="LITERAL"
>esp</TT
> register, points to the last valid entry; 
	thus, the operation of pushing a value onto the stack involves 
	decrementing <TT
CLASS="LITERAL"
>esp</TT
> and then moving the value into 
	the memory pointed to by <TT
CLASS="LITERAL"
>esp</TT
>
	(i.e., <TT
CLASS="LITERAL"
>push p</TT
> in assembly resembles 
	<TT
CLASS="LITERAL"
>*(--esp) = p;</TT
> in C). Removing (popping)
	values off the stack is the reverse (i.e., <TT
CLASS="LITERAL"
>pop p</TT
> 
	corresponds to <TT
CLASS="LITERAL"
>p = *(esp++);</TT
> in C).
      </P
><P
>	In the <TT
CLASS="LITERAL"
>stdcall</TT
> calling convention, arguments are 
	pushed onto the stack right-to-left. For example, the C call
	<TT
CLASS="FUNCTION"
>myfunction(40, 20, 70, 30);</TT
> is expressed in 
	Intel assembly as:
	<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    push 30
    push 70
    push 20
    push 40
    call myfunction
	</PRE
></TD
></TR
></TABLE
>
	The called function is responsible for removing the arguments 
	off the stack. Thus, before the call to myfunction, the
	stack would look like:
	<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>             [local variable or temporary]
             [local variable or temporary]
              30
              70
              20
    esp -&#62;    40
	</PRE
></TD
></TR
></TABLE
>
	After the call returns, it should look like:
	<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>             [local variable or temporary]
    esp -&#62;   [local variable or temporary]
	</PRE
></TD
></TR
></TABLE
>
      </P
><P
>	To restore the stack to this state, the called function must know how
	many arguments to remove (which is the number of arguments it takes).
	This is a problem if the function is undocumented.
      </P
><P
>	One way to attempt to document the number of arguments each function
	takes is to create a wrapper around that function that detects the
	stack offset. Essentially, each wrapper assumes that the function will 
	take a large number of arguments. The wrapper copies each of these 
	arguments into its stack, calls the actual function, and then calculates 
	the number of arguments by checking esp before and after the call.
      </P
><P
>	The main problem with this scheme is that the function must actually
	be called from another program. Many of these functions are seldom
	used. An attempt was made to aggressively query each function in a
	given library (<TT
CLASS="FILENAME"
>ntdll.dll</TT
>) by passing 64 arguments, 
	all 0, to each function. Unfortunately, Windows NT quickly goes to a 
	blue screen of death, even if the program is run from a 
	non-administrator account.
      </P
><P
>	Another method that has been much more successful is to attempt to
	figure out how many arguments each function is removing from the
	stack. This instruction, <TT
CLASS="LITERAL"
>ret hhll</TT
> (where 
	<SPAN
CLASS="SYMBOL"
>hhll</SPAN
> is the number of bytes to remove, i.e. the 
	number of arguments times 4), contains the bytes 
	<TT
CLASS="LITERAL"
>0xc2 ll hh</TT
> in memory. It is a reasonable
	assumption that few, if any, functions take more than 16 arguments; 
	therefore, simply searching for 
	<TT
CLASS="LITERAL"
>hh == 0 &#38;&#38; ll &#60; 0x40</TT
>  starting from the 
	address of a function yields the correct number of arguments most 
	of the time.
      </P
><P
>	Of course, this is not without errors. <TT
CLASS="LITERAL"
>ret 00ll</TT
> 
	is not the only instruction that can have the byte sequence 
	<TT
CLASS="LITERAL"
>0xc2 ll 0x0</TT
>; for example, 
	<TT
CLASS="LITERAL"
>push 0x000040c2</TT
> has the byte sequence
	<TT
CLASS="LITERAL"
>0x68 0xc2 0x40 0x0 0x0</TT
>, which matches 
	the above. Properly, the utility should look for this sequence 
	only on an instruction boundary; unfortunately, finding 
	instruction boundaries on an i386 requires implementing a full 
	disassembler -- quite a daunting task. Besides, the probability 
	of having such a byte sequence that is not the actual return 
	instruction is fairly low.
      </P
><P
>	Much more troublesome is the non-linear flow of a function. For
	example, consider the following two functions:
	<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    somefunction1:
        jmp  somefunction1_impl

    somefunction2:
        ret  0004

    somefunction1_impl:
        ret  0008
	</PRE
></TD
></TR
></TABLE
>
	In this case, we would incorrectly detect both 
	<TT
CLASS="FUNCTION"
>somefunction1</TT
> and
	<TT
CLASS="FUNCTION"
>somefunction2</TT
> as taking only a single 
	argument, whereas <TT
CLASS="FUNCTION"
>somefunction1</TT
> really 
	takes two arguments.
      </P
><P
>	With these limitations in mind, it is possible to implement
	more stubs 
	in Wine and, eventually, the functions themselves.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN1348"
>How to do regression testing using Git</A
></H2
><P
>	A problem that can happen sometimes is 'it used to work
	before, now it doesn't anymore...'. Here is a step by step
	procedure to try to pinpoint when the problem occurred. This
	is <I
CLASS="EMPHASIS"
>NOT</I
> for casual users.
      </P
><P
></P
><OL
TYPE="1"
><LI
><P
>	    Clone the <SPAN
CLASS="QUOTE"
>"Git"</SPAN
> repository from winehq. 
	    It's more than 90Mb, so you it may take some time with
	    a slow Internet connection.
	  </P
></LI
><LI
><P
>            If you found that something broke between wine-20041019 and
            wine-20050930 (these are [WWW] release tags). To start regression
            testing we run:
	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>git bisect start
git bisect good wine-20041019
git bisect bad wine-20050930
	    </PRE
></TD
></TR
></TABLE
>
	  </P
><P
>            If you have exact date/time instead of a release you will need
            to use sha1 IDs from <TT
CLASS="FILENAME"
>git log</TT
>.
	  </P
></LI
><LI
><P
>            Having told Git when things were working and when they broke,
            it will automatically "position" your source tree to the middle.
            So all you need to do is build the source:
	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>./configure &#38;&#38; make clean &#38;&#38; make depend &#38;&#38; make
./wine 'c:\test.exe'
	    </PRE
></TD
></TR
></TABLE
>
            If the version of Wine that Git picked still has the bug, run:
	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>git bisect bad
	    </PRE
></TD
></TR
></TABLE
>
            and if it does not, run:
	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>git bisect good
	    </PRE
></TD
></TR
></TABLE
>
            When you run this command, Git will checkout a new version of Wine
            for you to rebuild, so repeat this step again.  When the regression 
            has been isolated, git will inform you.
	  </P
><P
>            To find out what's left to test, try:
	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>git bisect visualize.
	    </PRE
></TD
></TR
></TABLE
>
	  </P
></LI
><LI
><P
>            When you have found the bad patch and want to go back to the current HEAD run:
	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>git bisect reset
	    </PRE
></TD
></TR
></TABLE
>
	  </P
></LI
><LI
><P
>	    If you find the patch that is the cause of the problem,
	    you have almost won; report about it to
	    <A
HREF="http://bugs.winehq.org/"
TARGET="_top"
>Wine Bugzilla</A
>
	    or subscribe to wine-devel and post it there. There is a
	    chance that the author will jump in to suggest a fix; or
	    there is always the possibility to look hard at the patch
	    until it is coerced to reveal where is the bug :-) 
	  </P
></LI
></OL
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN1374"
>Which code has been tested?</A
></H2
><P
>        Deciding what code should be tested next can be a difficult
        decision.  And in any given project, there is always code that
        isn't tested where bugs could be lurking.  This section goes
        over how to identify these sections using a tool called gcov.
      </P
><P
>        To use gcov on wine, do the following:
      </P
><P
></P
><OL
TYPE="1"
><LI
><P
>           In order to activate code coverage in the wine source code,
           when running <B
CLASS="COMMAND"
>make</B
> set
           <TT
CLASS="LITERAL"
>CFLAGS</TT
> like so <B
CLASS="COMMAND"
>make
           CFLAGS="-fprofile-arcs -ftest-coverage"</B
>.  Note that
           this can be done at any directory level.  Since compile 
           and run time are significantly  increased by these flags, you
           may want to only use these flags inside a given dll directory.
         </P
></LI
><LI
><P
>           Run any application or test suite.
         </P
></LI
><LI
><P
>           Run gcov on the file which you would like to know more
           about code coverage.
         </P
></LI
></OL
><P
>        The following is an example situation when using gcov to
        determine the coverage of a file could be helpful.  We'll use
        the <TT
CLASS="FILENAME"
>dlls/lzexpand/lzexpand_main.c.</TT
> file.
        At one time the code in this file was not fully tested (as it
        may still be).  For example at the time of this writing, the
        function <TT
CLASS="FUNCTION"
>LZOpenFileA</TT
> had the following
        lines in it:
        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>if ((mode&#38;~0x70)!=OF_READ)
        return fd;
if (fd==HFILE_ERROR)
        return HFILE_ERROR;
cfd=LZInit(fd);
if ((INT)cfd &#60;= 0) return fd;
return cfd;
        </PRE
></TD
></TR
></TABLE
>
        Currently there are a few tests written to test this function;
        however, these tests don't check that everything is correct.
        For instance, <CODE
CLASS="CONSTANT"
>HFILE_ERROR</CODE
> may be the wrong
        error code to return.  Using gcov and directed tests, we can
        validate the correctness of this line of code.  First, we see
        what has been tested already by running gcov on the file. 
        To do this, do the following:
        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>git clone git://source.winehq.org/git/wine.git wine
mkdir build
cd build
../wine/configure
make depend &#38;&#38; make CFLAGS="-fprofile-arcs -ftest-coverage"
cd dlls/lxexpand/tests
make test
cd ..
gcov ../../../wine/dlls/lzexpand/lzexpand_main.c
  0.00% of 3 lines executed in file ../../../wine/include/wine/unicode.h
  Creating unicode.h.gcov.
  0.00% of 4 lines executed in file /usr/include/ctype.h
  Creating ctype.h.gcov.
  0.00% of 6 lines executed in file /usr/include/bits/string2.h
  Creating string2.h.gcov.
  100.00% of 3 lines executed in file ../../../wine/include/winbase.h
  Creating winbase.h.gcov.
  50.83% of 240 lines executed in file ../../../wine/dlls/lzexpand/lzexpand_main.c
  Creating lzexpand_main.c.gcov.
less lzexpand_main.c.gcov
        </PRE
></TD
></TR
></TABLE
>
        Note that there is more output, but only output of gcov is
        shown.  The output file
        <TT
CLASS="FILENAME"
>lzexpand_main.c.gcov</TT
> looks like this.
        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>        9:  545:        if ((mode&#38;~0x70)!=OF_READ)
        6:  546:                return fd;
        3:  547:        if (fd==HFILE_ERROR)
    #####:  548:                return HFILE_ERROR;
        3:  549:        cfd=LZInit(fd);
        3:  550:        if ((INT)cfd &#60;= 0) return fd;
        3:  551:        return cfd;
        </PRE
></TD
></TR
></TABLE
>
        <B
CLASS="COMMAND"
>gcov</B
> output consists of three components:
        the number of times a line was run, the line number, and the
        actual text of the line.  Note: If a line is optimized out by
        the compiler, it will appear as if it was never run.  The line
        of code which returns <CODE
CLASS="CONSTANT"
>HFILE_ERROR</CODE
> is
        never executed (and it is highly unlikely that it is optimized
        out), so we don't know if it is correct.  In order to validate
        this line, there are two parts of this process.  First we must
        write the test.  Please see <A
HREF="#TESTING"
>the Chapter called <I
>Writing Conformance tests</I
></A
> to
        learn more about writing tests.  We insert the following lines
        into a test case:
        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>INT file;

/* Check for nonexistent file. */
file = LZOpenFile("badfilename_", &#38;test, OF_READ);
ok(file == LZERROR_BADINHANDLE, 
   "LZOpenFile succeeded on nonexistent file\n");
LZClose(file);
       </PRE
></TD
></TR
></TABLE
>
       Once we add in this test case, we now want to know if the line
       in question is run by this test and works as expected.  You
       should be in the same directory as you left off in the previous
       command example.  The only difference is that we have to remove
       the <TT
CLASS="FILENAME"
>*.da</TT
> files in order to start the
       count over (if we leave the files than the number of times the
       line is run is just added, e.g. line 545 below would be run 19 times)
       and we remove the <TT
CLASS="FILENAME"
>*.gcov</TT
> files because
       they are out of date and need to be recreated.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>rm *.da *.gcov
cd tests
make
make test
cd ..
gcov ../../../wine/dlls/lzexpand/lzexpand_main.c
  0.00% of 3 lines executed in file ../../../wine/include/wine/unicode.h
  Creating unicode.h.gcov.
  0.00% of 4 lines executed in file /usr/include/ctype.h
  Creating ctype.h.gcov.
  0.00% of 6 lines executed in file /usr/include/bits/string2.h
  Creating string2.h.gcov.
  100.00% of 3 lines executed in file ../../../wine/include/winbase.h
  Creating winbase.h.gcov.
  51.67% of 240 lines executed in file ../../../wine/dlls/lzexpand/lzexpand_main.c
  Creating lzexpand_main.c.gcov.
less lzexpand_main.c.gcov
      </PRE
></TD
></TR
></TABLE
><P
>        Note that there is more output, but only output of gcov is
        shown.  The output file
        <TT
CLASS="FILENAME"
>lzexpand_main.c.gcov</TT
> looks like this.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>       10:  545:        if ((mode&#38;~0x70)!=OF_READ)
        6:  546:                return fd;
        4:  547:        if (fd==HFILE_ERROR)
        1:  548:                return HFILE_ERROR;
        3:  549:        cfd=LZInit(fd);
        3:  550:        if ((INT)cfd &#60;= 0) return fd;
        3:  551:        return cfd;
      </PRE
></TD
></TR
></TABLE
><P
>        Based on gcov, we now know that
        <CODE
CLASS="CONSTANT"
>HFILE_ERROR</CODE
> is returned once.  And since
        all of our other tests have remain unchanged, we can assume
        that the one time it is returned is to satisfy the one case we
        added where we check for it.  Thus we have validated a line of
        code.  While this is a cursory example, it demostrates the
        potential usefulness of this tool.  
      </P
><P
>        For a further in depth description of gcov, the official gcc
        compiler suite page for gcov is <A
HREF="http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Gcov.html"
TARGET="_top"
>        http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Gcov.html</A
>.
        There is also an excellent article written by Steve Best for
        Linux Magazine which describes and illustrates this process
        very well at
        <A
HREF="http://www.linux-mag.com/2003-07/compile_01.html"
TARGET="_top"
>        http://www.linux-mag.com/2003-07/compile_01.html</A
>. 
      </P
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="CODINGPRACTICE"
></A
>Coding Practice</H1
><P
>      This chapter describes the relevant coding practices in Wine,
      that you should be aware of before doing any serious development
      in Wine.
    </P
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="PATCH-FORMAT"
>Patch Format</A
></H2
><P
>        Patches are submitted via email to the Wine patches mailing
	list,  <CODE
CLASS="EMAIL"
>&#60;<A
HREF="mailto:wine-patches@winehq.org"
>wine-patches@winehq.org</A
>&#62;</CODE
>. Your patch
	should include: 
      </P
><P
></P
><UL
><LI
><P
>            A meaningful subject (very short description of patch)            
          </P
></LI
><LI
><P
>            A long (paragraph) description of what was wrong and what
	    is now better. (recommended)
          </P
></LI
><LI
><P
>            A change log entry (short description of what was
	    changed). 
          </P
></LI
><LI
><P
>            The patch in <SPAN
CLASS="QUOTE"
>"Git"</SPAN
> format
          </P
></LI
></UL
><P
></P
><P
>        To generate a patch using Git, first commit it to your local tree.
      </P
><P
>        Each file that you change needs to be updated with
        <B
CLASS="COMMAND"
> git update </B
>.  If you are adding or removing
        a file, use <B
CLASS="COMMAND"
> git update --add </B
> or
        <B
CLASS="COMMAND"
>git update --remove</B
> respectively.
        After updating the index, commit the change using
        <B
CLASS="COMMAND"
> git commit </B
>.  The commit message will be
        sent with your patch, and recored in the ChangeLog.
      </P
><P
>        After committing the patch, you can extract it using
        <B
CLASS="COMMAND"
>git format-patch</B
> and send it to wine-patches
        using <B
CLASS="COMMAND"
>git imap-send</B
> or simply attaching it
        to you mail manually.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="STYLE-NOTES"
>Some notes about style</A
></H2
><P
>        There are a few conventions about coding style that have
	been adopted over the years of development. The rational for
	these <SPAN
CLASS="QUOTE"
>"rules"</SPAN
> is explained for each one.
      </P
><P
></P
><UL
><LI
><P
>	    No HTML mail, since patches should be in-lined and HTML
	    turns the patch into garbage. Also it is considered bad
	    etiquette as it  uglifies the message, and is not viewable
	    by many of the subscribers.
	  </P
></LI
><LI
><P
>	    Only one change set per patch. Patches should address only
	    one bug/problem at a time. If a lot of changes need to be
	    made then it is preferred to break it into a series of
	    patches. This makes it easier to find regressions.
	  </P
></LI
><LI
><P
>	    Tabs are not forbidden but discouraged. A tab is defined
	    as 8 characters and the usual amount of indentation is 4
	    characters.
	  </P
></LI
><LI
><P
>	    C++ style comments are discouraged since some compilers
	    choke on  them.
	  </P
></LI
><LI
><P
>	    Commenting out a block of code is usually done by
	    enclosing it in  <B
CLASS="COMMAND"
>#if 0 ... #endif</B
>
	    Statements. For example.
	  </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> 
/* note about reason for commenting block */
#if 0
code
code /* comments */
code
#endif
	  </PRE
></TD
></TR
></TABLE
><P
>	    The reason for using this method is that it does not
	    require that you edit comments that may be inside the
	    block of code.
	  </P
></LI
><LI
><P
>	    Patches should be in-lined (if you can configure your
	    email client to not wrap lines), or attached as plain text
	    attachments so they can be read inline. This may mean some
	    more work for you. However it allows others to review your
	    patch easily and decreases the chances of it being
	    overlooked or forgotten.
	  </P
></LI
><LI
><P
>	    Code is usually limited to 80 columns. This helps prevent
	    mailers mangling patches by line wrap. Also it generally
	    makes code easier to read.
	  </P
></LI
><LI
><P
>	    If the patch fixes a bug in Bugzilla please provide a link
	    to the bug in the comments of the patch. This will make it
	    easier for the maintainers of Bugzilla.
	  </P
></LI
></UL
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="INLINE-ATTACHMENTS-WITH-OE"
>Inline attachments with Outlook Express</A
></H3
><P
>          Outlook Express is notorious for mangling
	  attachments. Giving the patch a <TT
CLASS="FILENAME"
>.txt</TT
>
	  extension and attaching will solve the problem for most
	  mailers including Outlook. Also, there is a way to enable
	  Outlook Express to send <TT
CLASS="FILENAME"
>.diff</TT
>
	  attachments.
        </P
><P
>          You need the following two things to make it work.
        </P
><P
></P
><OL
TYPE="1"
><LI
><P
>	      Make sure that <TT
CLASS="FILENAME"
>.diff</TT
> files have
	      \r\n line ends, because if OE detects that there is no
	      \r\n line endings it switches to quoted-printable format
	      attachments.
	    </P
></LI
><LI
><P
>	      Using regedit add key "Content Type"
	      with value "text/plain" to the
	      <TT
CLASS="FILENAME"
>.diff</TT
> extension under
	      HKEY_CLASSES_ROOT (same as for <TT
CLASS="FILENAME"
>.txt</TT
>
	      extension). This tells OE to use
	      Content-Type: text/plain instead of
	      application/octet-stream.
	    </P
></LI
></OL
><P
>          Item #1 is important. After you hit the "Send" button, go to
	  "Outbox" and using "Properties" verify the message source to
	  make sure that the mail has the correct format. You might want
	  to send several test emails to yourself too.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="ALEXANDRE-BOTTOM-LINE"
>Alexandre's Bottom Line</A
></H3
><P
>          <SPAN
CLASS="QUOTE"
>"The basic rules are: no attachments, no MIME crap, no 
          line wrapping, a single patch per mail. Basically if I can't 
          do <B
CLASS="COMMAND"
>"cat raw_mail | patch -p0"</B
> it's in the
          wrong format."</SPAN
>
        </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="PATCH-QUALITY"
>Quality Assurance</A
></H2
><P
>        (Or, "How do I get Alexandre to apply my patch quickly so I
        can build on it and it will not go stale?")
      </P
><P
>        Make sure your patch applies to the current Git HEAD
        revisions.  If a bunch of patches are committed that may
        affect whether your patch will apply cleanly then verify that
        your patch does apply! <B
CLASS="COMMAND"
>git fetch; git rebase origin</B
>
        is your friend!
      </P
><P
>        Save yourself some embarrassment and run your patched code
        against more than just your current test example.  Experience
        will tell you how much effort to apply here. If there are
        any conformance tests for the code you're working on, run them
        and make sure they still pass after your patch is applied. Running
        tests can be done by running <B
CLASS="COMMAND"
>make test</B
>. You may
        need to run <B
CLASS="COMMAND"
>make testclean</B
> to undo the results
        of a previous test run. See the <SPAN
CLASS="QUOTE"
>"testing"</SPAN
> guide for
        more details on Wine's conformance tests.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="PORTING"
>Porting Wine to new Platforms</A
></H2
><P
>      This document provides a few tips on porting Wine to your
      favorite (UNIX-based) operating system. 
    </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1494"
>Why <SPAN
CLASS="SYMBOL"
>ifdef MyOS</SPAN
> is probably a mistake.</A
></H3
><P
>	Operating systems change. Maybe yours doesn't have the
	<TT
CLASS="FILENAME"
>foo.h</TT
> header, but maybe a future
	version will have it. If you want to <SPAN
CLASS="SYMBOL"
>#include
	  &#60;foo.h&#62;</SPAN
>, it doesn't matter what operating
	system you are using; it only matters whether
	<TT
CLASS="FILENAME"
>foo.h</TT
> is there.
      </P
><P
>	Furthermore, operating systems change names or "fork" into
	several ones. An <SPAN
CLASS="SYMBOL"
>#ifdef MyOs</SPAN
> will break
	over time.
      </P
><P
>	If you use the feature of <B
CLASS="COMMAND"
>autoconf</B
> -- the
	Gnu auto-configuration utility -- wisely, you will help
	future porters automatically because your changes will test
	for <I
CLASS="EMPHASIS"
>features</I
>, not names of operating
	systems. A feature can be many things:
      </P
><P
></P
><UL
><LI
><P
>	    existence of a header file
	  </P
></LI
><LI
><P
>	    existence of a library function
	  </P
></LI
><LI
><P
>	    existence of libraries
	  </P
></LI
><LI
><P
>	    bugs in header files, library functions, the compiler, ...
	  </P
></LI
></UL
><P
>	You will need Gnu Autoconf, which you can get from your
	friendly Gnu mirror. This program takes Wine's
	<TT
CLASS="FILENAME"
>configure.ac</TT
> file and produces a
	<TT
CLASS="FILENAME"
>configure</TT
> shell script that users use
	to configure Wine to their system.
      </P
><P
>	There <I
CLASS="EMPHASIS"
>are</I
> exceptions to the "avoid
	<SPAN
CLASS="SYMBOL"
>#ifdef MyOS</SPAN
>" rule. Wine, for example, needs
	the internals of the signal stack -- that cannot easily be
	described in terms of features. Moreover, you cannot use
	<TT
CLASS="FILENAME"
>autoconf</TT
>'s <SPAN
CLASS="SYMBOL"
>HAVE_*</SPAN
>
	symbols in Wine's headers, as these may be used by Winelib
	users who may not be using a <TT
CLASS="FILENAME"
>configure</TT
>
	script.
      </P
><P
>	Let's now turn to specific porting problems and how to solve
	them.
      </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1525"
>MyOS doesn't have the <TT
CLASS="FILENAME"
>foo.h</TT
> header!</A
></H3
><P
>	This first step is to make <B
CLASS="COMMAND"
>autoconf</B
> check
	for this header. In <TT
CLASS="FILENAME"
>configure.in</TT
> you
	add a segment like this in the section that checks for
	header files (search for "header files"):
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>AC_CHECK_HEADER(foo.h, AC_DEFINE(HAVE_FOO_H))
      </PRE
></TD
></TR
></TABLE
><P
>	If your operating system supports a header file with the
	same contents but a different name, say
	<TT
CLASS="FILENAME"
>bar.h</TT
>, add a check for that also.
      </P
><P
>	Now you can change
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#include &#60;foo.h&#62;
      </PRE
></TD
></TR
></TABLE
><P
>	to
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#ifdef HAVE_FOO_H
#include &#60;foo.h&#62;
#elif defined (HAVE_BAR_H)
#include &#60;bar.h&#62;
#endif
      </PRE
></TD
></TR
></TABLE
><P
>	If your system doesn't have a corresponding header file even
	though it has the library functions being used, you might
	have to add an <SPAN
CLASS="SYMBOL"
>#else</SPAN
> section to the
	conditional. Avoid this if you can.
      </P
><P
>	You will also need to add <SPAN
CLASS="SYMBOL"
>#undef HAVE_FOO_H</SPAN
>
	(etc.) to <TT
CLASS="FILENAME"
>include/config.h.in</TT
>
      </P
><P
>	Finish up with <B
CLASS="COMMAND"
>make configure</B
> and
	<B
CLASS="COMMAND"
>./configure</B
>.
      </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1546"
>MyOS doesn't have the <TT
CLASS="FUNCTION"
>bar</TT
> function!</A
></H3
><P
>	A typical example of this is the <TT
CLASS="FUNCTION"
>memmove</TT
>
	function. To solve this problem you would add
	<TT
CLASS="FUNCTION"
>memmove</TT
> to the list of functions that
	<B
CLASS="COMMAND"
>autoconf</B
> checks for. In
	<TT
CLASS="FILENAME"
>configure.in</TT
> you search for
	<TT
CLASS="FUNCTION"
>AC_CHECK_FUNCS</TT
> and add
	<TT
CLASS="FUNCTION"
>memmove</TT
>. (You will notice that someone
	already did this for this particular function.)
      </P
><P
>	Secondly, you will also need to add 
	<SPAN
CLASS="SYMBOL"
>#undef HAVE_BAR</SPAN
> to
	<TT
CLASS="FILENAME"
>include/config.h.in</TT
>
      </P
><P
>	The next step depends on the nature of the missing function.
      </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
>Case 1:</DT
><DD
><P
>	      It's easy to write a complete implementation of the
	      function. (<TT
CLASS="FUNCTION"
>memmove</TT
> belongs to
	      this case.)
	    </P
><P
>	      You add your implementation in
	      <TT
CLASS="FILENAME"
>misc/port.c</TT
> surrounded by
	      <SPAN
CLASS="SYMBOL"
>#ifndef HAVE_MEMMOVE</SPAN
> and
	      <SPAN
CLASS="SYMBOL"
>#endif</SPAN
>.
	    </P
><P
>	      You might have to add a prototype for your function.
	      If so, <TT
CLASS="FILENAME"
>include/miscemu.h</TT
> might be
	      the place. Don't forget to protect that definition by
	      <SPAN
CLASS="SYMBOL"
>#ifndef HAVE_MEMMOVE</SPAN
> and
	      <SPAN
CLASS="SYMBOL"
>#endif</SPAN
> also!
	    </P
></DD
><DT
>Case 2:</DT
><DD
><P
>	      A general implementation is hard, but Wine is only
	      using a special case.
	    </P
><P
>	      An example is the various <TT
CLASS="FUNCTION"
>wait</TT
>
	      calls used in <TT
CLASS="FUNCTION"
>SIGNAL_child</TT
> from
	      <TT
CLASS="FILENAME"
>loader/signal.c</TT
>. Here we have a
	      multi-branch case on features:
	    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#ifdef HAVE_THIS
...
#elif defined (HAVE_THAT)
...
#elif defined (HAVE_SOMETHING_ELSE)
...
#endif
	    </PRE
></TD
></TR
></TABLE
><P
>	      Note that this is very different from testing on
	      operating systems. If a new version of your operating
	      systems comes out and adds a new function, this code
	      will magically start using it.
	    </P
></DD
></DL
></DIV
><P
>	Finish up with <B
CLASS="COMMAND"
>make configure</B
> and
	<B
CLASS="COMMAND"
>./configure</B
>.
      </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="ADDING-LANGUAGES"
>Adding New Languages</A
></H2
><P
>      This file documents the necessary procedure for adding a new
      language to the list of languages that Wine can display system
      menus and forms in. Adding new translations is not hard as it
      requires no programming knowledge or special skills.
    </P
><P
>      Language dependent resources reside in files
      named <TT
CLASS="FILENAME"
>somefile_Xx.rc</TT
> or
      <TT
CLASS="FILENAME"
>Xx.rc</TT
>, where <TT
CLASS="LITERAL"
>Xx</TT
> 
      is your language  abbreviation (look for it in
      <TT
CLASS="FILENAME"
>include/winnls.h</TT
>). These are included
      in a master file named <TT
CLASS="FILENAME"
>somefile.rc</TT
> or
      <TT
CLASS="FILENAME"
>rsrc.rc</TT
>, located in the same
      directory as the language files.
    </P
><P
>      To add a new language to one of these resources you
      need to make a copy of the English resource (located
      in the <TT
CLASS="FILENAME"
>somefile_En.rc</TT
> file) over to
      your <TT
CLASS="FILENAME"
>somefile_Xx.rc</TT
> file, include this
      file in the master <TT
CLASS="FILENAME"
>somefile.rc</TT
> file,
      and edit the new file to translate the English text.
      You may also need to rearrange some of the controls
      to better fit the newly translated strings. Test your changes 
      to make sure they properly layout on the screen.
    </P
><P
>      In menus, the character "&#38;" means that the next
      character will be highlighted and that pressing that
      letter will select the item. You should place these
      "&#38;" characters suitably for your language, not just
      copy the positions from English. In particular,
      items within one menu should have different highlighted
      letters.
    </P
><P
>      To get a list of the files that need translating,
      run the following command in the root of your Wine tree:
      <B
CLASS="COMMAND"
>find -name "*En.rc"</B
>.
    </P
><P
>      When adding a new language, also make sure the parameters 
      defined in  <TT
CLASS="FILENAME"
>./dlls/kernel/nls/*.nls</TT
> 
      fit your local habits and language.
    </P
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="TESTING"
></A
>Writing Conformance tests</H1
><DIV
CLASS="SECT1"
><H2
CLASS="SECT1"
><A
NAME="TESTING-INTRO"
>Introduction</A
></H2
><P
>	The Windows API follows no standard, it is itself a de facto standard,
	and deviations from that standard, even small ones, often cause
	applications to crash or misbehave in some way.
      </P
><P
>	The question becomes, "How do we ensure compliance with that standard?"
	The answer is, "By using the API documentation available to us and
	backing that up with conformance tests." Furthermore, a conformance
	test suite is the most accurate (if not necessarily the most complete)
	form of API documentation and can be used to supplement the Windows
	API documentation.
      </P
><P
>        Writing a conformance test suite for more than 10000 APIs is no small
        undertaking. Fortunately it can prove very useful to the development
        of Wine way before it is complete.
        <P
></P
><UL
><LI
><P
>              The conformance test suite must run on Windows. This is
              necessary to provide a reasonable way to verify its accuracy.
              Furthermore the tests must pass successfully on all Windows
              platforms (tests not relevant to a given platform should be
              skipped).
            </P
><P
>              A consequence of this is that the test suite will provide a
              great way to detect variations in the API between different
              Windows versions. For instance, this can provide insights
              into the differences between the, often undocumented, Win9x and
              NT Windows families.
            </P
><P
>              However, one must remember that the goal of Wine is to run
              Windows applications on Linux, not to be a clone of any specific
              Windows version. So such variations must only be tested for when
              relevant to that goal.
            </P
></LI
><LI
><P
>              Writing conformance tests is also an easy way to discover
              bugs in Wine. Of course, before fixing the bugs discovered in
              this way, one must first make sure that the new tests do pass
              successfully on at least one Windows 9x and one Windows NT
              version.
            </P
><P
>              Bugs discovered this way should also be easier to fix. Unlike
              some mysterious application crashes, when a conformance test
              fails, the expected behavior and APIs tested for are known thus
              greatly simplifying the diagnosis.
            </P
></LI
><LI
><P
>              To detect regressions. Simply running the test suite regularly
              in Wine turns it into a great tool to detect regressions.
              When a test fails, one immediately knows what was the expected
              behavior and which APIs are involved. Thus regressions caught
              this way should be detected earlier, because it is easy to run
              all tests on a regular basis, and be easier to fix because of the
              reduced diagnosis work.
            </P
></LI
><LI
><P
>              Tests written in advance of the Wine development (possibly even
              by non Wine developers) can also simplify the work of the
              future implementer by making it easier for him to check the
              correctness of his code.
            </P
></LI
><LI
><P
>              Conformance tests will also come in handy when testing Wine on
              new (or not as widely used) architectures such as FreeBSD,
              Solaris x86 or even non-x86 systems. Even when the port does
              not involve any significant change in the thread management,
              exception handling or other low-level aspects of Wine, new
              architectures can expose subtle bugs that can be hard to
              diagnose when debugging regular (complex) applications.
            </P
></LI
></UL
>
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="TESTING-WHAT"
>What to test for?</A
></H2
><P
>        The first thing to test for is the documented behavior of APIs
        and such as CreateFile. For instance one can create a file using a
        long pathname, check that the behavior is correct when the file
        already exists, try to open the file using the corresponding short
        pathname, convert the filename to Unicode and try to open it using
        CreateFileW, and all other things which are documented and that
        applications rely on.
      </P
><P
>        While the testing framework is not specifically geared towards this
        type of tests, it is also possible to test the behavior of Windows
        messages. To do so, create a window, preferably a hidden one so that
        it does not steal the focus when running the tests, and send messages
        to that window or to controls in that window. Then, in the message
        procedure, check that you receive the expected messages and with the
        correct parameters.
      </P
><P
>        For instance you could create an edit control and use WM_SETTEXT to
        set its contents, possibly check length restrictions, and verify the
        results using WM_GETTEXT. Similarly one could create a listbox and
        check the effect of LB_DELETESTRING on the list's number of items,
        selected items list, highlighted item, etc. For concrete examples,
        see <TT
CLASS="FILENAME"
>dlls/user/tests/win.c</TT
> and the related tests.
      </P
><P
>        However, undocumented behavior should not be tested for unless there
        is an application that relies on this behavior, and in that case the
        test should mention that application, or unless one can strongly
        expect applications to rely on this behavior, typically APIs that
        return the required buffer size when the buffer pointer is NULL.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="TESTING-WINE"
>Running the tests in Wine</A
></H2
><P
>        The simplest way to run the tests in Wine is to type 'make test' in
        the Wine sources top level directory. This will run all the Wine
        conformance tests.
      </P
><P
>        The tests for a specific Wine library are located in a 'tests'
        directory in that library's directory. Each test is contained in a
        file (e.g. <TT
CLASS="FILENAME"
>dlls/kernel/tests/thread.c</TT
>). Each
        file itself contains many checks concerning one or more related APIs.
      </P
><P
>        So to run all the tests related to a given Wine library, go to the
        corresponding 'tests' directory and type 'make test'. This will
        compile the tests, run them, and create an '<TT
CLASS="REPLACEABLE"
><I
>xxx</I
></TT
>.ok'
        file for each test that passes successfully. And if you only want to
        run the tests contained in the <TT
CLASS="FILENAME"
>thread.c</TT
> file of the
        kernel library, you would do:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
><TT
CLASS="PROMPT"
>$ </TT
>cd dlls/kernel/tests
<TT
CLASS="PROMPT"
>$ </TT
>make thread.ok</PRE
></TD
></TR
></TABLE
>
      </P
><P
>        Note that if the test has already been run and is up to date (i.e. if
        neither the kernel library nor the <TT
CLASS="FILENAME"
>thread.c</TT
> file has
        changed since the <TT
CLASS="FILENAME"
>thread.ok</TT
> file was created), then make
        will say so. To force the test to be re-run, delete the
        <TT
CLASS="FILENAME"
>thread.ok</TT
> file, and run the make command again.
      </P
><P
>        You can also run tests manually using a command similar to the
        following:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
><TT
CLASS="PROMPT"
>$ </TT
>../../../tools/runtest -q -M kernel32.dll -p kernel32_test.exe.so thread.c
<TT
CLASS="PROMPT"
>$ </TT
>../../../tools/runtest -P wine -p kernel32_test.exe.so thread.c
thread.c: 86 tests executed, 5 marked as todo, 0 failures.</PRE
></TD
></TR
></TABLE
>
        The '-P wine' option defines the platform that is currently being
        tested and is used in conjunction with the 'todo' statements (see
        below). Remove the '-q' option if you want the testing framework
        to report statistics about the number of successful and failed tests.
        Run <B
CLASS="COMMAND"
>runtest -h</B
> for more details.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="CROSS-COMPILING-TESTS"
>Cross-compiling the tests with MinGW</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN1656"
>Setup of the MinGW cross-compiling environment</A
></H3
><P
>          Here are some instructions to setup MinGW on different Linux 
          distributions and *BSD.
        </P
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN1659"
>Debian GNU/Linux</A
></H4
><P
>            On Debian do <B
CLASS="COMMAND"
>apt-get install mingw32</B
>.
          </P
><P
>            The standard MinGW libraries will probably be incomplete, causing
            'undefined symbol' errors. So get the latest
            <A
HREF="http://mirzam.it.vu.nl/mingw/"
TARGET="_top"
>mingw-w32api RPM</A
>
            and use <B
CLASS="COMMAND"
>alien</B
> to either convert it to a .tar.gz file
            from which to extract just the relevant files, or to convert it
            to a Debian package that you will install.
          </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN1666"
>Red Hat Linux like rpm systems</A
></H4
><P
>            This includes Fedora Core, Red Hat Enterprise Linux, Mandrake,
            most probably SuSE Linux too, etc. But this list isn't exhaustive;
            the following steps should probably work on any rpm based system.
          </P
><P
>            Download and install the latest rpm's from 
            <A
HREF="http://mirzam.it.vu.nl/mingw/"
TARGET="_top"
>MinGW RPM packages</A
>.
            Alternatively you can follow the instructions on that page and
            build your own packages from the source rpm's listed there as well.
          </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN1671"
>*BSD</A
></H4
><P
>            The *BSD systems have in their ports collection a port for the
            MinGW cross-compiling environment. Please see the documentation
            of your system about how to build and install a port.
          </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1674"
>Compiling the tests</A
></H3
><P
>        Having the cross-compiling environment set up the generation of the
        Windows executables is easy by using the Wine build system.
      </P
><P
>        If you had already run <B
CLASS="COMMAND"
>configure</B
>, then delete
        <TT
CLASS="FILENAME"
>config.cache</TT
> and re-run <B
CLASS="COMMAND"
>configure</B
>.
        You can then run <B
CLASS="COMMAND"
>make crosstest</B
>. To sum up:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
><TT
CLASS="PROMPT"
>$ </TT
><KBD
CLASS="USERINPUT"
>rm config.cache</KBD
>
<TT
CLASS="PROMPT"
>$ </TT
><KBD
CLASS="USERINPUT"
>./configure</KBD
>
<TT
CLASS="PROMPT"
>$ </TT
><KBD
CLASS="USERINPUT"
>make crosstest</KBD
></PRE
></TD
></TR
></TABLE
>
      </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="TESTING-WINDOWS"
>Building and running the tests on Windows</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN1691"
>Using pre-compiled binaries</A
></H3
><P
>          The simplest solution is to download the
          <A
HREF="http://www.astro.gla.ac.uk/users/paulm/WRT/CrossBuilt/winetest-latest.exe"
TARGET="_top"
>latest
          version of winetest</A
>. This executable contains all the Wine
          conformance tests, runs them and reports the results.
        </P
><P
>          You can also get the older versions from
          <A
HREF="http://www.astro.gla.ac.uk/users/paulm/WRT/CrossBuilt/"
TARGET="_top"
>Paul
          Millar's website</A
>.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1697"
>With Visual C++</A
></H3
><P
></P
><UL
><LI
><P
>            If you are using Visual Studio 6, make sure you have the
            "processor pack" from
            <A
HREF="http://msdn.microsoft.com/vstudio/downloads/tools/ppack/default.aspx"
TARGET="_top"
>http://msdn.microsoft.com/vstudio/downloads/tools/ppack/default.aspx</A
>.
            The processor pack fixes <I
CLASS="EMPHASIS"
>"error C2520: conversion from
            unsigned __int64 to double not implemented, use signed __int64"</I
>.
	    However note that the "processor pack" is incompatible with
            Visual Studio 6.0 Standard Edition, and with the Visual Studio 6
            Service Pack 6. If you are using Visual Studio 7 or greater you
            do not need the processor pack. In either case it is recommended
            to the most recent compatible Visual Studio
            <A
HREF="http://msdn.microsoft.com/vstudio/downloads/updates/sp/"
TARGET="_top"
>service pack</A
>.  
            If are using Visual Studio Express and you need specific libraries like 
            <TT
CLASS="FILENAME"
>ntdll.lib</TT
> that don't ship with Visual Studio Express, you will need the 
            <A
HREF="http://www.microsoft.com/whdc/devtools/ddk/default.mspx"
TARGET="_top"
>Windows Driver Development Kit (DDK)</A
> 
            which is part of the 
            <A
HREF="http://www.microsoft.com/whdc/resources/downloads.mspx"
TARGET="_top"
>Windows Developer Kit</A
>.
          </P
></LI
><LI
><P
>            get the Wine sources
          </P
></LI
><LI
><P
>            Run msvcmaker to generate Visual C++ project files for the tests.
            'msvcmaker' is a perl script so you may be able to run it on
            Windows.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
><TT
CLASS="PROMPT"
>$ </TT
>./tools/winapi/msvcmaker --no-wine</PRE
></TD
></TR
></TABLE
>
          </P
></LI
><LI
><P
>            If the previous steps were done on your Linux development
            machine, make the Wine sources accessible to the Windows machine
            on which you are going to compile them. Typically you would do
            this using Samba but copying them altogether would work too.
          </P
></LI
><LI
><P
>            On the Windows machine, open the <TT
CLASS="FILENAME"
>winetest.dsw</TT
>
            workspace. This will load each test's project. For each test there
            are two configurations: one compiles the test with the Wine
            headers, and the other uses the Microsoft headers.
          </P
></LI
><LI
><P
>            If you choose the "Win32 MSVC Headers" configuration, most of the
            tests will not compile with the regular Visual Studio headers. So
            to use this configuration, download and install a recent
            <A
HREF="http://www.microsoft.com/msdownload/platformsdk/sdkupdate/"
TARGET="_top"
>Platform SDK</A
>
            as well as the latest <A
HREF="http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp"
TARGET="_top"
>DirectX SDK</A
>.
            Then, <A
HREF="http://msdn.microsoft.com/library/default.asp?url=/library/EN-US/sdkintro/sdkintro/installing_the_platform_sdk_with_visual_studio.asp"
TARGET="_top"
>configure Visual Studio</A
>
            to use these SDK's headers and libraries. Alternately you could go
            to the <B
CLASS="GUIMENU"
>Project</B
>+<B
CLASS="GUIMENU"
>Settings...</B
>
            menu and modify the settings appropriately, but you would then
            have to redo this whenever you rerun msvcmaker.
          </P
></LI
><LI
><P
>            Open the <B
CLASS="GUIMENU"
>Build</B
>+<B
CLASS="GUIMENU"
>Batch
            build...</B
> menu and select the tests and build configurations
            you want to build. Then click on <B
CLASS="GUIBUTTON"
>Build</B
>.
          </P
></LI
><LI
><P
>            To run a specific test from Visual C++, go to
            <B
CLASS="GUIMENU"
>Project</B
>+<B
CLASS="GUIMENU"
>Settings...</B
>. There
            select that test's project and build configuration and go to the
            <TT
CLASS="GUILABEL"
><B
>Debug</B
></TT
> tab. There type the name of the specific test
            to run (e.g. 'thread') in the <TT
CLASS="GUILABEL"
><B
>Program arguments</B
></TT
>
            field. Validate your change by clicking on <B
CLASS="GUIBUTTON"
>Ok</B
> and
            start the test by clicking the red exclamation mark (or hitting
            'F5' or any other usual method).
          </P
></LI
><LI
><P
>            You can also run the tests from the command line. You will find
            them in either <TT
CLASS="FILENAME"
>Output\Win32_Wine_Headers</TT
> or
            <TT
CLASS="FILENAME"
>Output\Win32_MSVC_Headers</TT
> depending on the build
            method. So to run the kernel 'path' tests you would do:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
><TT
CLASS="PROMPT"
>C:\&#62;</TT
>cd dlls\kernel\tests\Output\Win32_MSVC_Headers
<TT
CLASS="PROMPT"
>C:\wine\dlls\kernel\tests\Output\Win32_MSVC_Headers&#62;</TT
> kernel32_test path</PRE
></TD
></TR
></TABLE
>
          </P
></LI
></UL
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1748"
>With MinGW</A
></H3
><P
>         Wine's build system already has support for building tests with a MinGW
         cross-compiler. See the section above called 'Setup of the MinGW 
         cross-compiling environment' for instructions on how to set things up.
         When you have a MinGW environment installed all you need to do is rerun
         configure and it should detect the MinGW compiler and tools. Then run 
         'make crosstest' to start building the tests.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN1751"
>Standalone, using the Microsoft C++ Toolkit</A
></H3
><P
>         Sometimes it's nice to be able to build a new unit test
         on Windows without Wine, and without buying Microsoft Visual C++.
         Here's the simplest way to do that on a Windows system:
        <P
></P
><UL
><LI
><P
>           Download and install the free-as-in-beer 
           <A
HREF="http://msdn.microsoft.com/visualc/vctoolkit2003"
TARGET="_top"
>Microsoft C++ Toolkit</A
>
           and the 
           <A
HREF="http://www.microsoft.com/msdownload/platformsdk/sdkupdate"
TARGET="_top"
>Microsoft Platform SDK</A
>.
          </P
></LI
><LI
><P
>           Make a directory <TT
CLASS="FILENAME"
>wine</TT
> underneath your work directory,
           and copy the file <TT
CLASS="FILENAME"
>wine/test.h</TT
> from the Wine source tree there.
           (You can download this file from the latest revision at
            <A
HREF="http://source.winehq.org/git/?p=wine.git;a=blob_plain;f=include/wine/test.h"
TARGET="_top"
>                        http://source.winehq.org/git/?p=wine.git;a=blob_plain;f=include/wine/test.h</A
>).
          </P
></LI
><LI
><P
>           Copy some existing test from the Wine source tree, or
           create your test program (say, <TT
CLASS="FILENAME"
>mytest.c</TT
>) using Notepad,
           being sure to begin it with <TT
CLASS="FUNCTION"
>#include &#60;wine/test.h&#62;</TT
>
           following the usual Wine test style.
          </P
></LI
><LI
><P
>           Finally, in a command prompt window, compile the test with the command
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
><TT
CLASS="PROMPT"
>C:\your\work\dir&#62;</TT
>cl -I. -DSTANDALONE -D_X86_ mytest.c</PRE
></TD
></TR
></TABLE
>
          </P
></LI
><LI
><P
>           Once that's working, try running the program under Wine without
           recompiling it.   See?  No Wine source required at all,
           save for that one header, <TT
CLASS="FILENAME"
>wine/test.h</TT
>.
          </P
></LI
><LI
><P
>           If you want to use the Microsoft C++ Toolkit under Wine,
           install it under Windows, then copy it to your fake C drive;
           it'll work fine there.  See <A
HREF="http://kegel.com/wine/cl-howto.html"
TARGET="_top"
>CL Howto</A
>
           for some tips on making it easy to use from the Linux commandline.
          </P
></LI
></UL
>
        </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="TESTING-TEST"
>Inside a test</A
></H2
><P
>        When writing new checks you can either modify an existing test file or
        add a new one. If your tests are related to the tests performed by an
        existing file, then add them to that file. Otherwise create a new .c
        file in the tests directory and add that file to the
        <CODE
CLASS="VARNAME"
>CTESTS</CODE
> variable in <TT
CLASS="FILENAME"
>Makefile.in</TT
>.
      </P
><P
>        A new test file will look something like the following:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>#include &#60;wine/test.h&#62;
#include &#60;winbase.h&#62;

/* Maybe auxiliary functions and definitions here */

START_TEST(paths)
{
   /* Write your checks there or put them in functions you will call from
    * there
    */
}</PRE
></TD
></TR
></TABLE
>
      </P
><P
>        The test's entry point is the START_TEST section. This is where
        execution will start. You can put all your tests in that section but
        it may be better to split related checks in functions you will call
        from the START_TEST section. The parameter to START_TEST must match
        the name of the C file. So in the above example the C file would be
        called <TT
CLASS="FILENAME"
>paths.c</TT
>.
      </P
><P
>        Tests should start by including the <TT
CLASS="FILENAME"
>wine/test.h</TT
> header.
        This header will provide you access to all the testing framework
        functions. You can then include the windows header you need, but make
        sure to not include any Unix or Wine specific header: tests must
        compile on Windows.
      </P
><P
>        You can use <TT
CLASS="FUNCTION"
>trace</TT
> to print informational messages. Note
        that these messages will only be printed if 'runtest -v' is being used.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>  trace("testing GlobalAddAtomA\n");
  trace("foo=%d\n",foo);</PRE
></TD
></TR
></TABLE
>
      </P
><P
>        Then just call functions and use <TT
CLASS="FUNCTION"
>ok</TT
> to make sure that
        they behaved as expected:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>  ATOM atom = GlobalAddAtomA( "foobar" );
  ok( GlobalFindAtomA( "foobar" ) == atom, "could not find atom foobar\n" );
  ok( GlobalFindAtomA( "FOOBAR" ) == atom, "could not find atom FOOBAR\n" );</PRE
></TD
></TR
></TABLE
>
        The first parameter of <TT
CLASS="FUNCTION"
>ok</TT
> is an expression which must
        evaluate to true if the test was successful. The next parameter is a
        printf-compatible format string which is displayed in case the test
        failed, and the following optional parameters depend on the format
        string.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="TESTING-ERROR-MESSAGES"
>Writing good error messages</A
></H2
><P
>        The message that is printed when a test fails is
        <I
CLASS="EMPHASIS"
>extremely</I
> important.
      </P
><P
>        Someone will take your test, run it on a Windows platform that
        you don't have access to, and discover that it fails. They will then
        post an email with the output of the test, and in particular your
        error message. Someone, maybe you, will then have to figure out from
        this error message why the test failed.
      </P
><P
>        If the error message contains all the relevant information that will
        be easy. If not, then it will require modifying the test, finding
        someone to compile it on Windows, sending the modified version to the
        original tester and waiting for his reply. In other words, it will
        be long and painful.
      </P
><P
>        So how do you write a good error message? Let's start with an example
        of a bad error message:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    ok(GetThreadPriorityBoost(curthread,&#38;disabled)!=0,
       "GetThreadPriorityBoost Failed\n");</PRE
></TD
></TR
></TABLE
>
        This will yield:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>thread.c:123: Test failed: GetThreadPriorityBoost Failed</PRE
></TD
></TR
></TABLE
>
      </P
><P
>        Did you notice how the error message provides no information about
        why the test failed? We already know from the line number exactly
        which test failed. In fact the error message gives strictly no
        information that cannot already be obtained by reading the code. In
        other words it provides no more information than an empty string!
      </P
><P
>        Let's look at how to rewrite it:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    BOOL rc;
...
    rc=GetThreadPriorityBoost(curthread,&#38;disabled);
    ok(rc!=0 &#38;&#38; disabled==0,"rc=%d error=%ld disabled=%d\n",
       rc,GetLastError(),disabled);</PRE
></TD
></TR
></TABLE
>
        This will yield:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>thread.c:123: Test failed: rc=0 error=120 disabled=0</PRE
></TD
></TR
></TABLE
>
      </P
><P
>        When receiving such a message, one would check the source, see that
        it's a call to GetThreadPriorityBoost, that the test failed not
        because the API returned the wrong value, but because it returned an
        error code. Furthermore we see that GetLastError() returned 120 which
        winerror.h defines as ERROR_CALL_NOT_IMPLEMENTED. So the source of
        the problem is obvious: this Windows platform (here Windows 98) does
        not support this API and thus the test must be modified to detect
        such a condition and skip the test.
      </P
><P
>        So a good error message should provide all the information which
        cannot be obtained by reading the source, typically the function
        return value, error codes, and any function output parameter. Even if
        more information is needed to fully understand a problem,
        systematically providing the above is easy and will help cut down the
        number of iterations required to get to a resolution.
      </P
><P
>        It may also be a good idea to dump items that may be hard to retrieve
        from the source, like the expected value in a test if it is the
        result of an earlier computation, or comes from a large array of test
        values (e.g. index 112 of _pTestStrA in vartest.c). In that respect,
        for some tests you may want to define a macro such as the following:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>#define eq(received, expected, label, type) \
        ok((received) == (expected), "%s: got " type " instead of " type "\n", (label),(received),(expected))

...

    eq( b, curr_val, "SPI_{GET,SET}BEEP", "%d" );</PRE
></TD
></TR
></TABLE
>
       </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="TESTING-PLATFORMS"
>Handling platform issues</A
></H2
><P
>        Some checks may be written before they pass successfully in Wine.
        Without some mechanism, such checks would potentially generate
        hundred of known failures for months each time the tests are being run.
        This would make it hard to detect new failures caused by a regression.
        or to detect that a patch fixed a long standing issue.
      </P
><P
>        Thus the Wine testing framework has the concept of platforms and
        groups of checks can be declared as expected to fail on some of them.
        In the most common case, one would declare a group of tests as
        expected to fail in Wine. To do so, use the following construct:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>todo_wine {
    SetLastError( 0xdeadbeef );
    ok( GlobalAddAtomA(0) == 0 &#38;&#38; GetLastError() == 0xdeadbeef, "failed to add atom 0\n" );
}</PRE
></TD
></TR
></TABLE
>
        On Windows the above check would be performed normally, but on Wine it
        would be expected to fail, and not cause the failure of the whole
        test. However. If that check were to succeed in Wine, it would
        cause the test to fail, thus making it easy to detect when something
        has changed that fixes a bug. Also note that todo checks are accounted
        separately from regular checks so that the testing statistics remain
        meaningful. Finally, note that todo sections can be nested so that if
        a test only fails on the cygwin and reactos platforms, one would
        write:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>todo("cygwin") {
    todo("reactos") {
        ...
    }
}</PRE
></TD
></TR
></TABLE
>
        
        But specific platforms should not be nested inside a todo_wine section
        since that would be redundant.
      </P
><P
>        When writing tests you will also encounter differences between Windows
        9x and Windows NT platforms. Such differences should be treated
        differently from the platform issues mentioned above. In particular
        you should remember that the goal of Wine is not to be a clone of any
        specific Windows version but to run Windows applications on Unix.
      </P
><P
>        So, if an API returns a different error code on Windows 9x and
        Windows NT, your check should just verify that Wine returns one or
        the other:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>ok ( GetLastError() == WIN9X_ERROR || GetLastError() == NT_ERROR, ...);</PRE
></TD
></TR
></TABLE
>
      </P
><P
>        If an API is only present on some Windows platforms, then use
        LoadLibrary and GetProcAddress to check if it is implemented and
        invoke it. Remember, tests must run on all Windows platforms.
        Similarly, conformance tests should nor try to correlate the Windows
        version returned by GetVersion with whether given APIs are
        implemented or not. Again, the goal of Wine is to run Windows
        applications (which do not do such checks), and not be a clone of a
        specific Windows version.
      </P
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="DOCUMENTATION"
></A
>Documenting Wine</H1
><P
>      This chapter describes how you can help improve Wine's documentation.
    </P
><P
>      Like most large scale volunteer projects, Wine is strongest in areas that are rewarding
      for its volunteers to work in. The majority of contributors send code patches either
      fixing bugs, adding new functionality or otherwise improving the software components of
      the distribution. A lesser number contribute in other ways, such as reporting bugs and
      regressions, creating tests, providing organizational assistance, or helping to document
      Wine.
    </P
><P
>      Documentation is important for many reasons, and is often the key to the end user having
      a successful experience in installing, setting up and using software. Because Wine is a
      complicated, evolving entity, providing quality up to date documentation is vital to
      encourage more people to persevere with using and contributing to the project.
      The following sections describe in detail how to go about adding to or updating Wine's
      existing documentation.
    </P
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DOC-OVERVIEW"
>An Overview Of Wine Documentation</A
></H2
><P
>        The Wine source code tree comes with a large amount of documentation in the
        <TT
CLASS="FILENAME"
>documentation/</TT
> subdirectory. This used to be a collection
        of text files culled from various places such as the Wine Weekly News and the wine-devel
        mailing list, but was reorganized some time ago into a number of books, each of which is
        marked up using SGML. You are reading one of these books (the
        <I
CLASS="EMPHASIS"
>Wine Developer's Guide</I
>) right now.
      </P
><P
>        Since being reorganized, the books have been updated and extended regularly. In their
        current state they provide a good framework which over time can be expanded and kept
        up to date. This means that most of the time when further documentation is added, it is
        a simple matter of updating the content of an already existing file. The books
        available at the time of writing are:

          <P
></P
><UL
><LI
><P
>          The <I
CLASS="EMPHASIS"
>Wine User Guide</I
>. This book contains information for end users
          on installing, configuring and running Wine.
          </P
></LI
><LI
><P
>          The <I
CLASS="EMPHASIS"
>Wine Developer's Guide</I
>. This book contains information and
          guidelines for developers and contributors to the Wine project.
          </P
></LI
><LI
><P
>          The <I
CLASS="EMPHASIS"
>Winelib User's Guide</I
>. This book contains information for
          developers using Winelib to port Win32 applications to Unix.
          </P
></LI
><LI
><P
>          The <I
CLASS="EMPHASIS"
>Wine Packager's Guide</I
>. This book contains
          information for anyone who will be distributing Wine to end users
          in a prepackaged format. It is also the exception to the rule as
          it has intentionally been kept in text format.
          </P
></LI
><LI
><P
>          The <I
CLASS="EMPHASIS"
>Wine FAQ</I
>. This book contains frequently asked questions
          about Wine with their answers.
          </P
></LI
></UL
>
      </P
><P
>        To obtain a copy of the Wine documentation from Sourceforge refer to 
        <A
HREF="http://www.winehq.com/site/cvs#docs"
TARGET="_top"
>the Wine CVS page</A
> for instructions.
      </P
><P
>        Another source of documentation is the <I
CLASS="EMPHASIS"
>Wine API Guide</I
>. This is
        generated information taken from special comments placed in the Wine source code.
        When you update or add new API calls to Wine you should consider documenting them so
        that developers can determine what the API does and how it should be used.
      </P
><P
>        The next sections describe how to create Wine API documentation and how to work with
        SGML so you can add to the existing books.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="API-DOCS"
>Writing Wine API Documentation</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="API-DOCS-INTRO"
>Introduction to API Documentation</A
></H3
><P
>        Wine includes a large amount of documentation on the API functions
        it implements. There are several reasons to want to document the Win32
        API:
          <P
></P
><UL
><LI
><P
>          To allow Wine developers to know what each function should do, should
          they need to update or fix it.
          </P
></LI
><LI
><P
>          To allow Winelib users to understand the functions that are available
          to their applications.
          </P
></LI
><LI
><P
>          To provide an alternative source of free documentation on the Win32 API.
          </P
></LI
><LI
><P
>          To provide more accurate documentation where the existing documentation
          is accidentally or deliberately vague or misleading.
          </P
></LI
></UL
>
      </P
><P
>        To this end, a semi formalized way of producing documentation from the Wine
        source code has evolved. Since the primary users of API documentation are Wine
        developers themselves, documentation is usually inserted into the source code
        in the form of comments and notes. Good things to include in the documentation
        of a function include:
          <P
></P
><UL
><LI
><P
>          The purpose of the function.
          </P
></LI
><LI
><P
>          The parameters of the function and their purpose.
          </P
></LI
><LI
><P
>          The return value of the function, in success as well as failure cases.
          </P
></LI
><LI
><P
>          Additional notes such as interaction with other parts of the system, differences
          between Wine's implementation and Win32s, errors in MSDN documentation,
          undocumented cases and bugs that Wine corrects or is compatible with.
          </P
></LI
></UL
>
      </P
><P
>        Good documentation helps developers be aware of the effects of making changes. It
        also allows good tests to be written which cover all of the documented cases.
      </P
><P
>        Note that you do not need to be a programmer to update the documentation in Wine.
        If you would like to contribute to the project, patches that improve the API
        documentation are welcome. The following describes how to format any documentation
        that you write so that the Wine documentation generator can extract it and make it
        available to other developers and users.
      </P
><P
>        In general, if you did not write the function in question, you should be wary of
        adding comments to other peoples code. It is quite possible you may misunderstand
        or misrepresent what the original author intended! Adding API documentation on
        the other hand can be done by anybody, since in most cases there is plenty of
        information about what a function is supposed to do (if it isn't obvious)
        available in books and articles on the internet.
      </P
><P
>        A final warning concerns copyright and must be noted. If you read MSDN or any
        publication in order to find out what an API call does, you must be aware that
        the text you are reading is copyrighted and in most cases cannot legally be
        reproduced without the authors permission. If you copy verbatim any information
        from such sources and submit it for inclusion into Wine, you open yourself up
        to potential legal liability. You must ensure that anything you submit is
        your own work, although it can be based on your understanding gleaned from
        reading other peoples work.
      </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="API-DOCS-BASICS"
>Basic API Documentation</A
></H3
><P
>        The general form of an API comment in Wine is a block comment immediately before a
        function is implemented in the source code. General comments within a function body or
        at the top of an implementation file are ignored by the API documentation generator.
        Such comments are for the benefit of developers only, for example to explain what the
        source code is doing or to describe something that may not be obvious to the person
        reading the source code.
      </P
><P
>        The following text uses the function <I
CLASS="EMPHASIS"
>PathRelativePathToA()</I
> from
        <TT
CLASS="FILENAME"
>SHLWAPI.DLL</TT
> as an example. You can find this function in the Wine
        source code tree in the file <TT
CLASS="FILENAME"
>dlls/shlwapi/path.c</TT
>.
      </P
><P
>        The first line of the comment gives the name of the function, the DLL that the
        function is exported from, and its export ordinal number. This is the simplest
        (and most common type of) comment:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/*************************************************************************
 * PathRelativePathToW   [SHLWAPI.@]
 */
      </PRE
></TD
></TR
></TABLE
><P
>        The functions name and the DLL name are obvious. The ordinal number takes one of
        two forms: Either <B
CLASS="COMMAND"
>@</B
> as in the above, or a number if the export
        is exported by ordinal. You can see which to use by looking at the DLL's
        <TT
CLASS="FILENAME"
>.spec</TT
> file. If the line on which the function is listed begins
        with a number, use it, otherwise use the <B
CLASS="COMMAND"
>@</B
> symbol, which indicates
        that this function is imported only by name.
      </P
><P
>        Note also that round or square brackets can be used, and whitespace between the name
        and the DLL/ordinal is free form. Thus the following is equally valid:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/*************************************************************************
 *		PathRelativePathToW	(SHLWAPI.@)
 */
      </PRE
></TD
></TR
></TABLE
><P
>        This basic comment will not get processed into documentation, since it
        contains no information. In order to produce documentation for the function,
        We must add some of the information listed above.
      </P
><P
>        First we add a description of the function. This can be as long as you like, but
        typically contains only a brief description of what the function is meant to do
        in general terms. It is free form text:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/*************************************************************************
 * PathRelativePathToW   [SHLWAPI.@]
 *
 * Create a relative path from one path to another.
 */
      </PRE
></TD
></TR
></TABLE
><P
>        To be truly useful however we must document the parameters to the function.
        There are two methods for doing this: In the comment, or in the function
        prototype.
      </P
><P
>        Parameters documented in the comment should be formatted as follows:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/*************************************************************************
 * PathRelativePathToW   [SHLWAPI.@]
 *
 * Create a relative path from one path to another.
 *
 * PARAMS
 *  lpszPath   [O] Destination for relative path
 *  lpszFrom   [I] Source path
 *  dwAttrFrom [I] File attribute of source path
 *  lpszTo     [I] Destination path
 *  dwAttrTo   [I] File attributes of destination path
 *
 */
      </PRE
></TD
></TR
></TABLE
><P
>        The parameters section starts with <B
CLASS="COMMAND"
>PARAMS</B
> on its own line.
        Each parameter is listed in the order they appear in the functions prototype,
        first with the parameters name, followed by its input/output status, followed
        by a free form text description of the comment.
      </P
><P
>        The input/output status tells the programmer whether the value will be modified
        by the function (an output parameter), or only read (an input parameter). The
        status must be enclosed in square brackets to be recognized, otherwise, or if it
        is absent, anything following the parameter name is treated as the parameter
        description. This field is case insensitive and can be any of the following:
        <B
CLASS="COMMAND"
>[I]</B
>, <B
CLASS="COMMAND"
>[In]</B
>, <B
CLASS="COMMAND"
>[O]</B
>,
        <B
CLASS="COMMAND"
>[Out]</B
>, <B
CLASS="COMMAND"
>[I/O]</B
>, <B
CLASS="COMMAND"
>[In/Out]</B
>.
      </P
><P
>        Following the description and parameters come a number of optional sections, all
        in the same format. A section is defined as the section name, which is an all upper
        case section name on its own line, followed by free form text. You can create any
        sections you like, however for consistency it is recommended you use the following
        section names:
          <P
></P
><OL
TYPE="1"
><LI
><P
>          <B
CLASS="COMMAND"
>NOTES</B
>. Anything that needs to be noted about the function
          such as special cases and the effects of input arguments.
          </P
></LI
><LI
><P
>          <B
CLASS="COMMAND"
>BUGS</B
>. Any bugs in the function that exist 'by design', i.e.
          those that will not be fixed or exist for compatibility with Windows.
          </P
></LI
><LI
><P
>          <B
CLASS="COMMAND"
>TODO</B
>. Any unhandled cases or missing functionality in the Wine
          implementation of the function.
          </P
></LI
><LI
><P
>          <B
CLASS="COMMAND"
>FIXME</B
>. Things that should be updated or addressed in the implementation
          of the function at some future date (perhaps dependent on other parts of Wine). Note
          that if this information is only relevant to Wine developers then it should probably
          be placed in the relevant code section instead.
          </P
></LI
></OL
>
      </P
><P
>        Following or before the optional sections comes the <B
CLASS="COMMAND"
>RETURNS</B
> section
        which describes the return value of the function. This is free form text but should include
        what is returned on success as well as possible error return codes. Note that this
        section must be present for documentation to be generated for your comment.
      </P
><P
>        Our final documentation looks like the following:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/*************************************************************************
 * PathRelativePathToW   [SHLWAPI.@]
 *
 * Create a relative path from one path to another.
 *
 * PARAMS
 *  lpszPath   [O] Destination for relative path
 *  lpszFrom   [I] Source path
 *  dwAttrFrom [I] File attribute of source path
 *  lpszTo     [I] Destination path
 *  dwAttrTo   [I] File attributes of destination path
 *
 * RETURNS
 *  TRUE  If a relative path can be formed. lpszPath contains the new path
 *  FALSE If the paths are not relative or any parameters are invalid
 *
 * NOTES
 *  lpszTo should be at least MAX_PATH in length.
 *  Calling this function with relative paths for lpszFrom or lpszTo may
 *  give erroneous results.
 *
 *  The Win32 version of this function contains a bug where the lpszTo string
 *  may be referenced 1 byte beyond the end of the string. As a result random
 *  garbage may be written to the output path, depending on what lies beyond
 *  the last byte of the string. This bug occurs because of the behaviour of
 *  PathCommonPrefix() (see notes for that function), and no workaround seems
 *  possible with Win32.
 *  This bug has been fixed here, so for example the relative path from "\\"
 *  to "\\" is correctly determined as "." in this implementation.
 */
      </PRE
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="API-DOCS-ADVANCED"
>Advanced API Documentation</A
></H3
><P
>        There is no markup language for formatting API comments, since they should
        be easily readable by any developer working on the source file. A number of
        constructs are treated specially however, and are noted here. You can use these
        constructs to enhance the usefulness of the generated documentation by making it
        easier to read and referencing related documents.
      </P
><P
>        Any valid c identifier that ends with <B
CLASS="COMMAND"
>()</B
> is taken to
        be an API function and is formatted accordingly. When generating documentation,
        this text will become a link to that API call, if the output type supports
        hyperlinks or their equivalent.
      </P
><P
>        Similarly, any interface name starting with a capital I and followed by the
        words "reference" or "object" become a link to that objects documentation.
      </P
><P
>        Where an Ascii and Unicode version of a function are available, it is
        recommended that you document only the Unicode version and have the Ascii
        version refer to the Unicode one, as follows:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/*************************************************************************
 * PathRelativePathToA   [SHLWAPI.@]
 *
 * See PathRelativePathToW.
 */
      </PRE
></TD
></TR
></TABLE
><P
>        Alternately you may use the following form:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/*************************************************************************
 * PathRelativePathToA   [SHLWAPI.@]
 *
 * Unicode version of PathRelativePathToW.
 */
      </PRE
></TD
></TR
></TABLE
><P
>        You may also use this construct in any other section, such as <B
CLASS="COMMAND"
>NOTES</B
>.
      </P
><P
>        Any numbers and text in quotes (<B
CLASS="COMMAND"
>""</B
>) are highlighted.
      </P
><P
>        Words in all uppercase are assumed to be API constants and are highlighted. If
        you want to emphasize something in the documentation, put it in a section by itself
        rather than making it upper case.
      </P
><P
>        Blank lines in a section cause a new paragraph to be started. Blank lines
        at the start and end of sections are ignored.
      </P
><P
>        Any comment line starting with (<B
CLASS="COMMAND"
>"*|"</B
>) is treated as raw text and
        is not pre-processed before being output. This should be used for code listings,
        tables and any text that should remain unformatted.
      </P
><P
>        Any line starting with a single word followed by a colon (<B
CLASS="COMMAND"
>:</B
>)
        is assumed to be case listing and is emphasized and put in its own paragraph. This
        is most often used for return values, as in the example section below.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> * RETURNS
 *  Success: TRUE. Something happens that is documented here.
 *  Failure: FALSE. The reasons why this call can fail are listed here.
      </PRE
></TD
></TR
></TABLE
><P
>        Any line starting with a (<B
CLASS="COMMAND"
>-</B
>) is put into a paragraph by itself.
        this allows lists to avoid being run together.
      </P
><P
>        If you are in doubt as to how your comment will look, try generating the API
        documentation and checking the output.
      </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="API-DOCS-EXTRA"
>Extra API Documentation</A
></H3
><P
>        Simply documenting the API calls available provides a great deal of information to
        developers working with the Win32 API. However additional documentation is needed
        before the API Guide can be considered truly useful or comprehensive. For example,
        COM objects that are available for developers use should be documented, along with
        the interface(s) that those objects export. Also, it would be helpful to document
        each dll, to provide some structure to the documentation.
      </P
><P
>        To facilitate providing extra documentation, you can create comments that provide
        extra documentation on functions, or on keywords such as the name of a COM interface
        or a type definition.
      </P
><P
>        These items are generated using the same formatting rules as described earlier. The
        only difference is the first line of the comment, which indicates to the generator
        that the documentation is supplemental and does not describe an export from the dll
        being processed.
      </P
><P
>        Lets assume you have implemented a COM interface that you want to document; we'll
        use the name <B
CLASS="COMMAND"
>IExample</B
> as an example here. Your comment would
        look like the following (assuming you are exporting this object from
        <TT
CLASS="FILENAME"
>EXAMPLE.DLL</TT
>):
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>/*************************************************************************
 * IExample   {EXAMPLE}
 *
 * The IExample object provides lots of interesting functionality.
 * ...
 */
      </PRE
></TD
></TR
></TABLE
>
      </P
><P
>        Format this documentation exactly as you would a standard export. The only
        difference is the use of curly brackets to mark this documentation as supplemental.
        The generator will output this documentation using the name given before the
        DLL name, and will link to it from the main DLL page. In addition, if you have
        referred to the comment name in other documentation using "IExample interface",
        "IExample object", or "IExample()", those references will point to this documentation.
      </P
><P
>        If you document you COM interfaces this way then all following extra comments that
        follow in the same source file that begin with the same document title will be added
        as references to this comment before it is output. For an example of this see
        <TT
CLASS="FILENAME"
>dlls/oleaut32/safearray.c</TT
>. This uses an extra comment to document
        The SafeArray functions and link them together under one heading.
      </P
><P
>        As a special case, if you use the DLL name as the comment name, the comment will
        be treated as documentation on the DLL itself. When the documentation for the DLL
        is processed, the contents of the comment will be placed before the generated
        statistics, exports and other information that makes up a DLL's documentation page.
      </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="API-DOCS-GENERATING"
>Generating API Documentation</A
></H3
><P
>        Having edited or added new API documentation to a source code file, you
        should generate the documentation to ensure that the result is what you
        expected. Wine includes a tool (slightly misleadingly) called
        <B
CLASS="COMMAND"
>c2man.pl</B
> in the <TT
CLASS="FILENAME"
>tools/</TT
> directory
        which is used to generate the documentation from the source code.
      </P
><P
>        You can run <B
CLASS="COMMAND"
>c2man.pl</B
> manually for testing purposes; it is
        a fairly simple perl script which parses <TT
CLASS="FILENAME"
>.c</TT
> files
        to create output in several formats. If you wish to try this you may want
        to run it with no arguments, which will cause it to print usage information.
      </P
><P
>        An easier way is to use Wine's build system. To create man pages for a given
        dll, just type <B
CLASS="COMMAND"
>make man</B
> from within the dlls directory
        or type <B
CLASS="COMMAND"
>make manpages</B
> in the root directory of the Wine
        source tree. You can then check that a man page was generated for your function,
        it should be present in the <TT
CLASS="FILENAME"
>documentation/man3w</TT
> directory
        with the same name as the function.
      </P
><P
>        Once you have generated the man pages from the source code, running
        <B
CLASS="COMMAND"
>make install</B
> will install them for you. By default they are
        installed in section 3w of the manual, so they don't conflict with any existing
        man page names. So, to read the man page you should use
        <B
CLASS="COMMAND"
>man -S 3w {name}</B
>. Alternately you can edit
        <TT
CLASS="FILENAME"
>/etc/man.config</TT
> and add 3w to the list of search paths
        given in the variable <I
CLASS="EMPHASIS"
>MANSECT</I
>.
      </P
><P
>        You can also generate HTML output for the API documentation, in this case the
        make command is <B
CLASS="COMMAND"
>make doc-html</B
> in the dll directory,
        or <B
CLASS="COMMAND"
>make htmlpages</B
> from the root. The output will be
        placed by default under <TT
CLASS="FILENAME"
>documentation/html</TT
>. Similarly
        you can create SGML source code to produce the <I
CLASS="EMPHASIS"
>Wine Api Guide</I
>
        with the command <B
CLASS="COMMAND"
>make sgmlpages</B
>.
      </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="WINE-DOCBOOK"
>The Wine DocBook System</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="WRITING-DOCBOOK"
>Writing Documentation with DocBook</A
></H3
><P
>          DocBook is a flavour of <ACRONYM
CLASS="ACRONYM"
>SGML</ACRONYM
>
          (<I
CLASS="FIRSTTERM"
>Standard Generalized Markup
          Language</I
>), a syntax for marking up the contents
          of documents.  HTML is another very common flavour of SGML;
          DocBook markup looks very similar to HTML markup, although
          the names of the markup tags differ.
        </P
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN1998"
>Getting Started</A
></H4
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
><B
>Why SGML?</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>               The simple answer to that is that SGML allows you
               to create multiple formats of a given document from a single
	       source. Currently it is used to create HTML, PDF, PS
	       (PostScript) and Text versions of the Wine books.
              </P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
><B
>What do I need?</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>               You need the SGML tools. There are various places where you
               can get them. The most generic way of getting them is from their
               source as discussed below.
              </P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
><B
>Quick instructions</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>               These are the basic steps to create the Wine books from the SGML source.
	      </P
></TD
></TR
></TABLE
></DIV
><P
></P
><OL
TYPE="1"
><LI
><P
>          Go to <A
HREF="http://www.sgmltools.org"
TARGET="_top"
>http://www.sgmltools.org</A
>
          </P
></LI
><LI
><P
>          Download all of the sgmltools packages
          </P
></LI
><LI
><P
>          Install them all and build them (<B
CLASS="COMMAND"
>./configure; make; make install</B
>)
          </P
></LI
><LI
><P
>          Switch to your toplevel wine-docs directory
          </P
></LI
><LI
><P
>          Run <B
CLASS="COMMAND"
>./configure</B
>
          </P
></LI
><LI
><P
>          run <B
CLASS="COMMAND"
>make html</B
>
          </P
></LI
><LI
><P
>          View <TT
CLASS="FILENAME"
>en/wineusr-guide.html</TT
>, <TT
CLASS="FILENAME"
>en/winedev-guide.html</TT
>, etc. in your favorite browser
          </P
></LI
></OL
><P
>          For more information on building the documentation, please see the <TT
CLASS="FILENAME"
>README</TT
> file
          at the toplevel wine-docs directory.
          </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2032"
>Getting SGML for various distributions</A
></H4
><P
>            Most Linux distributions have everything you need already
            bundled up in package form.  Unfortunately, each
            distribution seems to handle its SGML environment
            differently, installing it into different paths, and
            naming its packages according to its own whims.
          </P
><DIV
CLASS="SECT4"
><HR><H5
CLASS="SECT4"
><A
NAME="AEN2035"
>SGML on Red Hat</A
></H5
><P
>            The following packages seem to be sufficient for Red Hat 7.1.  You
            will want to be careful about the order in which you install the
            RPMs.
            <P
></P
><UL
><LI
><P
>                  sgml-common-*.rpm
                </P
></LI
><LI
><P
>                  openjade-*.rpm
                </P
></LI
><LI
><P
>                  perl-SGMLSpm-*.rpm
                </P
></LI
><LI
><P
>                  docbook-dtd*.rpm
                </P
></LI
><LI
><P
>                  docbook-style-dsssl-*.rpm
                </P
></LI
><LI
><P
>                  tetex-*.rpm
                </P
></LI
><LI
><P
>                  jadetex-*.rpm
                </P
></LI
><LI
><P
>                  docbook-utils-*.rpm
                </P
></LI
></UL
>
            You can also use ghostscript to view the ps format output and
            Adobe Acrobat 4 to view the pdf file.
          </P
></DIV
><DIV
CLASS="SECT4"
><HR><H5
CLASS="SECT4"
><A
NAME="AEN2055"
>SGML on Debian</A
></H5
><P
>           This is not a definitive list yet, but it seems
	   you might need the following packages:
            <P
></P
><UL
><LI
><P
>                  docbook
                </P
></LI
><LI
><P
>                  docbook-dsssl
                </P
></LI
><LI
><P
>                  docbook-utils
                </P
></LI
><LI
><P
>                  docbook-xml
                </P
></LI
><LI
><P
>                  docbook-xsl
                </P
></LI
><LI
><P
>                  sgml-base
                </P
></LI
><LI
><P
>                  sgml-data
                </P
></LI
><LI
><P
>                  tetex-base
                </P
></LI
><LI
><P
>                  tetex-bin
                </P
></LI
><LI
><P
>                  jade
                </P
></LI
><LI
><P
>                  jadetex
                </P
></LI
></UL
>
          </P
></DIV
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2081"
>Terminology</A
></H4
><P
>            SGML markup contains a number of syntactical elements that
            serve different purposes in the markup.  We'll run through
            the basics here to make sure we're on the same page when
            we refer to SGML semantics.
          </P
><P
>            The basic currency of SGML is the
            <I
CLASS="FIRSTTERM"
>tag</I
>.  A simple tag consists of a
            pair of angle brackets and the name of the tag.  For
            example, the <CODE
CLASS="SGMLTAG"
>para</CODE
> tag would appear in
            an SGML document as <CODE
CLASS="SGMLTAG"
>&#60;para&#62;</CODE
>.  This start tag indicates
            that the immediately following text should be classified
            according to the tag.  In regular SGML, each opening tag
            must have a matching end tag to show where the start tag's
            contents end.  End tags begin with
            <SPAN
CLASS="QUOTE"
>"<TT
CLASS="LITERAL"
>&#60;/</TT
>"</SPAN
> markup, e.g.,
            <CODE
CLASS="SGMLTAG"
>&#60;/para&#62;</CODE
>.
          </P
><P
>            The combination of a start tag, contents, and an end tag
            is called an <I
CLASS="FIRSTTERM"
>element</I
>.  SGML
            elements can be nested inside of each other, or contain
            only text, or may be a combination of both text and other
            elements, although in most cases it is better to limit
            your elements to one or the other.
          </P
><P
>            The <ACRONYM
CLASS="ACRONYM"
>XML</ACRONYM
> (<I
CLASS="FIRSTTERM"
>eXtensible Markup
            Language</I
>) specification, a modern subset of
            the SGML specification, adds a so-called <I
CLASS="FIRSTTERM"
>empty
            tag</I
>, for elements that contain no text
            content.  The entire element is a single tag, ending with
            <SPAN
CLASS="QUOTE"
>"<TT
CLASS="LITERAL"
>/&#62;</TT
>"</SPAN
>, e.g.,
            <CODE
CLASS="SGMLTAG"
>&#60;xref/&#62;</CODE
>.  However, use of this
            tag style restricts you to XML DocBook processing, and
            your document may no longer compile with SGML-only
            processing systems.
          </P
><P
>            Often a processing system will need more information about
            an element than you can provide with just tags.  SGML
            allows you to add extra <SPAN
CLASS="QUOTE"
>"hints"</SPAN
> in the form
            of SGML <I
CLASS="FIRSTTERM"
>attributes</I
> to pass along
            this information.  The most common use of attributes in
            DocBook is giving specific elements a name, or an ID, so
            you can refer to it from elsewhere.  This ID can be used
            for many things, including file-naming for HTML output,
            hyper-linking to specific parts of the document, and even
            pulling text from that element (see the <CODE
CLASS="SGMLTAG"
>&#60;xref&#62;</CODE
> tag).
          </P
><P
>            An SGML attribute appears inside the start tag, between
            the &#60; and &#62; brackets.  For example, if you wanted to
            set the <CODE
CLASS="SGMLTAG"
>id</CODE
> attribute
            of the <CODE
CLASS="SGMLTAG"
>&#60;book&#62;</CODE
> element to
            <SPAN
CLASS="QUOTE"
>"mybook"</SPAN
>, you would create a start tag like
            this: <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;book id="mybook"&#62;</PRE
></TD
></TR
></TABLE
>
          </P
><P
>            Notice that the contents of the attribute are enclosed in
            quote marks.  These quotes are optional in SGML, but
            mandatory in XML.  It's a good habit to use quotes, as it
            will make it much easier to migrate your documents to an
            XML processing system later on.
          </P
><P
>            You can also specify more than one attribute in a single
            tag: <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;book id="mybook" status="draft"&#62;</PRE
></TD
></TR
></TABLE
>
          </P
><P
>            Another commonly used type of SGML markup is the
            <I
CLASS="FIRSTTERM"
>entity</I
>.  An entity lets you
            associate a block of text with a name.  You declare the
            entity once, at the beginning of your document, and can
            invoke it as many times as you like throughout the
            document.  You can use entities as shorthand, or to make
            it easier to maintain certain phrases in a central
            location, or even to insert the contents of an entire file
            into your document.
          </P
><P
>            An entity in your document is always surrounded by the
            <SPAN
CLASS="QUOTE"
>"&#38;"</SPAN
> and <SPAN
CLASS="QUOTE"
>";"</SPAN
> characters.  One
            entity you'll need sooner or later is the one for the
            <SPAN
CLASS="QUOTE"
>"&#60;"</SPAN
> character.  Since SGML expects all
            tags to begin with a <SPAN
CLASS="QUOTE"
>"&#60;"</SPAN
>, the
            <SPAN
CLASS="QUOTE"
>"&#60;"</SPAN
> is a reserved character.  To use it in
            your document (as I am doing here), you must insert it
            with the <TT
CLASS="LITERAL"
>&#38;lt;</TT
> entity.  Each time
            the SGML processor encounters <TT
CLASS="LITERAL"
>&#38;lt;</TT
>,
            it will place a literal <SPAN
CLASS="QUOTE"
>"&#60;"</SPAN
> in the output
            document. Similarly you must use the <TT
CLASS="LITERAL"
>&#38;gt;</TT
> 
            and <TT
CLASS="LITERAL"
>&#38;amp;</TT
> entities for the 
            <SPAN
CLASS="QUOTE"
>"&#62;"</SPAN
> and <SPAN
CLASS="QUOTE"
>"&#38;"</SPAN
> characters.
          </P
><P
>            The final term you'll need to know when writing simple
            DocBook documents is the <ACRONYM
CLASS="ACRONYM"
>DTD</ACRONYM
>
            (<I
CLASS="FIRSTTERM"
>Document Type Declaration</I
>).  The
            DTD defines the flavour of SGML a given document is written
            in.  It lists all the legal tag names, like <CODE
CLASS="SGMLTAG"
>&#60;book&#62;</CODE
>, <CODE
CLASS="SGMLTAG"
>&#60;para&#62;</CODE
>, and so on, and declares
            how those tags are allowed to be used together.  For
            example, it doesn't make sense to put a <CODE
CLASS="SGMLTAG"
>&#60;book&#62;</CODE
> element inside a <CODE
CLASS="SGMLTAG"
>&#60;para&#62;</CODE
> paragraph element -- only
            the reverse makes sense.
          </P
><P
>            The DTD thus defines the legal structure of the document.
            It also declares which attributes can be used with which
            tags.  The SGML processing system can use the DTD to make
            sure the document is laid out properly before attempting
            to process it.  SGML-aware text editors like
            Emacs can also use the DTD to
            guide you while you write, offering you choices about
            which tags you can add in different places in the
            document, and beeping at you when you try to add a tag
            where it doesn't belong.
          </P
><P
>            Generally, you will declare which DTD you want to use as
            the first line of your SGML document.  In the case of
            DocBook, you will use something like this:
            <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;!doctype book PUBLIC "-//OASIS//DTD
            DocBook V3.1//EN" []&#62; &#60;book&#62; ...
            &#60;/book&#62;</PRE
></TD
></TR
></TABLE
>
          </P
><P
>            Note that you must specify your toplevel element inside
            the doctype declaration.  If you were writing an article
            rather than a book, you might use this declaration instead:
            <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;!doctype article PUBLIC "-//OASIS//DTD DocBook V3.1//EN" []&#62;
&#60;article&#62;
...
&#60;/article&#62;</PRE
></TD
></TR
></TABLE
>
          </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="SGML-DOCUMENT"
>The Document</A
></H4
><P
>            Once you're comfortable with SGML, creating a DocBook
            document is quite simple and straightforward.  Even
            though DocBook contains over 300 different tags, you can
            usually get by with only a small subset of those tags.
            Most of them are for inline formatting, rather than for
            document structuring.  Furthermore, the common tags have
            short, intuitive names.
          </P
><P
>            Below is a (completely nonsensical) example to illustrate
            how a simple document might be laid out.  Notice that all
            <CODE
CLASS="SGMLTAG"
>&#60;chapter&#62;</CODE
> and <CODE
CLASS="SGMLTAG"
>&#60;sect1&#62;</CODE
> elements have <CODE
CLASS="SGMLTAG"
>id</CODE
> attributes.  This is not
            mandatory, but is a good habit to get into, as DocBook is
            commonly converted into HTML, with a separate generated
            file for each <CODE
CLASS="SGMLTAG"
>&#60;book&#62;</CODE
>,
            <CODE
CLASS="SGMLTAG"
>&#60;chapter&#62;</CODE
>, and/or <CODE
CLASS="SGMLTAG"
>&#60;sect1&#62;</CODE
> element.  If the given
            element has an <CODE
CLASS="SGMLTAG"
>id</CODE
>
            attribute, the processor will typically name the file
            accordingly.  Thus, the below document might result in
            <TT
CLASS="FILENAME"
>index.html</TT
>,
            <TT
CLASS="FILENAME"
>chapter-one.html</TT
>,
            <TT
CLASS="FILENAME"
>blobs.html</TT
>, and so on.
          </P
><P
>            Also notice the text marked off with <SPAN
CLASS="QUOTE"
>"&#60;!--
            "</SPAN
> and <SPAN
CLASS="QUOTE"
>" --&#62;"</SPAN
> characters.  These
            denote SGML comments.  SGML processors will completely
            ignore anything between these markers, similar to
            <SPAN
CLASS="QUOTE"
>"/*"</SPAN
> and <SPAN
CLASS="QUOTE"
>"*/"</SPAN
> comments in C
            source code.
          </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;!doctype book PUBLIC "-//OASIS//DTD DocBook V3.1//EN" []&#62;
&#60;book id="index"&#62;
  &#60;bookinfo&#62;
    &#60;title&#62;A Poet's Guide to Nonsense&#60;/title&#62;
  &#60;/bookinfo&#62;

  &#60;chapter id="chapter-one"&#62;
    &#60;title&#62;Blobs and Gribbles&#60;/title&#62;

    &#60;!-- This section contains only one major topic --&#62;
    &#60;sect1 id="blobs"&#62;
      &#60;title&#62;The Story Behind Blobs&#60;/title&#62;
      &#60;para&#62;
        Blobs are often mistaken for ice cubes and rain
        puddles...
      &#60;/para&#62;
    &#60;/sect1&#62;

    &#60;!-- This section contains embedded sub-sections --&#62;
    &#60;sect1 id="gribbles"&#62;
      &#60;title&#62;Your Friend the Gribble&#60;/title&#62;
      &#60;para&#62;
        A Gribble is a cute, unassuming little fellow...
      &#60;/para&#62;

      &#60;sect2 id="gribble-temperament"&#62;
        &#60;title&#62;Gribble Temperament&#60;/title&#62;
        &#60;para&#62;
          When left without food for several days...
        &#60;/para&#62;
      &#60;/sect2&#62;

      &#60;sect2 id="gribble-appearance"&#62;
        &#60;title&#62;Gribble Appearance&#60;/title&#62;
        &#60;para&#62;
          Most Gribbles have a shock of white fur running from...
        &#60;/para&#62;
      &#60;/sect2&#62;
    &#60;/sect1&#62;
  &#60;/chapter&#62;

  &#60;chapter id="chapter-two"&#62;
    &#60;title&#62;Phantasmagoria&#60;/title&#62;

    &#60;sect1 id="dretch-pools"&#62;
      &#60;title&#62;Dretch Pools&#60;/title&#62;

      &#60;para&#62;
        When most poets think of Dretch Pools, they tend to...
      &#60;/para&#62;
    &#60;/sect&#62;
  &#60;/chapter&#62;
&#60;/book&#62;</PRE
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2159"
>Common Elements</A
></H4
><P
>            Once you get used to the syntax of SGML, the next hurdle
            in writing DocBook documentation is to learn the many
            DocBook-specific tag names, and when to use them.  DocBook
            was created for technical documentation, and as such, the
            tag names and document structure are slanted towards the
            needs of such documentation.
          </P
><P
>            To cover its target audience, DocBook declares a wide
            variety of specialized tags, including tags for formatting
            source code (with somewhat of a C/C++ bias), computer
            prompts, GUI application features, keystrokes, and so on.
            DocBook also includes tags for universal formatting needs,
            like headers, footnotes, tables, and graphics.
          </P
><P
>            We won't cover all of these elements here (over 300
            DocBook tags exist!), but we will cover the basics.  To
            learn more about the other tags, check out the official
            DocBook guide, at <A
HREF="http://docbook.org"
TARGET="_top"
>http://docbook.org</A
>.  To
            see how they are used in practice, download the SGML
            source for this manual (the Wine Developer Guide) and
            browse through it, comparing it to the generated HTML (or
            PostScript or PDF).
          </P
><P
>            There are often many correct ways to mark up a given piece
            of text, and you may have to make guesses about which tag
            to use.  Sometimes you'll have to make compromises.
            However, remember that it is possible to further customize 
	    the output of  the SGML processors.  If you don't like the 
	    way a certain tag looks in HTML, that doesn't mean you
	    should choose a different tag based on its output formatting.
            The processing stylesheets can be altered to fix the
            formatting of that same tag everywhere in the document
            (not just in the place you're working on).  For example,
            if you're frustrated that the <CODE
CLASS="SGMLTAG"
>&#60;systemitem&#62;</CODE
> tag doesn't produce
            any formatting by default, you should fix the stylesheets,
            not change the valid <CODE
CLASS="SGMLTAG"
>&#60;systemitem&#62;</CODE
> tag to, for example,
            an <CODE
CLASS="SGMLTAG"
>&#60;emphasis&#62;</CODE
> tag.
          </P
><P
>            Here are the common SGML elements:
          </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
>Structural Elements</B
></P
><DL
><DT
><CODE
CLASS="SGMLTAG"
>&#60;book&#62;</CODE
></DT
><DD
><P
>                  The book is the most common toplevel element, and is
                  probably the one you should use for your document.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;set&#62;</CODE
></DT
><DD
><P
>                  If you want to group more than one book into a
                  single unit, you can place them all inside a set.
                  This is useful when you want to bundle up
                  documentation in alternate ways.  We do this with
                  the Wine documentation, using 
                  <CODE
CLASS="SGMLTAG"
>&#60;book&#62;</CODE
> to
                  put each Wine guide into a separate directory (see
                  <TT
CLASS="FILENAME"
>documentation/wine-devel.sgml</TT
>,
                  etc.).
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;chapter&#62;</CODE
></DT
><DD
><P
>                  A <CODE
CLASS="SGMLTAG"
>&#60;chapter&#62;</CODE
>
                  element includes a single entire chapter of the
                  book.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;part&#62;</CODE
></DT
><DD
><P
>                  If the chapters in your book fall into major
                  categories or groupings (as in the Wine Developer
                  Guide), you can place each collection of chapters
                  into a <CODE
CLASS="SGMLTAG"
>&#60;part&#62;</CODE
>
                  element.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;sect?&#62;</CODE
></DT
><DD
><P
>                  DocBook has many section elements to divide the
                  contents of a chapter into smaller chunks.  The
                  encouraged approach is to use the numbered section
                  tags, <CODE
CLASS="SGMLTAG"
>&#60;sect1&#62;</CODE
>,
                  <CODE
CLASS="SGMLTAG"
>&#60;sect2&#62;</CODE
>, <CODE
CLASS="SGMLTAG"
>&#60;sect3&#62;</CODE
>, <CODE
CLASS="SGMLTAG"
>&#60;sect4&#62;</CODE
>, and <CODE
CLASS="SGMLTAG"
>&#60;sect5&#62;</CODE
> (if necessary).
                  These tags must be nested in order: you can't place
                  a <CODE
CLASS="SGMLTAG"
>&#60;sect3&#62;</CODE
> directly
                  inside a <CODE
CLASS="SGMLTAG"
>&#60;sect1&#62;</CODE
>.
                  You have to nest the <CODE
CLASS="SGMLTAG"
>&#60;sect3&#62;</CODE
> inside a <CODE
CLASS="SGMLTAG"
>&#60;sect2&#62;</CODE
>, and so forth.
                  Documents with these explicit section groupings are
                  easier for SGML processors to deal with, and lead to
                  better organized documents.  DocBook also supplies a
                  <CODE
CLASS="SGMLTAG"
>&#60;section&#62;</CODE
> element
                  which you can nest inside itself, but its use is
                  discouraged in favor of the numbered section tags.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;title&#62;</CODE
></DT
><DD
><P
>                  The title of a book, chapter, part, section, etc.
                  In most of the major structural elements, like
                  <CODE
CLASS="SGMLTAG"
>&#60;chapter&#62;</CODE
>,
                  <CODE
CLASS="SGMLTAG"
>&#60;part&#62;</CODE
>, and the
                  various section tags, <CODE
CLASS="SGMLTAG"
>&#60;title&#62;</CODE
> is mandatory.  In
                  other elements like <CODE
CLASS="SGMLTAG"
>&#60;book&#62;</CODE
> and <CODE
CLASS="SGMLTAG"
>&#60;note&#62;</CODE
>, it's optional.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;para&#62;</CODE
></DT
><DD
><P
>                  The basic unit of text is the paragraph, represented
                  by the <CODE
CLASS="SGMLTAG"
>&#60;para&#62;</CODE
> tag.
                  This is probably the tag you'll use most often.  In
                  fact, in a simple document, you can probably get
                  away with using only <CODE
CLASS="SGMLTAG"
>&#60;book&#62;</CODE
>, <CODE
CLASS="SGMLTAG"
>&#60;chapter&#62;</CODE
>, <CODE
CLASS="SGMLTAG"
>&#60;title&#62;</CODE
>, and <CODE
CLASS="SGMLTAG"
>&#60;para&#62;</CODE
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;article&#62;</CODE
></DT
><DD
><P
>                  For shorter, more targeted documents, like topic
                  pieces and whitepapers, you can use <CODE
CLASS="SGMLTAG"
>&#60;article&#62;</CODE
> as your toplevel
                  element.
                </P
></DD
></DL
></DIV
><P
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
>Inline Formatting Elements</B
></P
><DL
><DT
><CODE
CLASS="SGMLTAG"
>&#60;filename&#62;</CODE
></DT
><DD
><P
>                  The name of a file.  You can optionally set the
                  <CODE
CLASS="SGMLTAG"
>class</CODE
> attribute
                  to <TT
CLASS="LITERAL"
>Directory</TT
>,
                  <TT
CLASS="LITERAL"
>HeaderFile</TT
>, and
                  <TT
CLASS="LITERAL"
>SymLink</TT
> to further classify the
                  filename.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;userinput&#62;</CODE
></DT
><DD
><P
>                  Literal text entered by the user.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;computeroutput&#62;</CODE
></DT
><DD
><P
>                  Literal text output by the computer.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;literal&#62;</CODE
></DT
><DD
><P
>                  A catch-all element for literal computer data.  Its
                  use is somewhat vague; try to use a more specific
                  tag if possible, like <CODE
CLASS="SGMLTAG"
>&#60;userinput&#62;</CODE
> or <CODE
CLASS="SGMLTAG"
>&#60;computeroutput&#62;</CODE
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;quote&#62;</CODE
></DT
><DD
><P
>                  An inline quotation.  This tag typically inserts
                  quotation marks for you, so you would write <CODE
CLASS="SGMLTAG"
>&#60;quote&#62;</CODE
>This is a
                  quote<CODE
CLASS="SGMLTAG"
>&#60;/quote&#62;</CODE
> rather
                  than "This is a quote".  This usage may be a little
                  bulkier, but it does allow for automated formatting
                  of all quoted material in the document.  Thus, if
                  you wanted all quotations to appear in italic, you
                  could make the change once in your stylesheet,
                  rather than doing a search and replace throughout
                  the document.  For larger chunks of quoted text, you
                  can use <CODE
CLASS="SGMLTAG"
>&#60;blockquote&#62;</CODE
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;note&#62;</CODE
></DT
><DD
><P
>                  Insert a side note for the reader.  By default, the
                  SGML processor usually prefixes the content with
                  "Note:".  You can change this text by adding a
                  <CODE
CLASS="SGMLTAG"
>&#60;title&#62;</CODE
> element.
                  Thus, to add a visible FIXME comment to the
                  documentation, you might write:
                </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;note&#62;
  &#60;title&#62;EXAMPLE&#60;/title&#62;
  &#60;para&#62;This is an example note...&#60;/para&#62;
&#60;/note&#62;</PRE
></TD
></TR
></TABLE
><P
>                  The results will look something like this:
                </P
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
><B
>EXAMPLE</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>This is an example note...</P
></TD
></TR
></TABLE
></DIV
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;sgmltag&#62;</CODE
></DT
><DD
><P
>                  Used for inserting SGML tags, etc., into a SGML
                  document without resorting to a lot of entity
                  quoting, e.g., &#38;lt;.  You can change the
                  appearance of the text with the <CODE
CLASS="SGMLTAG"
>class</CODE
> attribute.  Some
                  common values of this are
                  <TT
CLASS="LITERAL"
>starttag</TT
>,
                  <TT
CLASS="LITERAL"
>endtag</TT
>,
                  <TT
CLASS="LITERAL"
>attribute</TT
>,
                  <TT
CLASS="LITERAL"
>attvalue</TT
>, and even
                  <TT
CLASS="LITERAL"
>sgmlcomment</TT
>.  See this SGML file,
                  <TT
CLASS="FILENAME"
>documentation/documentation.sgml</TT
>,
                  for examples.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;prompt&#62;</CODE
></DT
><DD
><P
>                  The text used for a computer prompt, for example a
                  shell prompt, or command-line application prompt.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;replaceable&#62;</CODE
></DT
><DD
><P
>                  Meta-text that should be replaced by the user, not
                  typed in literally, e.g., in command descriptions
                  and <TT
CLASS="PARAMETER"
><I
>--help</I
></TT
> outputs.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;constant&#62;</CODE
></DT
><DD
><P
>                  A programming constant, e.g.,
                  <CODE
CLASS="CONSTANT"
>MAX_PATH</CODE
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;symbol&#62;</CODE
></DT
><DD
><P
>                  A symbolic value replaced, for example, by a
                  pre-processor.  This applies primarily to C macros,
                  but may have other uses.  Use the <CODE
CLASS="SGMLTAG"
>&#60;constant&#62;</CODE
> tag instead of
                  <CODE
CLASS="SGMLTAG"
>&#60;symbol&#62;</CODE
> where
                  appropriate.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;function&#62;</CODE
></DT
><DD
><P
>                  A programming function name.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;parameter&#62;</CODE
></DT
><DD
><P
>                  Programming language parameters you pass with a
                  function.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;option&#62;</CODE
></DT
><DD
><P
>                  Parameters you pass to a command-line executable.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;varname&#62;</CODE
></DT
><DD
><P
>                  Variable name, typically in a programming language.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;type&#62;</CODE
></DT
><DD
><P
>                  Programming language types, e.g., from a typedef
                  definition.  May have other uses, too.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;structname&#62;</CODE
></DT
><DD
><P
>                  The name of a C-language <SPAN
CLASS="TYPE"
>struct</SPAN
>
                  declaration, e.g., <CODE
CLASS="STRUCTNAME"
>sockaddr</CODE
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;structfield&#62;</CODE
></DT
><DD
><P
>                  A field inside a C <SPAN
CLASS="TYPE"
>struct</SPAN
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;command&#62;</CODE
></DT
><DD
><P
>                  An executable binary, e.g., <B
CLASS="COMMAND"
>wine</B
>
                  or <B
CLASS="COMMAND"
>ls</B
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;envar&#62;</CODE
></DT
><DD
><P
>                  An environment variable, e.g, <CODE
CLASS="ENVAR"
>$PATH</CODE
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;systemitem&#62;</CODE
></DT
><DD
><P
>                  A generic catch-all for system-related things, like
                  OS names, computer names, system resources, etc.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;email&#62;</CODE
></DT
><DD
><P
>                  An email address.  The SGML processor will typically
                  add extra formatting characters, and even a
                  <TT
CLASS="LITERAL"
>mailto:</TT
> link for HTML pages.
                  Usage: <CODE
CLASS="SGMLTAG"
>&#60;email&#62;</CODE
>user@host.com<CODE
CLASS="SGMLTAG"
>&#60;/email&#62;</CODE
>
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;firstterm&#62;</CODE
></DT
><DD
><P
>                  Special emphasis for introducing a new term.  Can
                  also be linked to a <CODE
CLASS="SGMLTAG"
>&#60;glossary&#62;</CODE
> entry, if
                  desired.
                </P
></DD
></DL
></DIV
><P
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
>Item Listing Elements</B
></P
><DL
><DT
><CODE
CLASS="SGMLTAG"
>&#60;itemizedlist&#62;</CODE
></DT
><DD
><P
>                  For bulleted lists, no numbering.  You can tweak the
                  layout with SGML attributes.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;orderedlist&#62;</CODE
></DT
><DD
><P
>                  A numbered list; the SGML processor will insert the
                  numbers for you.  You can suggest numbering styles
                  with the <CODE
CLASS="SGMLTAG"
>numeration</CODE
> attribute.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;simplelist&#62;</CODE
></DT
><DD
><P
>                  A very simple list of items, often inlined.  Control
                  the layout with the <CODE
CLASS="SGMLTAG"
>type</CODE
> attribute.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;variablelist&#62;</CODE
></DT
><DD
><P
>                  A list of terms with definitions or descriptions,
                  like this very list!
                </P
></DD
></DL
></DIV
><P
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
>Block Text Quoting Elements</B
></P
><DL
><DT
><CODE
CLASS="SGMLTAG"
>&#60;programlisting&#62;</CODE
></DT
><DD
><P
>                  Quote a block of source code.  Typically highlighted
                  in the output and set off from normal text.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;screen&#62;</CODE
></DT
><DD
><P
>                  Quote a block of visible computer output, like the
                  output of a command or chunks of debug logs.
                </P
></DD
></DL
></DIV
><P
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
>Hyperlink Elements</B
></P
><DL
><DT
><CODE
CLASS="SGMLTAG"
>&#60;link&#62;</CODE
></DT
><DD
><P
>                  Generic hypertext link, used for pointing to other
                  sections within the current document.  You supply
                  the visible text for the link, plus the name of the <CODE
CLASS="SGMLTAG"
>id</CODE
> attribute of the
                  element that you want to link to.  For example:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;link linkend="configuring-wine"&#62;the section on configuring wine&#60;/link&#62;
...
&#60;sect2 id="configuring-wine"&#62;
...</PRE
></TD
></TR
></TABLE
>
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;xref&#62;</CODE
></DT
><DD
><P
>                  In-document hyperlink that can generate its own
                  text.  Similar to the <CODE
CLASS="SGMLTAG"
>&#60;link&#62;</CODE
> tag, you use the
                  <CODE
CLASS="SGMLTAG"
>linkend</CODE
>
                  attribute to specify which target element you want
                  to jump to:
                </P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;xref linkend="configuring-wine"&#62;
...
&#60;sect2 id="configuring-wine"&#62;
...</PRE
></TD
></TR
></TABLE
>
                </P
><P
>                  By default, most SGML processors will auto generate
                  some generic text for the <CODE
CLASS="SGMLTAG"
>&#60;xref&#62;</CODE
> link, like
                  <SPAN
CLASS="QUOTE"
>"Section 2.3.1"</SPAN
>.  You can use the
                  <CODE
CLASS="SGMLTAG"
>endterm</CODE
>
                  attribute to grab the visible text content of the
                  hyperlink from another element:
                </P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;xref linkend="configuring-wine" endterm="config-title"&#62;
...
&#60;sect2 id="configuring-wine"&#62;
  &#60;title id="config-title"&#62;Configuring Wine&#60;/title&#62;
...</PRE
></TD
></TR
></TABLE
>
                </P
><P
>                  This would create a link to the
                  <SPAN
CLASS="SYMBOL"
>configuring-wine</SPAN
> element,
                  displaying the text of the
                  <SPAN
CLASS="SYMBOL"
>config-title</SPAN
> element for the
                  hyperlink.  Most often, you'll add an <CODE
CLASS="SGMLTAG"
>id</CODE
> attribute to the
                  <CODE
CLASS="SGMLTAG"
>&#60;title&#62;</CODE
> of the
                  section you're linking to, as above, in which case
                  the SGML processor will use the target's title text
                  for the link text.
                </P
><P
>                  Alternatively, you can use an <CODE
CLASS="SGMLTAG"
>xreflabel</CODE
> attribute in
                  the target element tag to specify the link text:
                </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#60;sect1 id="configuring-wine" xreflabel="Configuring Wine"&#62;</PRE
></TD
></TR
></TABLE
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>                    <CODE
CLASS="SGMLTAG"
>&#60;xref&#62;</CODE
> is an
                    empty element.  You don't need a closing tag for
                    it (this is defined in the DTD).  In SGML
                    documents, you should use the form <CODE
CLASS="SGMLTAG"
>&#60;xref&#62;</CODE
>, while in XML
                    documents you should use
                    <CODE
CLASS="SGMLTAG"
>&#60;xref/&#62;</CODE
>.
                  </P
></TD
></TR
></TABLE
></DIV
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;anchor&#62;</CODE
></DT
><DD
><P
>                  An invisible tag, used for inserting <CODE
CLASS="SGMLTAG"
>id</CODE
> attributes into a
                  document to link to arbitrary places (i.e., when
                  it's not close enough to link to the top of an
                  element).
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;ulink&#62;</CODE
></DT
><DD
><P
>                  Hyperlink in URL form, e.g., <A
HREF="http://www.winehq.org"
TARGET="_top"
>http://www.winehq.org</A
>.
                </P
></DD
><DT
><CODE
CLASS="SGMLTAG"
>&#60;olink&#62;</CODE
></DT
><DD
><P
>                  Indirect hyperlink; can be used for linking to
                  external documents.  Not often used in practice.
                </P
></DD
></DL
></DIV
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="SGML-ENVIRONMENT"
>Editing SGML Documents</A
></H3
><P
>          You can write SGML/DocBook documents in any text editor you
          might find although some editors are more friendly for
          this task than others.
        </P
><P
>          The most commonly used open source SGML editor is Emacs,
          with the PSGML <I
CLASS="FIRSTTERM"
>mode</I
>, or extension.
          Emacs does not supply a GUI or WYSIWYG (What You See Is What
          You Get) interface, but it does provide many helpful
          shortcuts for creating SGML, as well as automatic
          formatting, validity checking, and the ability to create
          your own macros to simplify complex, repetitive actions.
        </P
></DIV
></DIV
></DIV
></DIV
><DIV
CLASS="PART"
><A
NAME="PART-TWO"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="TITLE"
>II. Wine Architecture</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="#ARCHITECTURE"
>Overview</A
></DT
><DT
><A
HREF="#AEN2768"
>Kernel modules</A
></DT
><DT
><A
HREF="#AEN3750"
>Graphical modules</A
></DT
><DT
><A
HREF="#AEN3764"
>Windowing system</A
></DT
><DT
><A
HREF="#OLE"
>COM in Wine</A
></DT
><DT
><A
HREF="#OPENGL"
>Wine and OpenGL</A
></DT
><DT
><A
HREF="#DDRAW"
>Outline of DirectDraw Architecture</A
></DT
><DT
><A
HREF="#MULTIMEDIA"
>Wine and Multimedia</A
></DT
></DL
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="ARCHITECTURE"
></A
>Overview</H1
><P
>Brief overview of Wine's architecture...</P
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="BASIC-OVERVIEW"
>Wine Overview</A
></H2
><P
>        With the fundamental architecture of Wine stabilizing, and
        people starting to think that we might soon be ready to
        actually release this thing, it may be time to take a look at
        how Wine actually works and operates.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2493"
>Foreword</A
></H3
><P
>          Wine is often used as a recursive acronym, standing for
          "Wine Is Not an Emulator". Sometimes it is also known to be
          used for "Windows Emulator". In a way, both meanings are
          correct, only seen from different perspectives. The first
          meaning says that Wine is not a virtual machine, it does not
          emulate a CPU, and you are not supposed to install 
          Windows nor any Windows device drivers on top of it; rather,
          Wine is an implementation of the Windows API, and can be
          used as a library to port Windows applications to Unix. The
          second meaning, obviously, is that to Windows binaries
          (<TT
CLASS="FILENAME"
>.exe</TT
> files), Wine does look like
          Windows, and emulates its behaviour and quirks rather
          closely.
        </P
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
><B
>"Emulator"</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>            The "Emulator" perspective should not be thought of as if
            Wine is a typical inefficient emulation layer that means
            Wine can't be anything but slow - the faithfulness to the
            badly designed Windows API may of course impose a minor
            overhead in some cases, but this is both balanced out by
            the higher efficiency of the Unix platforms Wine runs on,
            and that other possible abstraction libraries (like Motif,
            GTK+, CORBA, etc) has a runtime overhead typically
            comparable to Wine's.
          </P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2500"
>Executables</A
></H3
><P
>	  Wine's main task is to run Windows executables under non
	  Windows operating systems. It supports different types of
	  executables:
	  <P
></P
><UL
><LI
><P
>		DOS executable. Those are even older programs, using
		the DOS format (either <TT
CLASS="FILENAME"
>.com</TT
> or
		<TT
CLASS="FILENAME"
>.exe</TT
> (the later being also called
		MZ)).
	      </P
></LI
><LI
><P
>		Windows NE executable, also called 16 bit. They were
		the native processes run by Windows 2.x and 3.x. NE
		stands for New Executable &#60;g&#62;.
	      </P
></LI
><LI
><P
>		Windows PE executable. These are programs were
		introduced in Windows 95 (and became the native
		formats for all later Windows version), even if 16 bit
		applications were still supported. PE stands for
		Portable Executable, in a sense where the format of
		the executable (as a file) is independent of the CPU
		(even if the content of the file - the code - is CPU
		dependent). 
	      </P
></LI
><LI
><P
>		Winelib executable. These are applications, written
		using the Windows API, but compiled as a Unix
		executable. Wine provides the tools to create such
		executables.
	      </P
></LI
></UL
>
	</P
><P
>	  Let's quickly review the main differences for the supported
	  executables:
	  <DIV
CLASS="TABLE"
><A
NAME="AEN2515"
></A
><P
><B
>Table 1. Wine executables</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>&nbsp;</TH
><TH
>DOS (.COM or .EXE)</TH
><TH
>Win16 (NE)</TH
><TH
>Win32 (PE)</TH
><TH
>Winelib</TH
></TR
></THEAD
><TBODY
><TR
><TD
>Multitasking</TD
><TD
>Only one application at a time (except for TSR)</TD
><TD
>Cooperative</TD
><TD
>Preemptive</TD
><TD
>Preemptive</TD
></TR
><TR
><TD
>Address space</TD
><TD
>		    One MB of memory, where each application is loaded
		    and unloaded.
		  </TD
><TD
>		    All 16 bit applications share a single address
		    space, protected mode.
		  </TD
><TD
>		    Each application has it's own address
		    space. Requires MMU support from CPU. 
		  </TD
><TD
>		    Each application has it's own address
		    space. Requires MMU support from CPU. 
		  </TD
></TR
><TR
><TD
>Windows API</TD
><TD
>		    No Windows API but the DOS API (like <TT
CLASS="FUNCTION"
>Int
		      21h</TT
> traps).
		  </TD
><TD
>		    Will call the 16 bit Windows API.
		  </TD
><TD
>		    Will call the 32 bit Windows API.
		  </TD
><TD
>		    Will call the 32 bit Windows API, and possibly
		    also the Unix APIs.
		  </TD
></TR
><TR
><TD
>Code (CPU level)</TD
><TD
>		    Only available on x86 in real mode. Code and data
		    are in segmented forms, with 16 bit
		    offsets. Processor is in real mode.
		  </TD
><TD
>		    Only available on IA-32 architectures, code and
		    data are in segmented forms, with 16 bit offsets
		    (hence the 16 bit name). Processor is in protected
		    mode.
		  </TD
><TD
>		    Available (with NT) on several CPUs, including
		    IA-32. On this CPU, uses a flat memory model with
		    32 bit offsets (hence the 32 bit name).
		  </TD
><TD
>		    Flat model, with 32 bit addresses.
		  </TD
></TR
><TR
><TD
>Multi-threading</TD
><TD
>Not available.</TD
><TD
>Not available.</TD
><TD
>		    Available.
		  </TD
><TD
>		    Available, but must use the Win32 APIs for
		    threading and synchronization, not the Unix ones.
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
><P
>          Wine deals with this issue by launching a separate Wine process (which
	  is in fact a Unix process) for each Win32 process, but not for Win16
	  tasks. Win16 tasks are run as different intersynchronized Unix-threads
	  in the same dedicated Wine process; this Wine process is commonly
	  known as a <I
CLASS="FIRSTTERM"
>WOW</I
> process (Windows on Windows),
	  referring to a similar mechanism used by Windows NT.
	</P
><P
>          Synchronization between the Win16 tasks running in the WOW
          process is normally done through the Win16 mutex - whenever
          one of them is running, it holds the Win16 mutex, keeping
          the others from running. When the task wishes to let the
          other tasks run, the thread releases the Win16 mutex, and
          one of the waiting threads will then acquire it and let its
          task run.
        </P
><P
>	  <B
CLASS="COMMAND"
>winevdm</B
> is the Wine process dedicated to running the
	  Win16 processes. Note that several instances of this process could
	  exist, has Windows has support for different VDM (Virtual Dos
	  Machines) in order to have Win16 processes running in different
	  address spaces. Wine also uses the same architecture to run DOS
	  programs (in this case, the DOS emulation is provided by a Wine only
	  DLL called <TT
CLASS="FILENAME"
>winedos</TT
>.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN2563"
>Standard Windows Architectures</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN2565"
>Windows 9x architecture</A
></H3
><P
>	  The windows architecture (Win 9x way) looks like this:
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>+---------------------+        \
|     Windows EXE     |         } application
+---------------------+        /

+---------+ +---------+        \
| Windows | | Windows |         \ application &#38; system DLLs
|   DLL   | |   DLL   |         /
+---------+ +---------+        /

+---------+ +---------+      \
|  GDI32  | | USER32  |       \
|   DLL   | |   DLL   |        \
+---------+ +---------+         } core system DLLs
+---------------------+        /
|     Kernel32 DLL    |       /
+---------------------+      / 

+---------------------+        \
|     Win9x kernel    |         } kernel space
+---------------------+        /

+---------------------+       \
|  Windows low-level  |        \ drivers (kernel space)
|       drivers       |        /
+---------------------+       /
	  </PRE
></TD
></TR
></TABLE
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2569"
>Windows NT architecture</A
></H3
><P
>	  The windows architecture (Windows NT way) looks like the
	  following drawing. Note the new DLL
	  (<TT
CLASS="FILENAME"
>NTDLL</TT
>) which allows implementing
	  different subsystems (as win32);
	  <TT
CLASS="FILENAME"
>kernel32</TT
> in NT architecture
	  implements the Win32 subsystem on top of
	  <TT
CLASS="FILENAME"
>NTDLL</TT
>.
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>+---------------------+                      \
|     Windows EXE     |                       } application
+---------------------+                      /

+---------+ +---------+                      \
| Windows | | Windows |                       \ application &#38; system DLLs
|   DLL   | |   DLL   |                       /
+---------+ +---------+                      /

+---------+ +---------+     +-----------+   \
|  GDI32  | |  USER32 |     |           |    \
|   DLL   | |   DLL   |     |           |     \
+---------+ +---------+     |           |      \ core system DLLs
+---------------------+     |           |      / (on the left side)
|    Kernel32 DLL     |     | Subsystem |     /
|  (Win32 subsystem)  |     |Posix, OS/2|    /
+---------------------+     +-----------+   / 

+---------------------------------------+
|               NTDLL.DLL               |
+---------------------------------------+

+---------------------------------------+     \
|               NT kernel               |      } NT kernel (kernel space)
+---------------------------------------+     /
+---------------------------------------+     \
|       Windows low-level drivers       |      } drivers (kernel space)
+---------------------------------------+     /
	  </PRE
></TD
></TR
></TABLE
>
	</P
><P
>	  Note also (not depicted in schema above) that the 16 bit
	  applications are supported in a specific subsystem.
	  Some basic differences between the Win9x and the NT
	  architectures include:
	  <P
></P
><UL
><LI
><P
>		Several subsystems (Win32, Posix...) can be run on NT,
		while not on Win 9x
	      </P
></LI
><LI
><P
>		Win 9x roots its architecture in 16 bit systems, while
		NT is truly a 32 bit system.
	      </P
></LI
><LI
><P
>		The drivers model and interfaces in Win 9x and NT are
		different (even if Microsoft tried to bridge the gap
		with some support of WDM drivers in Win 98 and above).
	      </P
></LI
></UL
>
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN2584"
>Wine architecture</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN2586"
>Global picture</A
></H3
><P
>	  Wine implementation is closer to the Windows NT
	  architecture, even if several subsystems are not implemented
	  yet (remind also that 16bit support is implemented in a 32-bit
	  Windows EXE, not as a subsystem). Here's the overall picture:
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>+---------------------+                                  \
|     Windows EXE     |                                   } application
+---------------------+                                  /

+---------+ +---------+                                  \
| Windows | | Windows |                                   \ application &#38; system DLLs
|   DLL   | |   DLL   |                                   /
+---------+ +---------+                                  /

+---------+ +---------+     +-----------+  +--------+  \
|  GDI32  | |  USER32 |     |           |  |        |   \
|   DLL   | |   DLL   |     |           |  |  Wine  |    \
+---------+ +---------+     |           |  | Server |     \ core system DLLs
+---------------------+     |           |  |        |     / (on the left side)
|    Kernel32 DLL     |     | Subsystem |  | NT-like|    /
|  (Win32 subsystem)  |     |Posix, OS/2|  | Kernel |   /
+---------------------+     +-----------+  |        |  / 
                                           |        |
+---------------------------------------+  |        |
|                 NTDLL                 |  |        |
+---------------------------------------+  +--------+

+---------------------------------------+               \
|   Wine executable (wine-?thread)      |                } unix executable
+---------------------------------------+               /
+---------------------------------------------------+   \
|                   Wine drivers                    |    } Wine specific DLLs
+---------------------------------------------------+   /

+------------+    +------------+     +--------------+   \
|    libc    |    |   libX11   |     |  other libs  |    } unix shared libraries
+------------+    +------------+     +--------------+   /  (user space)

+---------------------------------------------------+   \
|         Unix kernel (Linux,*BSD,Solaris,OS/X)     |    } (Unix) kernel space
+---------------------------------------------------+   /
+---------------------------------------------------+   \
|                 Unix device drivers               |    } Unix drivers (kernel space)
+---------------------------------------------------+   /
	  </PRE
></TD
></TR
></TABLE
>
	</P
><P
>	  Wine must at least completely replace the "Big Three" DLLs
	  (<TT
CLASS="FILENAME"
>KERNEL</TT
>/<TT
CLASS="FILENAME"
>KERNEL32</TT
>,
	  <TT
CLASS="FILENAME"
>GDI</TT
>/<TT
CLASS="FILENAME"
>GDI32</TT
>, and
	  <TT
CLASS="FILENAME"
>USER</TT
>/<TT
CLASS="FILENAME"
>USER32</TT
>),
	  which all other DLLs are layered on top of. But since Wine
	  is (for various reasons) leaning towards the NT way of
	  implementing things, the <TT
CLASS="FILENAME"
>NTDLL</TT
> is
	  another core DLL to be implemented in Wine, and many
	  <TT
CLASS="FILENAME"
>KERNEL32</TT
> and 
	  <TT
CLASS="FILENAME"
>ADVAPI32</TT
> features will be
	  implemented through the <TT
CLASS="FILENAME"
>NTDLL</TT
>.
	</P
><P
>	  As of today, no real subsystem (apart the Win32 one) has
	  been implemented in Wine. 
	</P
><P
>	  The Wine server provides the backbone for the implementation
	  of the core DLLs. It mainly implementents inter-process
	  synchronization and object sharing. It can be seen, from a
	  functional point of view, as a NT kernel (even if the APIs
	  and protocols used between Wine's DLL and the Wine server
	  are Wine specific).
	</P
><P
>	  Wine uses the Unix drivers to access the various hardware
	  pieces on the box. However, in some cases, Wine will
	  provide a driver (in Windows sense) to a physical hardware
	  device. This driver will be a proxy to the Unix driver
	  (this is the case, for example, for the graphical part
	  with X11 or SDL drivers, audio with OSS or ALSA drivers...).
	</P
><P
>	  All DLLs provided by Wine try to stick as much as possible
	  to the exported APIs from the Windows platforms. There are
	  rare cases where this is not the case, and have been
	  propertly documented (Wine DLLs export some Wine specific
	  APIs). Usually, those are prefixed with
	  <TT
CLASS="FUNCTION"
>__wine</TT
>.
	</P
><P
>	  Let's now review in greater details all of those components.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2607"
>The Wine server</A
></H3
><P
>	  The Wine server is among the most confusing concepts in
	  Wine. What is its function in Wine? Well, to be brief, it
	  provides Inter-Process Communication (IPC),
	  synchronization, and process/thread management. When the
	  Wine server launches, it creates a Unix socket for the
	  current host based on (see below) your home directory's
	  <TT
CLASS="FILENAME"
>.wine</TT
> subdirectory (or wherever the
	  <CODE
CLASS="ENVAR"
>WINEPREFIX</CODE
> environment variable points to)
          - all Wine processes launched later connects to the Wine
	  server using this socket. If a Wine server was not
	  already running, the first Wine process will start up the
	  Wine server in auto-terminate mode (i.e. the Wine server
	  will then terminate itself once the last Wine process has
	  terminated).
	</P
><P
>	  In earlier versions of Wine the master socket mentioned
	  above was actually created in the configuration directory;
	  either your home directory's <TT
CLASS="FILENAME"
>/wine</TT
>
	  subdirectory or wherever the
	  <CODE
CLASS="ENVAR"
>WINEPREFIX</CODE
> environment variable points&#62;.
	  Since that might not be possible the socket is actually
	  created within the <TT
CLASS="FILENAME"
>/tmp</TT
> directory
	  with a name that reflects the configuration directory.
	  This means that there can actually be several separate
	  copies of the Wine server running; one per combination of
	  user and configuration directory. Note that you should
	  not have several users using the same configuration
	  directory at the same time; they will have different
	  copies of the Wine server running and this could well
	  lead to problems with the registry information that
	  they are sharing.
	</P
><P
>	  Every thread in each Wine process has its own request
	  buffer, which is shared with the Wine server. When a
	  thread needs to synchronize or communicate with any other
	  thread or process, it fills out its request buffer, then
	  writes a command code through the socket. The Wine server
	  handles the command as appropriate, while the client
	  thread waits for a reply. In some cases, like with the
	  various <TT
CLASS="FUNCTION"
>WaitFor???</TT
> synchronization
	  primitives, the server handles it by marking the client
	  thread as waiting and does not send it a reply before the
	  wait condition has been satisfied.
	</P
><P
>	  The Wine server itself is a single and separate Unix
	  process and does not have its own threading - instead, it
	  is built on top of a large <TT
CLASS="FUNCTION"
>poll()</TT
>
	  loop that alerts the Wine server whenever anything
	  happens, such as a client having sent a command, or a wait
	  condition having been satisfied. There is thus no danger
	  of race conditions inside the Wine server itself - it is
	  often called upon to do operations that look completely
	  atomic to its clients.
	</P
><P
>	  Because the Wine server needs to manage processes,
	  threads, shared handles, synchronization, and any related
	  issues, all the clients' Win32 objects are also managed by
	  the Wine server, and the clients must send requests to the
	  Wine server whenever they need to know any Win32 object
	  handle's associated Unix file descriptor (in which case
	  the Wine server duplicates the file descriptor, transmits
	  it back to the client, and leaves it to the client to
	  close the duplicate when the client has finished with
	  it).
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2621"
>Wine builtin DLLs: about Relays, Thunks, and DLL
	  descriptors</A
></H3
><P
>	  This section mainly applies to builtin DLLs (DLLs provided
	  by Wine). See section <A
HREF="#ARCH-DLLS"
>the Section called <I
>Wine/Windows DLLs</I
></A
> for the
	  details on native vs. builtin DLL handling.
	</P
><P
>	  Loading a Windows binary into memory isn't that hard by
	  itself, the hard part is all those various DLLs and entry
	  points it imports and expects to be there and function as
	  expected; this is, obviously, what the entire Wine
	  implementation is all about. Wine contains a range of DLL
	  implementations. You can find the DLLs implementation in the
	  <TT
CLASS="FILENAME"
>dlls/</TT
> directory. 
	</P
><P
>	  Each DLL (at least, the 32 bit version, see below) is
	  implemented in a Unix shared library. The file name of this
	  shared library is the module name of the DLL with a
	  <TT
CLASS="FILENAME"
>.dll.so</TT
> suffix (or
	  <TT
CLASS="FILENAME"
>.drv.so</TT
> or any other relevant extension
	  depending on the DLL type). This shared library contains the
	  code itself for the DLL, as well as some more information,
	  as the DLL resources and a Wine specific DLL descriptor.
	</P
><P
>	  The DLL descriptor, when the DLL is instanciated, is used to
	  create an in-memory PE header, which will provide access to
	  various information about the DLL, including but not limited
	  to its entry point, its resources, its sections, its debug
	  information...
	</P
><P
>	  The DLL descriptor and entry point table is generated by
	  the <B
CLASS="COMMAND"
>winebuild</B
> tool (previously just
	  named <B
CLASS="COMMAND"
>build</B
>), taking DLL specification
	  files with the extension <TT
CLASS="FILENAME"
>.spec</TT
> as
	  input. Resources (after compilation by
	  <B
CLASS="COMMAND"
>wrc</B
>) or message tables (after
	  compilation by <B
CLASS="COMMAND"
>wmc</B
>) are also added to
	  the descriptor by <B
CLASS="COMMAND"
>winebuild</B
>.
	</P
><P
>	  Once an application module wants to import a DLL, Wine
	  will look at:
	  <P
></P
><UL
><LI
><P
>		through its list of registered DLLs (in fact, both
		the already loaded DLLs, and the already loaded
		shared libraries which has registered a DLL
		descriptor). Since, the DLL descriptor is
		automatically registered when the shared library is
		loaded - remember, registration call is put inside a
		shared library constructor - using the
		<CODE
CLASS="ENVAR"
>PRELOAD</CODE
> environment variable when
		running a Wine process can force the registration of
		some DLL descriptors.
	      </P
></LI
><LI
><P
>		If it's not registered, Wine will look for it on
		disk, building the shared library name from the DLL
		module name. Directory searched for are specified by
		the <CODE
CLASS="ENVAR"
>WINEDLLPATH</CODE
> environment variable.
	      </P
></LI
><LI
><P
>		Failing that, it will look for a real Windows
		<TT
CLASS="FILENAME"
>.DLL</TT
> file to use, and look
		through its imports, etc) and use the loading of
		native DLLs.
	      </P
></LI
></UL
>
	</P
><P
>	  After the DLL has been identified (assuming it's still a
	  native one), it's mapped into memory using a
	  <TT
CLASS="FUNCTION"
>dlopen()</TT
> call. Note, that Wine doesn't
	  use the shared library mechanisms for resolving and/or
	  importing functions between two shared libraries (for two
	  DLLs). The shared library is only used for providing a way
	  to load a piece of code on demand. This piece of code,
	  thanks the DLL descriptor, will provide the same type of
	  information a native DLL would. Wine can then use the same
	  code for native and builtin DLL to handle imports/exports.
	</P
><P
>	  Wine also relies on the dynamic loading features of the Unix
	  shared libraries to relocate the DLLs if needed (the same
	  DLL can be loaded at different address in two different
	  processes, and even in two consecutive run of the same
	  executable if the order of loading the DLLs differ). 
	</P
><P
>	  The DLL descriptor is registered in the Wine realm using
	  some tricks. The <B
CLASS="COMMAND"
>winebuild</B
> tool, while
	  creating the code for DLL descriptor, also creates a
	  constructor, that will be called when the shared library is
	  loaded into memory. This constructor will actually register
	  the descriptor to the Wine DLL loader. Hence, before the
	  <TT
CLASS="FUNCTION"
>dlopen</TT
> call returns, the DLL descriptor
	  will be known and registered. This also helps to deal with
	  the cases where there's still dependencies (at the ELF
	  shared lib level, not at the embedded DLL level) between
	  different shared libraries: the embedded DLLs will be
	  properly registered, and even loaded (from a Windows point
	  of view).
	</P
><P
>	  Since Wine is 32-bit code itself, and if the compiler
	  supports Windows' calling convention, <SPAN
CLASS="TYPE"
>stdcall</SPAN
>
	  (<B
CLASS="COMMAND"
>gcc</B
> does), Wine can resolve imports
	  into Win32 code by substituting the addresses of the Wine
	  handlers directly without any thunking layer in
	  between. This eliminates the overhead most people
	  associate with "emulation", and is what the applications
	  expect anyway.
	</P
><P
>	  However, if the user specified <TT
CLASS="PARAMETER"
><I
>WINEDEBUG=+relay
	  </I
></TT
>, a thunk layer is inserted between the
	  application imports and the Wine handlers (actually the
	  export table of the DLL is modified, and a thunk is
	  inserted in the table); this layer is known as "relay"
	  because all it does is print out the arguments/return
	  values (by using the argument lists in the DLL
	  descriptor's entry point table), then pass the call on,
	  but it's invaluable for debugging misbehaving calls into
	  Wine code. A similar mechanism also exists between Windows
	  DLLs - Wine can optionally insert thunk layers between
	  them, by using <TT
CLASS="PARAMETER"
><I
>WINEDEBUG=+snoop</I
></TT
>,
	  but since no DLL descriptor information exists for
	  non-Wine DLLs, this is less reliable and may lead to
	  crashes.
	</P
><P
>	  For Win16 code, there is no way around thunking - Wine
	  needs to relay between 16-bit and 32-bit code. These
	  thunks switch between the app's 16-bit stack and Wine's
	  32-bit stack, copies and converts arguments as appropriate
	  (an int is 16 bit 16-bit and 32 bits in 32-bit, pointers
	  are segmented in 16 bit (and also near or far) but are 32
	  bit linear values in 32 bit), and handles the Win16
	  mutex. Some finer control can be obtained on the
	  conversion, see <B
CLASS="COMMAND"
>winebuild</B
> reference
	  manual for the details. Suffice to say that the kind of
	  intricate stack content juggling this results in, is not
	  exactly suitable study material for beginners.
	</P
><P
>	  A DLL descriptor is also created for every 16 bit
	  DLL. However, this DLL normally paired with a 32 bit
	  DLL. Either, it's the 16 bit counterpart of the 16 bit DLL
	  (<TT
CLASS="FILENAME"
>KRNL386.EXE</TT
> for 
	  <TT
CLASS="FILENAME"
>KERNEL32</TT
>, <TT
CLASS="FILENAME"
>USER</TT
>
	  for <TT
CLASS="FILENAME"
>USER32</TT
>...), or a 16
	  bit DLL directly linked to a 32 bit DLL (like 
	  <TT
CLASS="FILENAME"
>SYSTEM</TT
> for <TT
CLASS="FILENAME"
>KERNEL32</TT
>,
	  or <TT
CLASS="FILENAME"
>DDEML</TT
> for
	  <TT
CLASS="FILENAME"
>USER32</TT
>). In those cases, the 16 bit
	  descriptor(s) is (are) inserted in the same shared library
	  as the the corresponding 32 bit DLL. Wine will also create
	  symbolic links between kernel32.dll.so and system.dll.so
	  so that loading of either
	  <TT
CLASS="FILENAME"
>KERNEL32.DLL</TT
> or
	  <TT
CLASS="FILENAME"
>SYSTEM.DLL</TT
> will end up on the same
	  shared library.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="ARCH-DLLS"
>Wine/Windows DLLs</A
></H3
><P
>	  This document mainly deals with the status of current DLL
	  support by Wine.  The Wine ini file currently supports
	  settings to change the load order of DLLs.  The load order
	  depends on several issues, which results in different settings
	  for various DLLs.
	</P
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2677"
>Pros of Native DLLs</A
></H4
><P
>	    Native DLLs of course guarantee 100% compatibility for
	    routines they implement. For example, using the native
	    <TT
CLASS="FILENAME"
>USER</TT
> DLL would maintain a virtually
	    perfect and Windows 95-like look for window borders,
	    dialog controls, and so on. Using the built-in Wine
	    version of this library, on the other hand, would produce
	    a display that does not precisely mimic that of Windows
	    95. Such subtle differences can be engendered in other
	    important DLLs, such as the common controls library
	    <TT
CLASS="FILENAME"
>COMMCTRL</TT
> or the common dialogs library
	    <TT
CLASS="FILENAME"
>COMMDLG</TT
>, when built-in Wine DLLs
	    outrank other types in load order.
	  </P
><P
>	    More significant, less aesthetically-oriented problems can
	    result if the built-in Wine version of the
	    <TT
CLASS="FILENAME"
>SHELL</TT
> DLL is loaded before the native
	    version of this library. <TT
CLASS="FILENAME"
>SHELL</TT
>
	    contains routines such as those used by installer utilities
	    to create desktop shortcuts. Some installers might fail when
	    using Wine's built-in <TT
CLASS="FILENAME"
>SHELL</TT
>.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2687"
>Cons of Native DLLs</A
></H4
><P
>	    Not every application performs better under native DLLs. If
	    a library tries to access features of the rest of the system
	    that are not fully implemented in Wine, the native DLL might
	    work much worse than the corresponding built-in one, if at
	    all. For example, the native Windows <TT
CLASS="FILENAME"
>GDI</TT
>
	    library must be paired with a Windows display driver, which
	    of course is not present under Intel Unix and Wine.
	  </P
><P
>	    Finally, occasionally built-in Wine DLLs implement more
	    features than the corresponding native Windows DLLs.
	    Probably the most important example of such behavior is the
	    integration of Wine with X provided by Wine's built-in
	    <TT
CLASS="FILENAME"
>USER</TT
> DLL. Should the native Windows
	    <TT
CLASS="FILENAME"
>USER</TT
> library take load-order
	    precedence, such features as the ability to use the
	    clipboard or drag-and-drop between Wine windows and X
	    windows will be lost.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2694"
>Deciding Between Native and Built-In DLLs</A
></H4
><P
>	    Clearly, there is no one rule-of-thumb regarding which
	    load-order to use. So, you must become familiar with
	    what specific DLLs do and which other DLLs or features 
	    a given library interacts with, and use this information 
	    to make a case-by-case decision.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2697"
>Load Order for DLLs</A
></H4
><P
>	    Using the DLL sections from the wine configuration file, the
	    load order can be tweaked to a high degree. In general it is
	    advised not to change the settings of the configuration
	    file. The default configuration specifies the right load
	    order for the most important DLLs.
	  </P
><P
>	    The default load order follows this algorithm: for all DLLs
	    which have a fully-functional Wine implementation, or where
	    the native DLL is known not to work, the built-in library
	    will be loaded first. In all other cases, the native DLL
	    takes load-order precedence.
	  </P
><P
>	    The <CODE
CLASS="VARNAME"
>DefaultLoadOrder</CODE
> from the
	    [DllDefaults] section specifies for all DLLs which version
	    to try first. See manpage for explanation of the arguments.
	  </P
><P
>	    The [DllOverrides] section deals with DLLs, which need a
	    different-from-default treatment. 
	  </P
><P
>	    The [DllPairs] section is for DLLs, which must be loaded in
	    pairs. In general, these are DLLs for either 16-bit or
	    32-bit applications. In most cases in Windows, the 32-bit
	    version cannot be used without its 16-bit counterpart. For
	    Wine, it is customary that the 16-bit implementations rely
	    on the 32-bit implementations and cast the results back to
	    16-bit arguments. Changing anything in this section is bound
	    to result in errors.
	  </P
><P
>	    For the future, the Wine implementation of Windows DLL seems
	    to head towards unifying the 16 and 32 bit DLLs wherever
	    possible, resulting in larger DLLs.  They are stored in the
	    <TT
CLASS="FILENAME"
>dlls/</TT
> subdirectory using the 32-bit
	    name.
	  </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="ARCH-MEM"
>Memory management</A
></H3
><P
>	  Every Win32 process in Wine has its own dedicated native
	  process on the host system, and therefore its own address
	  space. This section explores the layout of the Windows
	  address space and how it is emulated.
	</P
><P
>	  Firstly, a quick recap of how virtual memory works. Physical
	  memory in RAM chips is split into
	  <I
CLASS="EMPHASIS"
>frames</I
>, and the memory that each
	  process sees is split into <I
CLASS="EMPHASIS"
>pages</I
>. Each
	  process has its own 4 gigabytes of address space (4gig being
	  the maximum space addressable with a 32 bit pointer). Pages
	  can be mapped or unmapped: attempts to access an unmapped
	  page cause an
	  <CODE
CLASS="CONSTANT"
>EXCEPTION_ACCESS_VIOLATION</CODE
> which has
	  the easily recognizable code of
	  <CODE
CLASS="CONSTANT"
>0xC0000005</CODE
>. Any page can be mapped to
	  any frame, therefore you can have multiple addresses which
	  actually "contain" the same memory. Pages can also be mapped
	  to things like files or swap space, in which case accessing
	  that page will cause a disk access to read the contents into
	  a free frame.
	</P
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2715"
>Initial layout (in Windows)</A
></H4
><P
>	    When a Win32 process starts, it does not have a clear
	    address space to use as it pleases. Many pages are already
	    mapped by the operating system. In particular, the EXE
	    file itself and any DLLs it needs are mapped into memory,
	    and space has been reserved for the stack and a couple of
	    heaps (zones used to allocate memory to the app
	    from). Some of these things need to be at a fixed address,
	    and others can be placed anywhere.
	  </P
><P
>	    The EXE file itself is usually mapped at address
	    <CODE
CLASS="CONSTANT"
>0x400000</CODE
> and up: indeed, most EXEs have
	    their relocation records stripped which means they must be
	    loaded at their base address and cannot be loaded at any
	    other address.
	  </P
><P
>	    DLLs are internally much the same as EXE files but they
	    have relocation records, which means that they can be
	    mapped at any address in the address space. Remember we
	    are not dealing with physical memory here, but rather
	    virtual memory which is different for each
	    process. Therefore <TT
CLASS="FILENAME"
>OLEAUT32.DLL</TT
> may
	    be loaded at one address in one process, and a totally
	    different one in another. Ensuring all the functions
	    loaded into memory can find each other is the job of the
	    Windows dynamic linker, which is a part of
	    <TT
CLASS="FILENAME"
>NTDLL</TT
>.
	  </P
><P
>	    So, we have the EXE and its DLLs mapped into memory. Two
	    other very important regions also exist: the stack and the
	    process heap. The process heap is simply the equivalent of
	    the libc <TT
CLASS="FUNCTION"
>malloc</TT
> arena on UNIX: it's a
	    region of memory managed by the OS which
	    <TT
CLASS="FUNCTION"
>malloc</TT
>/<TT
CLASS="FUNCTION"
>HeapAlloc</TT
>
	    partitions and hands out to the application. Windows
	    applications can create several heaps but the process heap
	    always exists.
	  </P
><P
>	    Windows 9x also implements another kind of heap: the
	    shared heap. The shared heap is unusual in that
	    anything allocated from it will be visible in every other
	    process.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN2728"
>Comparison</A
></H4
><P
>	    So far we've assumed the entire 4 gigs of address space is
	    available for the application. In fact that's not so: only
	    the lower 2 gigs are available, the upper 2 gigs are on
	    Windows NT used by the operating system and hold the
	    kernel (from <CODE
CLASS="CONSTANT"
>0x80000000</CODE
>). Why is the
	    kernel mapped into every address space?  Mostly for
	    performance: while it's possible to give the kernel its own
	    address space too - this is what Ingo Molnars 4G/4G VM
	    split patch does for Linux - it requires that every system
	    call into the kernel switches address space. As that is a
	    fairly expensive operation (requires flushing the
	    translation lookaside buffers etc) and syscalls are made
	    frequently it's best avoided by keeping the kernel mapped
	    at a constant position in every processes address space.
	  </P
><P
>	    Basically, the comparison of memory mappings looks as
	    follows:
	    <DIV
CLASS="TABLE"
><A
NAME="AEN2733"
></A
><P
><B
>Table 2. Memory layout (Windows and Wine)</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Address</TH
><TH
>Windows 9x</TH
><TH
>Windows NT</TH
><TH
>Linux</TH
></TR
></THEAD
><TBODY
><TR
><TD
>00000000-7fffffff</TD
><TD
>User</TD
><TD
>User</TD
><TD
>User</TD
></TR
><TR
><TD
>80000000-bfffffff</TD
><TD
>Shared</TD
><TD
>User</TD
><TD
>User</TD
></TR
><TR
><TD
>c0000000-ffffffff</TD
><TD
>Kernel</TD
><TD
>Kernel</TD
><TD
>Kernel</TD
></TR
></TBODY
></TABLE
></DIV
>
	  </P
><P
>	    On Windows 9x, in fact only the upper gigabyte
	    (<CODE
CLASS="CONSTANT"
>0xC0000000</CODE
> and up) is used by the
	    kernel, the region from 2 to 3 gigs is a shared area used
	    for loading system DLLs and for file mappings. The bottom
	    2 gigs on both NT and 9x are available for the programs
	    memory allocation and stack.
	  </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2760"
>Wine drivers</A
></H3
><P
>	  Wine will not allow running native Windows drivers under
	  Unix. This comes mainly because (look at the generic
	  architecture schemas) Wine doesn't implement the kernel
	  features of Windows (kernel here really means the kernel,
	  not the <TT
CLASS="FILENAME"
>KERNEL32</TT
> DLL), but rather
	  sets up a proxy layer on top of the Unix kernel to provide
	  the <TT
CLASS="FILENAME"
>NTDLL</TT
> and
	  <TT
CLASS="FILENAME"
>KERNEL32</TT
> features. This means that
	  Wine doesn't provide the inner infrastructure to run
	  native drivers, either from the Win9x family or from the 
	  NT family.
	</P
><P
>	  In other words, Wine will only be able to provide access to
	  a specific device, if and only if, 1/ this device is
	  supported in Unix (there is Unix-driver to talk to it), 2/
	  Wine has implemented the proxy code to make the glue between
	  the API of a Windows driver, and the Unix interface of the
	  Unix driver.
	</P
><P
>	  Wine, however, tries to implement in the various DLLs
	  needing to access devices to do it through the standard
	  Windows APIs for device drivers in user space. This is for
	  example the case for the multimedia drivers, where Wine
	  loads Wine builtin DLLs to talk to the OSS interface, or the
	  ALSA interface. Those DLLs implement the same interface as
	  any user space audio driver in Windows.
	</P
></DIV
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="AEN2768"
></A
>Kernel modules</H1
><P
>      This section covers the kernel modules.  As already stated, Wine
      implements the NT architecture, hence provides <TT
CLASS="FILENAME"
>NTDLL</TT
>
      for the core kernel functions, and <TT
CLASS="FILENAME"
>KERNEL32</TT
>, which is
      the implementation of the basis of the Win32 subsystem, on top of
      <TT
CLASS="FILENAME"
>NTDLL</TT
>.
    </P
><P
>      This chapter is made of two types of material (depending of their point of
      view).  Some items will be tackled from a global point of view and then,
      when needed, explaining the split of work between
      <TT
CLASS="FILENAME"
>NTDLL</TT
> and <TT
CLASS="FILENAME"
>KERNEL32</TT
>; some others
      will be tackled from a DLL point of view (<TT
CLASS="FILENAME"
>NTDLL</TT
> or
      <TT
CLASS="FILENAME"
>KERNEL32</TT
>).  The choice is made so that the output is
      more readable and understantable.  At least, that's the intend (sigh).
    </P
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="INITIALIZATION"
>The Wine initialization process</A
></H2
><P
>	Wine has a rather complex startup procedure, so unlike many programs the
	best place to begin exploring the code-base is
	<I
CLASS="EMPHASIS"
>not</I
> in fact at the	<TT
CLASS="FUNCTION"
>main()</TT
>
	function but instead at some of the  more straightforward DLLs that
	exist on the periphery such as MSI, the widget library (in
	<TT
CLASS="FILENAME"
>USER</TT
> and <TT
CLASS="FILENAME"
>COMCTL32</TT
>) etc. The
	purpose of this section is to document and explain how Wine starts up
	from the moment the user runs "<B
CLASS="COMMAND"
>wine myprogram.exe</B
>" to
	the point at which <TT
CLASS="FILENAME"
>myprogram</TT
> gets control.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2788"
>First Steps</A
></H3
><P
>	  The actual wine binary that the user runs does not do very much, in
	  fact it is only responsible for checking the threading model in use
	  (NPTL vs LinuxThreads) and then invoking a new binary which performs
	  the next stage in the startup sequence. See the beginning of this
	  chapter for more information on this check and why it's necessary. You
	  can find this code in <TT
CLASS="FILENAME"
>loader/glibc.c</TT
>. The result
	  of this check is an exec of either <B
CLASS="COMMAND"
>wine-pthread</B
> or
	  <B
CLASS="COMMAND"
>wine-kthread</B
>, potentially (on Linux) via the
	  <I
CLASS="EMPHASIS"
>preloader</I
>. We need to use separate binaries here
	  because overriding the native pthreads library requires us to exploit
	  a property of ELF symbol fixup semantics: it's not possible to do this
	  without starting a new process.
	</P
><P
>	  The Wine preloader is found in
	  <TT
CLASS="FILENAME"
>loader/preloader.c</TT
>, and is required in order to
	  impose a Win32 style address space layout upon the newly created Win32
	  process. The details of what this does is covered in the address space
	  layout chapter. The preloader is a statically linked ELF binary which
	  is passed the name of the actual Wine binary to run (either
	  <B
CLASS="COMMAND"
>wine-kthread</B
> or <B
CLASS="COMMAND"
>wine-pthread</B
>)
	  along with the arguments the user passed in from the command line. The
	  preloader is an unusual program: it does not have a
	  <TT
CLASS="FUNCTION"
>main()</TT
> function. In standard ELF applications,
	  the entry point is actually at a symbol named
	  <TT
CLASS="FUNCTION"
>_start()</TT
>: this is provided  by the
	  standard <B
CLASS="COMMAND"
>gcc</B
> infrastructure and normally jumps to
	  <TT
CLASS="FUNCTION"
>__libc_start_main()</TT
> which initializes glibc before
	  passing control to the main function as defined by the programmer.
	</P
><P
>	  The preloader takes control direct from the entry point for a few
	  reasons. Firstly, it is required that glibc is not initialized twice:
	  the result of such behaviour is undefined and subject to change
	  without notice. Secondly, it's possible that as part of initializing
	  glibc, the address space layout could be changed - for instance, any
	  call to <TT
CLASS="FUNCTION"
>malloc()</TT
> will initialize a heap arena
	  which modifies the VM mappings. Finally, glibc does not return to
	  <TT
CLASS="FUNCTION"
>_start()</TT
> at any point, so by reusing it we avoid
	  the need to recreate the ELF bootstrap stack
	  (<CODE
CLASS="VARNAME"
>env</CODE
>, <CODE
CLASS="VARNAME"
>argv</CODE
>, auxiliary array etc).
	</P
><P
>	  The preloader is responsible for two things: protecting important
	  regions of the address space so the dynamic linker does not map shared
	  libraries into them, and once that is done loading the real Wine
	  binary off disk, linking it and starting it up. Normally all this is 
	  automatically by glibc and the kernel but as we intercepted this
	  process by using a static binary it's up to us to restart the
	  process. The bulk of the code in the preloader is about loading
	  <B
CLASS="COMMAND"
>wine-[pk]thread</B
> and
	  <TT
CLASS="FILENAME"
>ld-linux.so.2</TT
> off disk, linking them together,
	  then starting the dynamic linking process.
	</P
><P
>	  One of the last things the preloader does before jumping into the
	  dynamic linker is scan the symbol table of the loaded Wine binary and
	  set the value of a global variable directly: this is a more efficient
	  way of passing information to the main Wine program than flattening
	  the data structures into an environment variable or command line
	  parameter then unpacking it on the other side, but it achieves pretty
	  much the same thing. The global variable set points to the preload
	  descriptor table, which contains the VMA regions protected by the
	  preloader. This allows Wine to unmap them once the dynamic linker has
	  been run, so leaving gaps we can initialize properly later on.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2812"
>Starting the emulator</A
></H3
><P
>	  The process of starting up the emulator itself is mostly one of
	  chaining through various initializer functions defined in the core
	  libraries and DLLs: <TT
CLASS="FILENAME"
>libwine</TT
>, then
	  <TT
CLASS="FILENAME"
>NTDLL</TT
>, then <TT
CLASS="FILENAME"
>KERNEL32</TT
>.
	</P
><P
>	  Both the <B
CLASS="COMMAND"
>wine-pthread</B
> and
	  <B
CLASS="COMMAND"
>wine-kthread</B
> binaries share a common
	  <TT
CLASS="FUNCTION"
>main()</TT
> function, defined in
	  <TT
CLASS="FILENAME"
>loader/main.c</TT
>, so no matter which binary is
	  selected after the preloader has run we start here. This passes the
	  information provided by the preloader into
	  <TT
CLASS="FILENAME"
>libwine</TT
> and then calls
	  <TT
CLASS="FUNCTION"
>wine_init()</TT
>, defined in
	  <TT
CLASS="FILENAME"
>libs/wine/loader.c</TT
>. This is where the emulation
	  really starts: 
	  <TT
CLASS="FUNCTION"
>wine_init()</TT
> can, with the correct preparation,
	  be called from programs other than the wine loader itself.
	</P
><P
>	  <TT
CLASS="FUNCTION"
>wine_init()</TT
> does some very basic setup tasks such
	  as initializing the debugging infrastructure, yet more address space
	  manipulation (see the information on the 4G/4G VM split in the address
	  space chapter), before loading <TT
CLASS="FILENAME"
>NTDLL</TT
> - the core
	  of both Wine and the Windows NT series - and jumping to the
	  <TT
CLASS="FUNCTION"
>__wine_process_init()</TT
> function defined
	  in <TT
CLASS="FILENAME"
>dlls/ntdll/loader.c</TT
>
	</P
><P
>	  This function is responsible for initializing the primary Win32
	  environment. In <TT
CLASS="FUNCTION"
>thread_init()</TT
>, it sets up the
	  TEB, the <B
CLASS="COMMAND"
>wineserver</B
> connection for the main thread
	  and the process heap. See the beginning of this chapter for more
	  information on this.

	</P
><P
>	  Finally, it loads and jumps to
	  <TT
CLASS="FUNCTION"
>__wine_kernel_init()</TT
> in
	  <TT
CLASS="FILENAME"
>KERNEL32.DLL</TT
>: this is defined in
	  <TT
CLASS="FILENAME"
>dlls/kernel32/process.c</TT
>. This is where the bulk
	  of the work is done. The <TT
CLASS="FILENAME"
>KERNEL32</TT
> initialization
	  code  retrieves the startup info for the process from the server,
	  initializes the registry, sets up the drive mapping system and locale
	  data, then begins loading the requested application itself. Each
	  process has a <CODE
CLASS="STRUCTNAME"
>STARTUPINFO</CODE
> block that can be
	  passed into <TT
CLASS="FUNCTION"
>CreateProcess</TT
> specifying various
	  things like how the first window should be displayed: this is sent to
	  the new process via the <B
CLASS="COMMAND"
>wineserver</B
>.
	</P
><P
>	  After determining the type of file given to Wine by the user (a Win32
	  EXE file, a Win16 EXE, a Winelib app etc), the program is loaded into
	  memory (which may involve loading and initializing other DLLs, the
	  bulk of Wines startup code), before control reaches the end of
	  <TT
CLASS="FUNCTION"
>__wine_kernel_init()</TT
>. This function ends with the
	  new process stack being initialized, and start_process being called on
	  the new stack. Nearly there!
	</P
><P
>	  The final element of initializing Wine is starting the newly loaded
	  program itself. <TT
CLASS="FUNCTION"
>start_process()</TT
> sets up the SEH
	  backstop handler, calls <TT
CLASS="FUNCTION"
>LdrInitializeThunk()</TT
>
	  which performs the last part of the process initialization (such as
	  performing relocations and calling the <TT
CLASS="FUNCTION"
>DllMain()</TT
>
	  with <CODE
CLASS="CONSTANT"
>PROCESS_ATTACH</CODE
>), grabs the entry point of
	  the executable and then on this line:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>ExitProcess( entry( peb ) );
	</PRE
></TD
></TR
></TABLE
><P
>	  ... jumps to the entry point of the program. At this point the users
	  program is running and the API provided by Wine is ready to be
	  used. When entry returns, the <TT
CLASS="FUNCTION"
>ExitProcess()</TT
> API
	  will be used to initialize a graceful shutdown.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN2853"
>Detailed memory management</A
></H2
><P
>	As already explained in previous chapter (see <A
HREF="#ARCH-MEM"
>the Section called <I
>Memory management</I
> in the Chapter called <I
>Overview</I
></A
>
	for the details), Wine creates every 32-bit Windows process in its own
	32 address space. Wine also tries to map at the relevant addresses what
	Windows would do. There are however a few nasty bits to look at.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2857"
>Implementation</A
></H3
><P
>	  Wine (with a bit of black magic) is able to map the main module at
	  it's desired address (likely <CODE
CLASS="CONSTANT"
>0x400000</CODE
>), to create
	  the process heap, its stack (as a Windows executable can ask for a
	  specific stack size), Wine simply use the initial stack of the ELF
	  executable for its initialisation, but creates a new stack (as a Win32
	  one) for the main thread of the executable. Wine also tries to map all
	  native DLLs at their desired address, so that no relocation has to be
	  performed.
	</P
><P
>	  Wine also implements the shared heap so native win9x DLLs can be
	  used. This heap is always created at the
	  <CODE
CLASS="CONSTANT"
>SYSTEM_HEAP_BASE</CODE
> address or
	  <CODE
CLASS="CONSTANT"
>0x80000000</CODE
> and defaults to 16 megabytes in size.
	</P
><P
>	  There are a few other magic locations. The bottom 64k of memory is
	  deliberately left unmapped to catch null pointer dereferences. The
	  region from 64k to 1mb+64k are reserved for DOS compatibility and
	  contain various DOS data structures. Finally, the address space also
	  contains mappings for the Wine binary itself, any native libaries
	  Wine is using, the glibc malloc arena and so on.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="ADDRESS-SPACE"
>Laying out the address space</A
></H3
><P
>	  Up until about the start of 2004, the Linux address space very much
	  resembled the Windows 9x layout: the kernel sat in the top gigabyte,
	  the bottom pages were unmapped to catch null pointer dereferences, and
	  the rest was free. The kernels mmap algorithm was predictable: it
	  would start by mapping files at low addresses and work up from there.
	</P
><P
>	  The development of a series of new low level patches violated many of
	  these assumptions, and resulted in Wine needing to force the Win32
	  address space layout upon the system. This section looks at why and
	  how this is done.
	</P
><P
>	  The exec-shield patch increases security by randomizing the kernels
	  mmap algorithms. Rather than consistently choosing the same addresses
	  given the same sequence of requests, the kernel will now choose
	  randomized addresses. Because the Linux dynamic linker
	  (<TT
CLASS="FILENAME"
>ld-linux.so.2</TT
>) loads DSOs into memory by using
	  mmap, this means that DSOs are no longer loaded at predictable
	  addresses, so making it harder to attack software by using buffer
	  overflows. It also attempts to relocate certain binaries into a
	  special low area of memory known as the ASCII armor so making it
	  harder to jump into them when using string based attacks.
	</P
><P
>	  Prelink is a technology that enhances startup times by precalculating
	  ELF global offset tables then saving the results inside the native
	  binaries themselves. By grid fitting each DSO into the address space,
	  the dynamic linker does not have to perform as many relocations so
	  allowing applications that heavily rely on dynamic linkage to be
	  loaded into memory much quicker. Complex C++ applications such as
	  Mozilla, OpenOffice and KDE can especially benefit from this
	  technique.
	</P
><P
>	  The 4G VM split patch was developed by Ingo Molnar. It gives the Linux
	  kernel its own address space, thereby allowing processes to access the
	  maximum addressable amount of memory on a 32-bit machine: 4
	  gigabytes. It allows people with lots of RAM to fully utilise that in
	  any given process at the cost of performance: the reason behind giving
	  the kernel a part of each processes address space was to avoid the
	  overhead of switching on each syscall.
	</P
><P
>	  Each of these changes alter the address space in a way incompatible
	  with Windows. Prelink and exec-shield mean that the libraries Wine
	  uses can be placed at any point in the address space: typically this
	  meant that a library was sitting in the region that the EXE you wanted
	  to run had to be loaded (remember that unlike DLLs, EXE files cannot
	  be moved around in memory). The 4G VM split means that programs could
	  receive pointers to the top gigabyte of address space which some are
	  not prepared for (they may store extra information in the high bits of
	  a pointer, for instance). In particular, in combination with
	  exec-shield this one is especially deadly as it's possible the process
	  heap could be allocated beyond
	  <CODE
CLASS="CONSTANT"
>ADDRESS_SPACE_LIMIT</CODE
> which causes Wine
	  initialization to fail.
	</P
><P
>	  The solution to these problems is for Wine to reserve particular parts
	  of the address space so that areas that we don't want the system to
	  use will be avoided. We later on (re/de)allocate those areas as
	  needed. One problem is that some of these mappings are put in place
	  automatically by the dynamic linker: for instance any libraries that
	  Wine is linked to (like <TT
CLASS="FILENAME"
>libc</TT
>,
	  <TT
CLASS="FILENAME"
>libwine</TT
>, <TT
CLASS="FILENAME"
>libpthread</TT
> etc)
	  will be mapped into memory before Wine even gets control. In order to
	  solve that, Wine overrides the default ELF initialization sequence at
	  a low level and reserves the needed areas by using direct syscalls
	  into the kernel (ie without linking against any other code to do it)
	  before restarting the standard initialization and letting the dynamic
	  linker continue. This is referred to as the preloader and is found in
	  <TT
CLASS="FILENAME"
>loader/preloader.c</TT
>.
	</P
><P
>	  Once the usual ELF boot sequence has been completed, some native
	  libraries may well have been mapped above the 3gig limit: however,
	  this doesn't matter as 3G is a Windows limit, not a Linux limit. We
	  still have to prevent the system from allocating anything else above
	  there (like the heap or other DLLs) though so Wine performs a binary
	  search over the upper gig of address space in order to iteratively
	  fill in the holes with <CODE
CLASS="CONSTANT"
>MAP_NORESERVE</CODE
> mappings so
	  the address space is allocated but the memory to actually back it is
	  not. This code can be found in
	  <TT
CLASS="FILENAME"
>libs/wine/mmap.c</TT
>:<TT
CLASS="FUNCTION"
>reserve_area</TT
>.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN2884"
>Multi-processing in Wine</A
></H2
><P
>	Let's take a closer look at the way Wine loads and run processes in memory.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2887"
>Starting a process from command line</A
></H3
><P
>	  When starting a Wine process from command line (we'll get later on to
	  the differences between NE, PE and Winelib executables), there are a
	  couple of things Wine need to do first. A first executable is run to
	  check the threading model of the underlying OS (see
	  <A
HREF="#THREADING"
>the Section called <I
>Multi-threading in Wine</I
></A
> for the details) and will start the real
	  Wine loader corresponding to the choosen threading model.
	</P
><P
>	  Then Wine graps a few elements from the Unix world: the environment,
	  the program arguments. Then the <TT
CLASS="FILENAME"
>ntdll.dll.so</TT
> is
	  loaded into memory using the standard shared library dynamic
	  loader. When loaded, <TT
CLASS="FILENAME"
>NTDLL</TT
> will mainly first
	  create a decent Windows environment:
	  <P
></P
><UL
><LI
><P
>		create a PEB (Process Environment Block) and a TEB (Thread
		Environment Block).
	      </P
></LI
><LI
><P
>		set up the connection to the Wine server - and eventually
		launching the Wine server if none runs
	      </P
></LI
><LI
><P
>create the process heap</P
></LI
></UL
>
	</P
><P
>	  Then <TT
CLASS="FILENAME"
>Kernel32</TT
> is loaded (but now using the
	  Windows dynamic loading capabilities) and a Wine specific entry point
	  is called <TT
CLASS="FUNCTION"
>__wine_kernel_init</TT
>. This function will
	  actually handle all the logic of the process loading and execution,
	  and will never return from it's call.
	</P
><P
>	  <TT
CLASS="FUNCTION"
>__wine_kernel_init</TT
> will undergo the following
	  tasks:
	  <P
></P
><UL
><LI
><P
>		initialization of program arguments from Unix program arguments
	      </P
></LI
><LI
><P
>lookup of executable in the file system</P
></LI
><LI
><P
>		If the file is not found, then an error is printed and the Wine
		loader stops.
	      </P
></LI
><LI
><P
>		We'll cover the non-PE file type later on, so assume for now
		it's a PE file. The PE module is loaded in memory using the
		same mechanisms as for a native DLLs (mainly mapping the file
		data and code sections into memory, and handling relocation if
		needed). Note that the dependencies on the module are not
		resolved at this point.
	      </P
></LI
><LI
><P
>		A new stack is created, which size is given in the PE header,
		and this stack is made the one of the running thread (which is
		still the only one in the process). The stack used at startup
		will no longer be used.
	      </P
></LI
><LI
><P
>		Which this new stack,
		<TT
CLASS="FUNCTION"
>ntdll.LdrInitializeThunk</TT
> is called which
		performs the remaining initialization parts, including resolving
		all the DLL imports on the PE module, and doing the init of the
		TLS slots.
	      </P
></LI
><LI
><P
>		Control can now be passed to the <TT
CLASS="FUNCTION"
>EntryPoint</TT
>
		of the PE module, which will let the executable run.
	      </P
></LI
></UL
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2923"
>Creating a child process from a running process</A
></H3
><P
>	  The steps used are closely link to what is done in the previous case.
	</P
><P
>	  There are however a few points to look at a bit more closely. The
	  inner implementation creates the child process using the
	  <TT
CLASS="FUNCTION"
>fork()</TT
> and <TT
CLASS="FUNCTION"
>exec()</TT
>
	  calls. This means that we don't need to check again for the threading
	  model, we can use what the parent (or the grand-parent process...)
	  started from command line has found.
	</P
><P
>	  The Win32 process creation allows to pass a lot of information between
	  the parent and the child. This includes object handles, windows title,
	  console parameters, environment strings... Wine makes use of both the
	  standard Unix inheritance mechanisms (for environment for example) and
	  the Wine server (to pass from parent to child a chunk of data
	  containing the relevant information).
	</P
><P
>	  The previously described loading mechanism will check in the Wine
	  server if such a chunk exists, and, if so, will perform the relevant
	  initialization.
	</P
><P
>	  Some further synchronization is also put in place: a parent will wait
	  until the child has started, or has failed. The Wine server is also
	  used to perform those tasks.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2932"
>Starting a Winelib process</A
></H3
><P
>	  Before going into the gory details, let's first go back to what a
	  Winelib application is. It can be either a regular Unix executable, or
	  a more specific Wine beast. This later form in fact creates two files
	  for a given executable (say <TT
CLASS="FILENAME"
>foo.exe</TT
>). The first
	  one, named <TT
CLASS="FILENAME"
>foo</TT
> will be a symbolic link to the
	  Wine loader (<TT
CLASS="FILENAME"
>wine</TT
>). The second one, named
	  <TT
CLASS="FILENAME"
>foo.exe.so</TT
>, is the equivalent of the
	  <TT
CLASS="FILENAME"
>.dll.so</TT
> files we've already described for
	  DLLs. As in Windows, an executable is, among other things, a module
	  with its import and export information, as any DLL, it makes sense
	  Wine uses the same mechanisms for loading native executables and
	  DLLs.
	</P
><P
>	  When starting a Winelib application from the command line (say with
	  <B
CLASS="COMMAND"
>foo arg1 arg2</B
>), the Unix shell will execute
	  <B
CLASS="COMMAND"
>foo</B
> as a Unix executable. Since this is in fact the
	  Wine loader, Wine will fire up. However, will notice that it hasn't
	  been started as <B
CLASS="COMMAND"
>wine</B
> but as <B
CLASS="COMMAND"
>foo</B
>,
	  and hence, will try to load (using Unix shared library mechanism) the
	  second file <TT
CLASS="FILENAME"
>foo.exe.so</TT
>. Wine will recognize a 32
	  bit module (with its descriptor) embedded in the shared library, and
	  once the shared library loaded, it will proceed the same path as when
	  loading a standard native PE executable.
	</P
><P
>	  Wine needs to implement this second form of executable in order to
	  maintain the order of initialization of some elements in the
	  executable. One particular issue is when dealing with global C++
	  objects. In standard Unix executable, the call of the constructor to
	  such objects is stored in the specific section of the executable
	  (<TT
CLASS="FUNCTION"
>.init</TT
> not to name it). All constructors in this
	  section are called before the <TT
CLASS="FUNCTION"
>main()</TT
> or
	  <TT
CLASS="FUNCTION"
>WinMain</TT
> function is called. Creating a Wine
	  executable using the first form mentionned above will let those
	  constructors being called before Wine gets a chance to initialize
	  itself. So, any constructor using a Windows API will fail, because
	  Wine infrastructure isn't in place. The use of the second form for
	  Winelib executables ensures that we do the initialization using the
	  following steps:
	  <P
></P
><UL
><LI
><P
>		initialize the Wine infrastructure
	      </P
></LI
><LI
><P
>		load the executable into memory
	      </P
></LI
><LI
><P
>		handle the import sections for the executable
	      </P
></LI
><LI
><P
>		call the global object constructors (if any). They now can
		properly call the Windows APIs
	      </P
></LI
><LI
><P
>		call the executable entry point
	      </P
></LI
></UL
>
	</P
><P
>	  The attentive reader would have noted that the resolution of imports
	  for the executable is done, as for a DLL, when the executable/DLL
	  descriptor is registered. However, this is done also by adding a
	  specific constructor in the <TT
CLASS="FUNCTION"
>.init</TT
> section. For
	  the above describe scheme to function properly, this constructor must
	  be the first constructor to be called, before all the other
	  constructors, generated by the executable itself. The Wine build chain
	  takes care of that, and also generating the executable/DLL descriptor
	  for the Winelib executable.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="THREADING"
>Multi-threading in Wine</A
></H2
><P
>	This section will assume you understand the basics of multithreading. If
	not there are plenty of good tutorials available on the net to get you
	started.
      </P
><P
>	Threading in Wine is somewhat complex due to several factors. The first
	is the advanced level of multithreading support provided by Windows -
	there are far more threading related constructs available in Win32 than
	the Linux equivalent (pthreads). The second is the need to be able to
	map Win32 threads to native Linux threads which provides us with
	benefits like having the kernel schedule them without our
	intervention. While it's possible to implement threading entirely
	without kernel support, doing so is not desirable on most platforms that
	Wine runs on.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2967"
>Threading support in Win32</A
></H3
><P
>	  Win32 is an unusually thread friendly API. Not only is it entirely
	  thread safe, but it provides many different facilities for working
	  with threads. These range from the basics such as starting and
	  stopping threads, to the extremely complex such as injecting threads
	  into other processes and COM inter-thread marshalling.
	</P
><P
>	  One of the primary challenges of writing Wine code therefore is
	  ensuring that all our DLLs are thread safe, free of race conditions
	  and so on. This isn't simple - don't be afraid to ask if you aren't
	  sure whether a piece of code is thread safe or not!
	</P
><P
>	  Win32 provides many different ways you can make your code thread safe
	  however the most common are <I
CLASS="EMPHASIS"
>critical section</I
> and
	  the <I
CLASS="EMPHASIS"
>interlocked functions</I
>. Critical sections are
	  a type of mutex designed to protect a geographic area of code. If you
	  don't want multiple threads running in a piece of code at once, you
	  can protect them with calls to
	  <TT
CLASS="FUNCTION"
>EnterCriticalSection()</TT
> and
	  <TT
CLASS="FUNCTION"
>LeaveCriticalSection()</TT
>. The first call to
	  <TT
CLASS="FUNCTION"
>EnterCriticalSection()</TT
> by a thread will lock the
	  section and continue without stopping. If another thread calls it then
	  it will block until the original thread calls
	  <TT
CLASS="FUNCTION"
>LeaveCriticalSection()</TT
> again.
	</P
><P
>	  It is therefore vitally important that if you use critical sections to
	  make some code thread-safe, that you check every possible codepath out
	  of the code to ensure that any held sections are left. Code like this:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>if (res != ERROR_SUCCESS) return res;
	</PRE
></TD
></TR
></TABLE
><P
>	  is extremely suspect in a function that also contains a call to
	  <TT
CLASS="FUNCTION"
>EnterCriticalSection()</TT
>. Be careful.
	</P
><P
>	  If a thread blocks while waiting for another thread to leave a
	  critical section, you will see an error from the
	  <TT
CLASS="FUNCTION"
>RtlpWaitForCriticalSection()</TT
> function, along with
	  a note of which thread is holding the lock. This only appears after a
	  certain timeout, normally a few seconds. It's possible the thread
	  holding the lock is just being really slow which is why Wine won't
	  terminate the app like a non-checked build of Windows would, but the
	  most common cause is that for some reason a thread forgot to call
	  <TT
CLASS="FUNCTION"
>LeaveCriticalSection()</TT
>, or died while holding the
	  lock (perhaps because it was in turn waiting for another lock). This
	  doesn't just happen in Wine code: a deadlock while waiting for a
	  critical section could be due to a bug in the app triggered by a
	  slight difference in the emulation.
	</P
><P
>	  Another popular mechanism available is the use of functions like
	  <TT
CLASS="FUNCTION"
>InterlockedIncrement()</TT
>
	  and <TT
CLASS="FUNCTION"
>InterlockedExchange()</TT
>. These make use of native
	  CPU abilities to execute a single instruction while ensuring any other
	  processors on the system cannot access memory, and allow you to do
	  common operations like add/remove/check a variable in thread-safe code
	  without holding a mutex. These are useful for reference counting
	  especially in free-threaded (thread safe) COM objects.
	</P
><P
>	  Finally, the usage of TLS slots are also popular. TLS stands for
	  thread-local storage, and is a set of slots scoped local to a thread
	  which you can store pointers in. Look on MSDN for the
	  <TT
CLASS="FUNCTION"
>TlsAlloc()</TT
> function to learn more about the Win32
	  implementation of this. Essentially, the contents of a given slot will
	  be different in each thread, so you can use this to store data that is
	  only meaningful in the context of a single thread. On recent versions
	  of Linux the __thread keyword provides a convenient interface to this
	  functionality - a more portable API is exposed in the pthread
	  library. However, these facilities are not used by Wine, rather, we
	  implement Win32 TLS entirely ourselves.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN2990"
>POSIX threading vs. kernel threading</A
></H3
><P
>	  Wine runs in one of two modes: either pthreads (posix threading) or
	  kthreads (kernel threading). This section explains the differences
	  between them. The one that is used is automatically selected on
	  startup by a small test program which then execs the correct binary,
	  either <B
CLASS="COMMAND"
>wine-kthread</B
> or 
	  <B
CLASS="COMMAND"
>wine-pthread</B
>. On NPTL-enabled systems pthreads
	  will be used, and on older non-NPTL systems kthreads is selected.
	</P
><P
>	  Let's start with a bit of history. Back in the dark ages when Wine's
	  threading support was first implemented a problem was faced - Windows
	  had much more capable threading APIs than Linux did. This presented a
	  problem - Wine works either by reimplementing an API entirely or by
	  mapping it onto the underlying systems equivalent. How could Win32
	  threading be implemented using a library which did not have all the
	  needed features? The answer, of course, was that it couldn't be.
	</P
><P
>	  On Linux the pthreads interface is used to start, stop and control
	  threads. The pthreads library in turn is based on top of so-called
	  "kernel threads" which are created using the
	  <TT
CLASS="FUNCTION"
>clone(2)</TT
> syscall. Pthreads provides a nicer (more
	  portable) interface to this functionality and also provides APIs for
	  controlling mutexes. There is a <A
HREF="http://www.llnl.gov/computing/tutorials/pthreads/"
TARGET="_top"
> good
	    tutorial on pthreads</A
> available if you want to learn more.
	</P
><P
>	  As pthreads did not provide the necessary semantics to implement Win32
	  threading, the decision was made to implement Win32 threading on top
	  of the underlying kernel threads by using syscalls like
	  <TT
CLASS="FUNCTION"
>clone()</TT
> directly. This provided maximum
	  flexibility and allowed a correct implementation but caused some bad
	  side effects. Most notably, all the userland Linux APIs assumed that
	  the user was utilising the pthreads library. Some only enabled thread
	  safety when they detected that pthreads was in use - this is true of
	  glibc, for instance. Worse, pthreads and pure kernel threads had
	  strange interactions when run in the same process yet some libraries
	  used by Wine used pthreads internally. Throw in source code porting
	  using Winelib - where you have both UNIX and Win32 code in the same
	  process - and chaos was the result.
	</P
><P
>	  The solution was simple yet ingenious: Wine would provide its own
	  implementation of the pthread library <I
CLASS="EMPHASIS"
>inside</I
> its
	  own binary. Due to the semantics of ELF symbol scoping, this would
	  cause Wine's own implementation to override any implementation loaded
	  later on (like the real libpthread.so). Therefore, any calls to the
	  pthread APIs in external libraries would be linked to Wine's instead
	  of the system's pthreads library, and Wine implemented pthreads by
	  using the standard Windows threading APIs it in turn implemented
	  itself.
	</P
><P
>	  As a result, libraries that only became thread-safe in the presence of
	  a loaded pthreads implementation would now do so, and any external
	  code that used pthreads would actually end up creating Win32 threads
	  that Wine was aware of and controlled. This worked quite nicely for a
	  long time, even though it required doing some extremely un-kosher
	  things like overriding internal libc structures and functions. That
	  is, it worked until NPTL was developed at which point the underlying
	  thread implementation on Linux changed dramatically.
	</P
><P
>	  The fake pthread implementation can be found in
	  <TT
CLASS="FILENAME"
>loader/kthread.c</TT
>, which is used to
	  produce the <B
CLASS="COMMAND"
>wine-kthread</B
> binary. In contrast,
	  <TT
CLASS="FILENAME"
>loader/pthread.c</TT
> produces the
	  <B
CLASS="COMMAND"
>wine-pthread</B
> binary which is used on newer NPTL
	  systems.
	</P
><P
>	  NPTL is a new threading subsystem for Linux that hugely improves its
	  performance and flexibility. By allowing threads to become much more
	  scalable and adding new pthread APIs, NPTL made Linux competitive with
	  Windows in the multi-threaded world. Unfortunately it also broke many
	  assumptions made by Wine (as well as other applications such as the
	  Sun JVM and RealPlayer) in the process.
	</P
><P
>	  There was, however, some good news. NPTL made Linux threading powerful
	  enough that Win32 threads could now be implemented on top of pthreads
	  like any other normal application. There would no longer be problems
	  with mixing win32-kthreads and pthreads created by external libraries,
	  and no need to override glibc internals. As you can see from the
	  relative sizes of the <TT
CLASS="FILENAME"
>loader/kthread.c</TT
> and
	  <TT
CLASS="FILENAME"
>loader/pthread.c</TT
> files, the difference in code
	  complexity is considerable. NPTL also made several other semantic
	  changes to things such as signal delivery so changes were required in
	  many different places in Wine.
	</P
><P
>	  On non-Linux systems the threading interface is typically not powerful
	  enough to replicate the semantics Win32 applications expect and so
	  kthreads with the pthread overrides are used.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3014"
>The Win32 thread environment</A
></H3
><P
>	  All Win32 code, whether from a native EXE/DLL or in Wine  itself,
	  expects certain constructs to be present in its environment. This
	  section explores what those constructs are and how Wine sets them
	  up. The lack of this environment is one thing that makes it hard to
	  use Wine code directly from standard Linux applications - in order to
	  interact with Win32 code a thread must first be
	  "adopted" by Wine.
	</P
><P
>	  The first thing Win32 code requires is the
	  <I
CLASS="EMPHASIS"
>TEB</I
> or "Thread Environment Block". This is an
	  internal (undocumented) Windows structure associated with every thread
	  which stores a variety of things such as TLS slots, a pointer to the
	  threads message queue, the last error code and so on. You can see the
	  definition of the TEB in <TT
CLASS="FILENAME"
>include/thread.h</TT
>, or at
	  least what we know of it so far. Being internal and subject to change,
	  the layout of the TEB has had to be reverse engineered from scratch.
	</P
><P
>	  A pointer to the TEB is stored in the %fs register and can be accessed
	  using <TT
CLASS="FUNCTION"
>NtCurrentTeb()</TT
> from within Wine code. %fs
	  actually stores a selector, and setting it therefore requires
	  modifying the processes local descriptor table (LDT) - the code to do
	  this is in <TT
CLASS="FILENAME"
>lib/wine/ldt.c</TT
>. 
	</P
><P
>	  The TEB is required by nearly all Win32 code run in the Wine
	  environment, as any <B
CLASS="COMMAND"
>wineserver</B
> RPC will use it,
	  which in turn implies that any code which could possibly block for
	  instance by using a critical section) needs it. The TEB also holds the
	  SEH exception handler chain as the first element, so if disassembling
	  you see code like this:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>movl %esp, %fs:0</PRE
></TD
></TR
></TABLE
><P
>	  ... then you are seeing the program set up an SEH handler frame. All
	  threads must have at least one SEH entry, which normally points to the
	  backstop handler which is ultimately responsible for popping up the
	  all-too-familiar  This program has performed an illegal operation and
	  will be terminated" message. On Wine we just drop straight into the
	  debugger. A full description of SEH is out of the scope of this
	  section, however there are some good articles in MSJ if you are
	  interested.
	</P
><P
>	  All Win32-aware threads must have a <B
CLASS="COMMAND"
>wineserver</B
>
	  connection. Many  different APIs require the ability to communicate
	  with the <B
CLASS="COMMAND"
>wineserver</B
>. In turn, the
	  <B
CLASS="COMMAND"
>wineserver</B
> must be aware of Win32 threads in order
	  to be able to accurately report information to other parts of the
	  program and do things like route inter-thread messages, dispatch APCs
	  (asynchronous procedure calls) and so on. Therefore a part of thread
	  initialization is initializing the thread server-side. The result is
	  not only correct information in the server, but a set of file
	  descriptors the thread can use to communicate with the server - the
	  request fd, reply fd and wait fd (used for blocking).
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="SEH"
>Structured Exception Handling</A
></H2
><P
>        Structured Exception Handling (or SEH) is an implementation of
	exceptions inside the Windows core. It allows code written in different
	languages to throw exceptions across DLL boundaries, and Windows reports
	various errors like access violations by throwing them. This section
	looks at how it works, and how it's implemented in Wine.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3034"
>How SEH works</A
></H3
><P
>          SEH is based on embedding
	  <CODE
CLASS="STRUCTNAME"
>EXCEPTION_REGISTRATION_RECORD</CODE
> structures in
	  the stack. Together they form a linked list rooted at offset zero in
	  the TEB (see the threading section if you don't know what this is). A
	  registration record points to a handler function, and when an
	  exception is thrown the handlers are executed in turn. Each handler
	  returns a code, and they can elect to either continue through the
	  handler chain or it can handle the exception and then restart the
	  program. This is referred to as unwinding the stack. After each
	  handler is called it's popped off the chain.
        </P
><P
>          Before the system begins unwinding the stack, it runs vectored
	  handlers. This is an extension to SEH available in Windows XP, and
	  allows registered functions to get a first chance to watch or deal
	  with any exceptions thrown in the entire program, from any thread.
        </P
><P
>          A thrown exception is represented by an
	  <CODE
CLASS="STRUCTNAME"
>EXCEPTION_RECORD</CODE
> structure. It consists of a
	  code, flags, an address and an arbitrary number of <SPAN
CLASS="TYPE"
>DWORD</SPAN
>
	  parameters. Language runtimes can use these parameters to associate
	  language-specific information with the exception.
        </P
><P
>          Exceptions can be triggered by many things. They can be thrown
	  explicitly by using the RaiseException API, or they can be triggered
	  by a crash (ie, translated from a signal). They may be used internally
	  by a language runtime to implement language-specific exceptions. They
	  can also be thrown across DCOM connections.
        </P
><P
>          Visual C++ has various extensions to SEH which it uses to implement,
	  eg, object destruction on stack unwind as well as the ability to throw
	  arbitrary types. The code for this is in
	  <TT
CLASS="FILENAME"
>dlls/msvcrt/except.c</TT
>
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3045"
>Translating signals to exceptions</A
></H3
><P
>          In Windows, compilers are expected to use the system exception
	  interface, and the kernel itself uses the same interface to
	  dynamically insert exceptions into a running program. By contrast on
	  Linux the exception ABI is implemented at the compiler level
          (inside GCC and the linker) and the kernel tells a thread of
          exceptional events by sending <I
CLASS="EMPHASIS"
>signals</I
>. The
	  language runtime may or may not translate these signals into native
	  exceptions, but whatever happens the kernel does not care.
        </P
><P
>          You may think that if an app crashes, it's game over and it really
	  shouldn't matter how Wine handles this. It's what you might
	  intuitively guess, but you'd be wrong. In fact some Windows programs
	  expect to be able to crash themselves and recover later without the
	  user noticing, some contain buggy binary-only components from third
	  parties and use SEH to swallow crashes, and still others execute
	  privileged (kernel-level) instructions and expect it to work. In
	  fact, at least one set of APIs (the <TT
CLASS="FUNCTION"
>IsBad*Ptr()</TT
>
	  series) can only be implemented by performing an operation that may
	  crash and returning <CODE
CLASS="CONSTANT"
>TRUE</CODE
> if it does, and
	  <CODE
CLASS="CONSTANT"
>FALSE</CODE
> if it doesn't! So, Wine needs to not only
	  implement the SEH infrastructure but also translate Unix signals into
	  SEH exceptions.
        </P
><P
>          The code to translate signals into exceptions is a part of
	  <TT
CLASS="FILENAME"
>NTDLL</TT
>, and can be found in
	  <TT
CLASS="FILENAME"
>dlls/ntdll/signal_i386.c</TT
>. This file sets up
	  handlers for various signals during Wine startup, and for the ones
	  that indicate exceptional conditions translates them into
	  exceptions. Some signals are used by Wine internally and have nothing
	  to do with SEH.
        </P
><P
>          Signal handlers in Wine run on their own stack. Each thread has its
	  own signal stack which resides 4k after the TEB. This is important for
	  a couple of reasons. Firstly, because there's no guarantee that the
	  app thread which triggered the signal has enough stack space for the
	  Wine signal handling code. In Windows, if a thread hits the limits of
	  its stack it triggers a fault on the stack guard page. The language
	  runtime can use this to grow the stack if it wants to.

          

          However, because a guard page violation is just a regular segfault to
	  the kernel, that would lead to a nested signal handler and that gets
	  messy really quick so we disallow that in Wine. Secondly, setting up
	  the exception to throw requires modifying the stack of the thread
	  which triggered it, which is quite hard to do when you're still
	  running on it.
        </P
><P
>          Windows exceptions typically contain more information than the Unix
	  standard APIs provide. For instance, a
	  <CODE
CLASS="CONSTANT"
>STATUS_ACCESS_VIOLATION</CODE
> exception
	  (<CODE
CLASS="CONSTANT"
>0xC0000005</CODE
>) structure contains the faulting
	  address, whereas a standard Unix <CODE
CLASS="CONSTANT"
>SIGSEGV</CODE
> just
	  tells the app that it crashed. Usually this information is passed as
	  an extra parameter to the signal handler, however its location and
	  contents vary between kernels (BSD, Solaris, etc). This data is
	  provided in a <CODE
CLASS="STRUCTNAME"
>SIGCONTEXT</CODE
> structure, and on
	  entry to the signal handler it contains the register state of the CPU
	  before the signal was sent. Modifying it will cause the kernel to
	  adjust the context before restarting the thread.
        </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN3062"
>File management</A
></H2
><P
>	With time, Windows API comes closer to the old Unix paradigm "Everything
	is a file".  Therefore, this whole section dedicated to file management
	will cover firstly the file management, but also some other objects like
	directories, and even devices, which are manipulated in Windows in a
	rather coherent way.  We'll see later on some other objects fitting
	(more or less) in this picture (pipes or consoles to name a few).
      </P
><P
>	First of all, Wine, while implementing the file interface from Windows,
	needs to maps a file name (expressed in the Windows world) onto a file
	name in the Unix world.  This encompasses several aspects: how to map
	the file names, how to map access rights (both on files and
	directories), how to map physical devices (hardisks, but also other
	devices - like serial or parallel interfaces - and even VxDs).
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3066"
>Various Windows formats for file names</A
></H3
><P
>	  Let's first review a bit the various forms Windows uses when it comes
	  to file names.
	</P
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3069"
>The DOS inheritance</A
></H4
><P
>	    At the beginning was DOS, where each file has to sit on a drive,
	    called from a single letter.  For separating device names from
	    directory or file names, a ':' was appended to this single letter,
	    hence giving the (in)-famous <TT
CLASS="FILENAME"
>C:</TT
> drive
	    designations.  Another great invention was to use some fixed names
	    for accessing devices: not only where these named fixed, in a way
	    you couldn't change the name if you'd wish to, but also, they were
	    insensible to the location where you were using them.  For example,
	    it's well known that <TT
CLASS="FILENAME"
>COM1</TT
> designates the first
	    serial port, but it's also true that
	    <TT
CLASS="FILENAME"
>c:\foo\bar\com1</TT
> also designates the first
	    serial port.  It's still true today: on XP, you still cannot name a
	    file <TT
CLASS="FILENAME"
>COM1</TT
>, whatever the directory!!!
	  </P
><P
>	    Well later on (with Windows 95), Microsoft decided to overcome some
	    little details in file names: this included being able to get out of
	    the 8+3 format (8 letters for the name, 3 letters for the
	    extension), and so being able to use "long names" (that's the
	    "official" naming; as you can guess, the 8+3 format is a short
	    name), and also to use very strange characters in a file name (like
	    a space, or even a '.').  You could then name a file
	    <TT
CLASS="FILENAME"
>My File V0.1.txt</TT
>, instead of
	    <TT
CLASS="FILENAME"
>myfile01.txt</TT
>.  Just to keep on the fun side of
	    things, for many years the format used on the disk itself for
	    storing the names has been the short name as the real one and to use
	    some tricky aliasing techniques to store the long name.  When some
	    newer disk file systems have been introduced (NTFS with NT), in
	    replacement of the old FAT system (which had little evolved since
	    the first days of DOS), the long name became the real name while the
	    short name took the alias role.
	  </P
><P
>	    Windows also started to support mounting network shares, and see
	    them as they were a local disk (through a specific drive letter).
	    The way it has been done changed along the years, so we won't go
	    into all the details (especially on the DOS and Win9x side).
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3080"
>The NT way</A
></H4
><P
>	    The introduction of NT allowed a deep change in the ways DOS had
	    been handling devices:
	    <P
></P
><UL
><LI
><P
>		  There's no longer a forest of DOS drive letters (even if the
		  <B
CLASS="COMMAND"
>assign</B
> was a way to create symbolic links
		  in the forest), but a single hierarchical space.
		</P
></LI
><LI
><P
>		  This hierarchy includes several distinct elements.  For
		  example, <TT
CLASS="FILENAME"
>\Device\Hardisk0\Partition0</TT
>
		  refers to the first partition on the first physical hard disk
		  of the system.
		</P
></LI
><LI
><P
>		  This hierarchy covers way more than just the files and drives
		  related objects, but most of the objects in the system.  We'll
		  only cover here the file related part.
		</P
></LI
><LI
><P
>		  This hierarchy is not directly accessible for the Win32 API,
		  but only the <TT
CLASS="FILENAME"
>NTDLL</TT
> API.  The Win32 API
		  only allows to manipulate part of this hierarchy (the rest
		  being hidden from the Win32 API). Of course, the part you see
		  from Win32 API looks very similar to the one that DOS
		  provided.
		</P
></LI
><LI
><P
>		  Mounting a disk is performed by creating a symbol link in this
		  hierarchy from <TT
CLASS="FILENAME"
>\Global??\C:</TT
> (the name
		  seen from the Win32 API) to
		  <TT
CLASS="FILENAME"
>\Device\Harddiskvolume1</TT
> which determines
		  the partition on a physical disk where C: is going to be seen.
		</P
></LI
><LI
><P
>		  Network shares are also accessible through a symbol link.
		  However in this case, a symbol link is created from 
		  <TT
CLASS="FILENAME"
>\Global??\UNC\host\share\</TT
> for the share
		  <TT
CLASS="FILENAME"
>share</TT
> on the machine
		  <TT
CLASS="FILENAME"
>host</TT
>) to what's called a network
		  redirector, and which will take care of 1/ the connection to
		  the remote share, 2/ handling with that remote share the rest
		  of the path (after the name of the server, and the name of the
		  share on that server).
		</P
><DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>		    In NT naming convention, <TT
CLASS="FILENAME"
>\Global??</TT
> can
		    also be called <TT
CLASS="FILENAME"
>\??</TT
> to shorten the
		    access.
		  </P
></TD
></TR
></TABLE
></DIV
></LI
></UL
>
	  </P
><P
>	    All of these things, make the NT system pretty much more flexible
	    (you can add new types of filesystems if you want), you provide a
	    unique name space for all objects, and most operations boil down to
	    creating relationship between different objects.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3109"
>Wrap up</A
></H4
><P
>	    Let's end this chapter about files in Windows with a review of the
	    different formats used for file names:
	    <P
></P
><UL
><LI
><P
><TT
CLASS="FILENAME"
>c:\foo\bar</TT
> is a full path.</P
></LI
><LI
><P
>		  <TT
CLASS="FILENAME"
>\foo\bar</TT
> is an absolute path; the full
		  path is created by appending the default drive (ie. the drive
		  of the current directory).
		</P
></LI
><LI
><P
>		  <TT
CLASS="FILENAME"
>bar</TT
> is a relative path; the full path is
		  created by adding the current directory.
		</P
></LI
><LI
><P
>		  <TT
CLASS="FILENAME"
>c:bar</TT
> is a drive relative path.  Note
		  that the case where <TT
CLASS="FILENAME"
>c:</TT
> is the drive of
		  the current directory is rather easy; it's implemented the
		  same way as the case just below (relative path). In the rest
		  of this chapter, drive relative path will only cover the case
		  where the drive in the path isn't the drive of the default
		  directory.  The resolution of this to a full pathname defers
		  according to the version of Windows, and some parameters.
		  Let's take some time browsing through these issues.  On
		  Windows 9x (as well as on DOS), the system maintains a process
		  wide set of default directories per drive.  Hence, in this
		  case, it will resolve <TT
CLASS="FILENAME"
>c:bar</TT
> to the
		  default directory on drive <TT
CLASS="FILENAME"
>c:</TT
> plus file
		  <TT
CLASS="FILENAME"
>bar</TT
>.  Of course, the default per drive
		  directory is updated each time a new current directory is set
		  (only the current directory of the drive specified is
		  modified).  On Windows NT, things differ a bit.  Since NT
		  implements a namespace for file closer to a single tree
		  (instead of 26 drives), having a current directory per drive
		  is a bit ackward.  Hence, Windows NT default behavior is to
		  have only one current directory across all drives (in fact, a
		  current directory expressed in the global tree) - this
		  directory is of course related to a given process -,
		  <TT
CLASS="FILENAME"
>c:bar</TT
> is resolved this way:
		  <P
></P
><UL
><LI
><P
>			If <TT
CLASS="FILENAME"
>c:</TT
> is the drive of the default
			directory, the final path is the current directory plus
			<TT
CLASS="FILENAME"
>bar</TT
>.
		      </P
></LI
><LI
><P
>			Otherwise it's resolved into
			<TT
CLASS="FILENAME"
>c:\bar</TT
>.
		      </P
></LI
><LI
><P
>			In order to bridge the gap between the two
			implementations (Windows 9x and NT), NT adds a bit of
			complexity on the second case.  If the
			<CODE
CLASS="ENVAR"
>=C:</CODE
> environment variable is defined, then
			it's value is used as a default directory for drive
			<TT
CLASS="FILENAME"
>C:</TT
>.  This is handy, for example,
			when writing a DOS shell, where having a current drive
			per drive is still implemented, even on NT.  This
			mechanism (through environment variables) is implemented
			on <B
CLASS="COMMAND"
>CMD.EXE</B
>, where those variables are
			set when you change directories with the
			<B
CLASS="COMMAND"
>cd</B
>.  Since environment variables are
			inherited at process creation, the current directories
			settings are inherited by child processes, hence
			mimicing the behavior of the old DOS shell.  There's no
			mechanism (in <TT
CLASS="FILENAME"
>NTDLL</TT
> or
			<TT
CLASS="FILENAME"
>KERNEL32</TT
>) to set up, when current
			directory changes, the relevant environment variables.
			This behavior is clearly band-aid, not a full featured
			extension of current directory behavior.
		      </P
></LI
></UL
>
		  Wine fully implements all those behaviors (the Windows 9x vs
		  NT ones are triggered by the version flag in Wine).
		</P
></LI
><LI
><P
>		  <TT
CLASS="FILENAME"
>\\host\share</TT
> is <I
CLASS="FIRSTTERM"
>UNC</I
>
		  (Universal Naming Convention) path, ie. represents a file on a
		  remote share.
		</P
></LI
><LI
><P
>		  <TT
CLASS="FILENAME"
>\\.\device</TT
> denotes a physical device
		  installed in the system (as seen from the Win32 subsystem).  A
		  standard NT system will map it to the
		  <TT
CLASS="FILENAME"
>\??\device</TT
> NT path.  Then, as a standard
		  configuration, <TT
CLASS="FILENAME"
>\??\device</TT
> is likely to be
		  a link to in a physical device described and hooked into the
		  <TT
CLASS="FILENAME"
>\Device\</TT
> tree.  For example,
		  <TT
CLASS="FILENAME"
>COM1</TT
> is a link to
		  <TT
CLASS="FILENAME"
>\Device\Serial0</TT
>.
		</P
></LI
><LI
><P
>		  On some versions of Windows, paths were limited to
		  <CODE
CLASS="CONSTANT"
>MAX_PATH</CODE
> characters.  To circumvent this,
		  Microsoft allowed paths to be <CODE
CLASS="CONSTANT"
>32,767</CODE
>
		  characters long, under the conditions that the path is
		  expressed in Unicode (no Ansi version), and that the path is
		  prefixed with <TT
CLASS="FILENAME"
>\\?\</TT
>.  This convention is
		  applicable to any of the cases described above.
		</P
></LI
></UL
>
	  </P
><P
>	    To summarize, what we've discussed so, let's put everything into a
	    single table...
	    <DIV
CLASS="TABLE"
><A
NAME="AEN3164"
></A
><P
><B
>Table 1. DOS, Win32 and NT paths equivalences</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Type of path</TH
><TH
>Win32 example</TH
><TH
>NT equivalent</TH
><TH
>Rule to construct</TH
></TR
></THEAD
><TBODY
><TR
><TD
>Full path</TD
><TD
><TT
CLASS="FILENAME"
>c:\foo\bar.txt</TT
></TD
><TD
><TT
CLASS="FILENAME"
>\Global??\C:\foo\bar.txt</TT
></TD
><TD
>Simple concatenation</TD
></TR
><TR
><TD
>Absolute path</TD
><TD
><TT
CLASS="FILENAME"
>\foo\bar.txt</TT
></TD
><TD
><TT
CLASS="FILENAME"
>\Global??\J:\foo\bar.txt</TT
></TD
><TD
>		      Simple concatenation using the drive of the default
		      directory (here J:)
		    </TD
></TR
><TR
><TD
>Relative path</TD
><TD
><TT
CLASS="FILENAME"
>gee\bar.txt</TT
></TD
><TD
>		      <TT
CLASS="FILENAME"
>			\Global??\J:\mydir\mysubdir\gee\bar.txt
		      </TT
>
		    </TD
><TD
>		      Simple concatenation using the default directory
		      (here <TT
CLASS="FILENAME"
>J:\mydir\mysubdir</TT
>)
		    </TD
></TR
><TR
><TD
>Drive relative path</TD
><TD
><TT
CLASS="FILENAME"
>j:gee\bar.txt</TT
></TD
><TD
>		      <P
>			  <P
></P
><UL
><LI
><P
>				On Windows 9x (and DOS),
				<TT
CLASS="FILENAME"
>J:\toto\gee\bar.txt</TT
>.
			      </P
></LI
><LI
><P
>				On Windows NT,
				<TT
CLASS="FILENAME"
>J:\gee\bar.txt</TT
>.
			      </P
></LI
><LI
><P
>				On Windows NT,
				<TT
CLASS="FILENAME"
>J:\tata\titi\bar.txt</TT
>.
			      </P
></LI
></UL
>
			</P
>
		    </TD
><TD
>		    <P
>			  <P
></P
><UL
><LI
><P
>				On Windows NT (and DOS),
				<TT
CLASS="FILENAME"
>\toto</TT
> is the default
				directory on drive <TT
CLASS="FILENAME"
>J:</TT
>.
			      </P
></LI
><LI
><P
>				On Windows NT, if <CODE
CLASS="ENVAR"
>=J:</CODE
> isn't set.
			      </P
></LI
><LI
><P
>				On Windows NT, if <CODE
CLASS="ENVAR"
>=J:</CODE
> is set to
				<TT
CLASS="FILENAME"
>J:\tata\titi</TT
>.
			      </P
></LI
></UL
>
			</P
>
		    </TD
></TR
><TR
><TD
>UNC (Uniform Naming Convention) path</TD
><TD
><TT
CLASS="FILENAME"
>\\host\share\foo\bar.txt</TT
></TD
><TD
>		      <TT
CLASS="FILENAME"
>\Global??\UNC\host\share\foo\bar.txt</TT
>
		    </TD
><TD
>		      Simple concatenation.
		    </TD
></TR
><TR
><TD
>Device path</TD
><TD
><TT
CLASS="FILENAME"
>\\.\device</TT
></TD
><TD
><TT
CLASS="FILENAME"
>\Global??\device</TT
></TD
><TD
>Simple concatenation</TD
></TR
><TR
><TD
>Long paths</TD
><TD
><TT
CLASS="FILENAME"
>\\?\...</TT
></TD
><TD
>&nbsp;</TD
><TD
>		      With this prefix, paths can take up to
		      <CODE
CLASS="CONSTANT"
>32,767</CODE
> characters, instead of
		      <CODE
CLASS="CONSTANT"
>MAX_PATH</CODE
> for all the others).  Once
		      the prefix stripped, to be handled like one of the
		      previous ones, just providing internal buffers large
		      enough).
		    </TD
></TR
></TBODY
></TABLE
></DIV
>
	  </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3250"
>Wine implementation</A
></H3
><P
>	  We'll mainly cover in this section the way Wine opens a file (in the
	  Unix sense) when given a Windows file name.  This will include mapping
	  the Windows path onto a Unix path (including the devices case),
	  handling the access rights, the sharing attribute if any...  
	</P
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3253"
>Mapping a Windows path into an absolute Windows path</A
></H4
><P
>	    First of all, we described in previous section the way to convert
	    any path in an absolute path.  Wine implements all the previous algorithms
	    in order to achieve this.  Note also, that this transformation is
	    done with information local to the process (default directory,
	    environment variables...).  We'll assume in the rest of this section
	    that all paths have now been transformed into absolute from.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3256"
>Mapping a Windows (absolute) path onto a Unix path</A
></H4
><P
>	    When Wine is requested to map a path name (in DOS form, with a drive
	    letter, e.g. <TT
CLASS="FILENAME"
>c:\foo\bar\myfile.txt</TT
>), Wine
	    converts this into the following Unix path
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/c:/foo/bar/myfile.txt</TT
>.
	    The Wine configuration process is responsible for setting
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/c:</TT
> to be a symbolic
	    link pointing to the directory in Unix hierarchy the user wants to
	    expose as the <TT
CLASS="FILENAME"
>C:</TT
> drive in the DOS forest of
	    drives.  
	  </P
><P
>	    This scheme allows:
	    <P
></P
><UL
><LI
><P
>		  a very simple algorithm to map a DOS path name into a Unix one
		  (no need of Wine server calls) 
		</P
></LI
><LI
><P
>		  a very configurable implementation: it's very easy to change a
		  drive mapping 
		</P
></LI
><LI
><P
>		  a rather readable configuration: no need of sophisticated
		  tools to read a drive mapping, a <B
CLASS="COMMAND"
>ls -l
		   $(WINEPREFIX)/dosdevices</B
> 
		  says it all.
		</P
></LI
></UL
>
	  </P
><P
>	    This scheme is also used to implement UNC path names.  For example,
	    Wine maps <TT
CLASS="FILENAME"
>\\host\share\foo\bar\MyRemoteFile.txt</TT
>
	    into 
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/unc/host/share/foo/bar/MyRemoteFile.txt</TT
>.
	    It's then up to the user to decide where
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/unc/host/share</TT
> shall
	    point to (or be).  For example, it can either be a symbolic link to a
	    directory inside the local machine (just for emulation purpose), or
	    a symbolic link to the mount point of a remote disk (done through
	    Samba or NFS), or even the real mount point.  Wine will not do any
	    checking here, nor will help in actually mounting the remote drive.
	  </P
><P
>	    We've seen how Wine maps a drive letter or a UNC path onto the Unix
	    hierarchy, we now have to look on a the filename is searched within
	    this hierarchy.  The main issue is about case sensivity.  Here's a
	    reminder of the various properties for the file systems in the
	    field.
	    <DIV
CLASS="TABLE"
><A
NAME="AEN3277"
></A
><P
><B
>Table 2. File systems' properties</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>FS Name</TH
><TH
>Length of elements</TH
><TH
>Case sensitivity (on disk)</TH
><TH
>Case sensitivity for lookup</TH
></TR
></THEAD
><TBODY
><TR
><TD
>FAT, FAT16 or FAT32</TD
><TD
>Short name (8+3)</TD
><TD
>Names are always stored in upper-case</TD
><TD
>Case insensitive</TD
></TR
><TR
><TD
>VFAT</TD
><TD
>Short name (8+3) + alias on long name</TD
><TD
>		      Short names are always stored in upper-case.  Long names
		      are stored with case preservation.
		    </TD
><TD
>Case insensitive</TD
></TR
><TR
><TD
>NTFS</TD
><TD
>Long name + alias on short name (8+3).</TD
><TD
>		      Long names are stored with case preservation.  Short names
		      are always stored in upper-case.
		    </TD
><TD
>Case insentivite</TD
></TR
><TR
><TD
>Linux FS (ext2fs, ext3fs, reiserfs...)</TD
><TD
>Long name</TD
><TD
>Case preserving</TD
><TD
>Case sensitive</TD
></TR
></TBODY
></TABLE
></DIV
>
	  </P
><P
>	    <DIV
CLASS="NOTE"
><P
></P
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="./stylesheet-images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="MIDDLE"
><B
>Case sensitivity vs. preservation</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>		When we say that most systems in NT are case insensitive, this
		has to be understood for looking up for a file, where the
		matches are made in a case insensitive mode.  This is different
		from VFAT or NTFS "case preservation" mechanism, which stores
		the file names as they are given when creating the file, while
		doing case insensitive matches.
	      </P
></TD
></TR
></TABLE
></DIV
>
	    Since most file systems used in NT are case insensitive and since
	    most Unix file systems are case sensitive, Wine undergo a case
	    insensitive search when it has found the Unix path is has to look
	    for.  This means, for example, that for opening the
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/c:/foo/bar/myfile.txt</TT
>,
	    Wine will recursively open all directories in the path, and check,
	    in this order, for the existence of the directory entry in the form
	    given in the file name (ie. case sensitive), and if it's not found,
	    in a case insensitive form.  This allows to also pass, in most Win32
	    file API also a Unix path (instead of a DOS or NT path), but we'll
	    come back to this later.  This also means that the algorithm
	    described doesn't correctly handle the case of two files in the same
	    directory, which names only differ on the case of the letters.  This
	    means, that if, in the same directory, two files (which names match
	    in a case sensitive comparison), Wine will pick-up the right one if
	    the filename given matches on of the name (in a case sensitive way),
	    but will pickup one of the two (without defining the one it's going
	    to pickup) if the filename given matches none of the two names in a
	    case sensitive way (but in a case insensitive way).  For example, if
	    the two filenames are <TT
CLASS="FILENAME"
>my_neat_file.txt</TT
> and
	    <TT
CLASS="FILENAME"
>My_Neat_File.txt</TT
>, Wine's behavior when opening
	    <TT
CLASS="FILENAME"
>MY_neat_FILE.txt</TT
> is undefined.
	  </P
><P
>	    As Windows, at the early days, didn't support the notion of symbolic
	    links on directories, lots of applications (and some old native
	    DLLs) are not ready for this feature.  Mainly, they imply that the
	    directory structure is a tree, which has lots of consequences on
	    navigating in the forest of directories (ie: there cannot be two
	    ways for going from directory to another, there cannot be
	    cycles...).  In order to prevent some bad behavior for such
	    applications, Wine sets up an option.  By default, symbolic links on
	    directories are not followed by Wine.  There's an options to follow
	    them (see the Wine User Guide), but this could be harmful.
	  </P
><P
>	    Wine considers that Unix file names <I
CLASS="EMPHASIS"
>are</I
> long
	    filename.  This seems a reasonable approach; this is also the
	    approach followed by most of the Unix OSes while mounting Windows
	    partitions (with filesystems like FAT, FAT32 or NTFS).  Therefore,
	    Wine tries to support short names the best it can.  Basically, they
	    are two options:
	    <P
></P
><UL
><LI
><P
>		  The filesystem on which the inspected directory lies in a real
		  Windows FS (like FAT, or FAT32, or NTFS) and the OS has
		  support to access the short filename (for example, Linux does
		  this on FAT, FAT32 or VFAT).  In this case, Wine makes full use
		  of this information and really mimics the Windows behavior:
		  the short filename used for any file is the same than on
		  Windows.
		</P
></LI
><LI
><P
>		  If conditions listed above are not met (either, FS has no
		  physical short name support, or OS doesn't provide the access
		  access to the short name), Wine decides and computes on its
		  own the short filename for a given long filename.  We cannot
		  ensure that the generated short name is the same than on
		  Windows (because the algorithm on Windows takes into account
		  the order of creation of files, which cannot be implemented in
		  Wine: Wine would have to cache the short names of every
		  directory it uses!).  The short name is made up of part of the
		  long name (first characters) and the rest with a hashed
		  value.  This has several advantages:
		  <P
></P
><UL
><LI
><P
>			The algorithm is rather simple and low cost.
		      </P
></LI
><LI
><P
>			The algorithm is stateless (doesn't depend of the other
			files in the directory).
		      </P
></LI
></UL
>
		  But, it also has the drawbacks (of the advantages):
		  <P
></P
><UL
><LI
><P
>			The algorithm isn't the same as on Windows, which means
			a program cannot use short names generated on
			Windows.  This could happen when copying an existing
			installed program from Windows (for example, on a dual
			boot machine).
		      </P
></LI
><LI
><P
>			Two long file names can end up with the same short name
			(Windows handles the collision in this case, while Wine
			doesn't).  We rely on our hash algorithm to lower at most
			this possibility (even if it exists).
		      </P
></LI
></UL
>
		</P
></LI
></UL
>
	  </P
><P
>	    Wine also allows in most file API to give as a parameter a full Unix
	    path name.  This is handy when running a Wine (or Winelib) program
	    from the command line, and one doesn't need to convert the path into
	    the Windows form.  However, Wine checks that the Unix path given can
	    be accessed from one of the defined drives, insuring that only part
	    of the Unix <TT
CLASS="FILENAME"
>/</TT
> hierarchy can be accessed.
	  </P
><P
>	    As a side note, as Unix doesn't widely provide a Unicode interface
	    to the filenames, and that Windows implements filenames as Unicode
	    strings (even on the physical layer with NTFS, the FATs variant are
	    ANSI), we need to properly map between the two.  At startup, Wine
	    defines what's called the Unix Code Page, that's is the code page
	    the Unix kernel uses as a reference for the strings.  Then Wine uses
	    this code page for all the mappings it has to do between a Unicode
	    path (on the Windows side) and a Ansi path to be used in a Unix path
	    API.  Note, that this will work as long as a disk isn't mounted with
	    a different code page than the one the kernel uses as a default.
	  </P
><P
>	    We describe below how Windows devices are mapped to Unix devices.  
	    Before that, let's finish the pure file round-up with some basic
	    operations.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3337"
>Access rights and file attributes</A
></H4
><P
>	    Now that we have looked how Wine converts a Windows pathname into a
	    Unix one, we need to cover the various meta-data attached to a file
	    or a directory.
	  </P
><P
>	    In Windows, access rights are simplistic: a file can be read-only or
	    read-write.  Wine sets the read-only flag if the file doesn't have
	    the Unix user-write flag set.  As a matter of fact, there's no way
	    Wine can return that a file cannot be read (that doesn't exist under
	    Windows).  The file will be seen, but trying to open it will return
	    an error.  The Unix exec-flag is never reported.  Wine doesn't use
	    this information to allow/forbid running a new process (as Unix does
	    with the exec-flag).  Last but not least: hidden files.  This exists
	    on Windows but not really on Unix! To be exact, in Windows, the
	    hidden flag is a metadata associated to any file or directoy; in
	    Unix, it's a convention based on the syntax of the file name
	    (whether it starts with a '.' or not).  Wine implements two behaviors
	    (chosen by configuration).  This impacts file names and directory
	    names starting by a '.'.  In first mode
	    (<CODE
CLASS="OPTION"
>ShowDotFile</CODE
> is <CODE
CLASS="CONSTANT"
>FALSE</CODE
>), every 
	    file or directory starting by '.' is returned with the hidden flag
	    turned on.  This is the natural behavior on Unix (for
	    <B
CLASS="COMMAND"
>ls</B
> or even file explorer).  In the second mode
	    (<CODE
CLASS="OPTION"
>ShowDotFile</CODE
> is <CODE
CLASS="CONSTANT"
>TRUE</CODE
>), Wine
	    never sets the hidden flag, hence every file will be seen.
	  </P
><P
>	    Last but not least, before opening a file, Windows makes use of
	    sharing attributes in order to check whether the file can be opened;
	    for example, a process, being the first in the system to open a
	    given file, could forbid, while it maintains the file opened, that
	    another process opens it for write access, whereas open for read
	    access would be granted.  This is fully supported in Wine by moving
	    all those checks in the Wine server for a global view on the system.
	    Note also that what's moved in the Wine server is the check, when
	    the file is opened, to implement the Windows sharing semantics.
	    Further operation on the file (like reading and writing) will not
	    require heavy support from the server.
	  </P
><P
>	    The other good reason for putting the code for actually opening a
	    file in the server is that an opened files in Windows is managed
	    through a handle, and handles can only be created in Wine server!
	  </P
><P
>	    Just a note about attributes on directories: while we can easily map
	    the meaning of Windows' <CODE
CLASS="CONSTANT"
>FILE_ATTRIBUTE_READONLY</CODE
>
	    on a file, we cannot do it for a directory.  Windows' semantic (when
	    this flag is set) means do not delete the directory, while the
	    <CODE
CLASS="CONSTANT"
>w</CODE
> attribute in Unix means don't write nor
	    delete it.  Therefore, Wine uses an asymetric mapping here: if the
	    directory (in Unix) isn't writable, then Wine reports the
	    <CODE
CLASS="CONSTANT"
>FILE_ATTRIBUTE_READONLY</CODE
> attribute; on the other
	    way around, when asked to set a directory with
	    <CODE
CLASS="CONSTANT"
>FILE_ATTRIBUTE_READONLY</CODE
> attribute, Wine simply
	    does nothing.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3353"
>Operations on file</A
></H4
><DIV
CLASS="SECT4"
><H5
CLASS="SECT4"
><A
NAME="AEN3355"
>Reading and writing</A
></H5
><P
>	      Reading and writing are the basic operations on files.  Wine of
	      course implements this, and bases the implementation on client
	      side calls to Unix equivalents (like <TT
CLASS="FUNCTION"
>read()</TT
>
	      or <TT
CLASS="FUNCTION"
>write()</TT
>).  Note, that the Wine server is
	      involved in any read or write operation, as Wine needs to
	      transform the Windows-handle to the file into a Unix file
	      descriptor it can pass to any Unix file function.
	    </P
></DIV
><DIV
CLASS="SECT4"
><HR><H5
CLASS="SECT4"
><A
NAME="AEN3360"
>Getting a Unix fd</A
></H5
><P
>	      This is major operation in any file related operation.  Basically,
	      each file opened (at the Windows level), is first opened in the
	      Wine server, where the fd is stored.  Then, Wine (on client side)
	      uses <TT
CLASS="FUNCTION"
>recvmsg()</TT
> to pass the fd from the wine
	      server process to the client process.  Since this operation could
	      be lengthy, Wine implement some kind of cache mechanism to send it
	      only once, but getting a fd from a handle on a file (or any other
	      Unix object which can be manipulated through a file descriptor)
	      still requires a round trip to the Wine server.
	    </P
></DIV
><DIV
CLASS="SECT4"
><HR><H5
CLASS="SECT4"
><A
NAME="AEN3364"
>Locking</A
></H5
><P
>	      Windows provides file locking capabilities.  When a lock is set
	      (and a lock can be set on any contiguous range in a file), it
	      controls how other processes in the system will have access to the
	      range in the file.  Since locking range on a file are defined on a
	      system wide manner, its implementation resides in
	      <B
CLASS="COMMAND"
>wineserver</B
>.  It tries to make use Unix file
	      locking (if the underlying OS and the mounted disk where the file
	      sits support this feature) with <TT
CLASS="FUNCTION"
>fcntl()</TT
> and
	      the <CODE
CLASS="CONSTANT"
>F_SETLK</CODE
> command.  If this isn't
	      supported, then <B
CLASS="COMMAND"
>wineserver</B
> just pretends it
	      works. 
	    </P
></DIV
><DIV
CLASS="SECT4"
><HR><H5
CLASS="SECT4"
><A
NAME="AEN3371"
>I/O control</A
></H5
><P
>	      There's no need (so far) to implement support (for files and
	      directories) for <TT
CLASS="FUNCTION"
>DeviceIoControl()</TT
>, even if
	      this is supported by Windows, but for very specific needs
	      (like compression management, or file system related information).
	      This isn't the case for devices (including disks), but we'll cover
	      this in the hereafter section related to devices.
	    </P
></DIV
><DIV
CLASS="SECT4"
><HR><H5
CLASS="SECT4"
><A
NAME="AEN3375"
>Buffering</A
></H5
><P
>	      Wine doesn't do any buffering on file accesses but rely on the
	      underlying Unix kernel for that (when possible). This scheme is
	      needed because it's easier to implement multiple accesses on the
	      same file at the kernel level, rather than at Wine levels.  Doing
	      lots of small reads on the same file can turn into a performance
	      hog, because each read operation needs a round trip to the server
	      in order to get a file descriptor (see above).
	    </P
></DIV
><DIV
CLASS="SECT4"
><HR><H5
CLASS="SECT4"
><A
NAME="AEN3378"
>Overlapped I/O</A
></H5
><P
>	      Windows introduced the notion of overlapped I/O.  Basically, it
	      just means that an I/O operation (think read / write to start
	      with) will not wait until it's completed, but rather return to the
	      caller as soon as possible, and let the caller handle the wait
	      operation and determine when the data is ready (for a read
	      operation) or has been sent (for a write operation).  Note that the
	      overlapped operation is linked to a specific thread.
	    </P
><P
>	      There are several interests to this: a server can handle several
	      clients without requiring multi-threading techniques; you can
	      handle an event driven model more easily (ie how to kill properly
	      a server while waiting in the lengthy <TT
CLASS="FUNCTION"
>read()</TT
>
	      operation).
	    </P
><P
>	      Note that Microsoft's support for this feature evolved along the
	      various versions of Windows.  For example, Windows 95 or 98 only
	      supports overlapped I/O for serial and parallel ports, while NT
	      supports also files, disks, sockets, pipes, or mailslots.
	    </P
><P
>	      Wine implements overlapped I/O operations.  This is mainly done by
	      queueing in the server a request that will be triggered when
	      something the current state changes (like data available for a
	      read operation).  This readiness is signaled to the calling
	      processing by queueing a specific APC, which will be called within
	      the next waiting operation the thread will have.  This specific
	      APC will then do the hard work of the I/O operation.  This scheme
	      allows to put in place a wait mechanism, to attach a routine to be
	      called (on the thread context) when the state changes, and to be
	      done is a rather transparent manner (embedded any the generic wait
	      operation).  However, it isn't 100% perfect.  As the heavy
	      operations are done in the context of the calling threads, if
	      those operations are lengthy, there will be an impact on the
	      calling thread, especially its latency.  In order to provide an
	      effective support for this overlapped I/O operations, we would
	      need to rely on Unix kernel features (AIO is a good example).
	    </P
></DIV
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3385"
>Devices &#38; volume management</A
></H4
><P
>	    We've covered so far the ways file names are mapped into Unix
	    paths.  There's still need to cover it for devices.  As a regular
	    file, devices are manipulated in Windows with both read / write
	    operations, but also control mechanisms (speed or parity of a serial
	    line; volume name of a hard disk...).  Since, this is also supported
	    in Linux, there's also a need to open (in a Unix sense) a device
	    when given a Windows device name.  This section applies to DOS device
	    names, which are seen in NT as nicknames to other devices.
	  </P
><P
>	    Firstly, Wine implements the Win32 to NT mapping as described above,
	    hence every device path (in NT sense) is of the following form:
	    <TT
CLASS="FILENAME"
>/??/devicename</TT
> (or
	    <TT
CLASS="FILENAME"
>/DosDevices/devicename</TT
>).  As Windows device
	    names are case insensitive, Wine also converts them to lower case
	    before any operation.  Then, the first operation Wine tries is to
	    check whether
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/devicename</TT
> exists.  If
	    so, it's used as the final Unix path for the device.  The
	    configuration process is in charge of creating for example, a
	    symbolic link between
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/PhysicalDrive0</TT
> and
	    <TT
CLASS="FILENAME"
>/dev/hda0</TT
>.  If such a link cannot be found, and
	    the device name looks like a DOS disk name (like
	    <TT
CLASS="FILENAME"
>C:</TT
>), Wine first tries to get the Unix device
	    from the path <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/c:</TT
>
	    (i.e. the device which is mounted on the target of the symbol link);
	    if this doesn't give a Unix device, Wine tries whether
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/c::</TT
> exists.  If so,
	    it's assumed to be a link to the actual Unix device.  For example,
	    for a CD Rom, <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/e::</TT
>
	    would be a symbolic link to <TT
CLASS="FILENAME"
>/dev/cdrom</TT
>.  If
	    this doesn't exist (we're still handling the a device name of the
	    <TT
CLASS="FILENAME"
>C:</TT
> form), Wine tries to get the Unix device
	    from the system information (<TT
CLASS="FILENAME"
>/etc/mtab</TT
> and
	    <TT
CLASS="FILENAME"
>/etc/fstab</TT
> on Linux).  We cannot apply this
	    method in all the cases, because we have no insurance that the
	    directory can actually be found.  One could have, for example, a CD
	    Rom which he/she want only to use as audio CD player (ie never
	    mounted), thus not having any information of the device itself.  If
	    all of this doesn't work either, some basic operations are checked:
	    if the devicename is <TT
CLASS="FILENAME"
>NUL</TT
>, then
	    <TT
CLASS="FILENAME"
>/dev/null</TT
> is returned.  If the device name is a
	    default serial name (<TT
CLASS="FILENAME"
>COM1</TT
> up to
	    <TT
CLASS="FILENAME"
>COM9</TT
>) (resp.  printer name
	    <TT
CLASS="FILENAME"
>LPT1</TT
> up to <TT
CLASS="FILENAME"
>LPT9</TT
>), then
	    Wine tries to open the Nth serial (resp. printer) in the system.
	    Otherwise, some basic old DOS name support is done
	    <TT
CLASS="FILENAME"
>AUX</TT
> is transformed into
	    <TT
CLASS="FILENAME"
>COM1</TT
> and <TT
CLASS="FILENAME"
>PRN</TT
> into
	    <TT
CLASS="FILENAME"
>LPT1</TT
>), and the whole process is retried with
	    those new names.
	  </P
><P
>	    To sum up:
	    <DIV
CLASS="TABLE"
><A
NAME="AEN3413"
></A
><P
><B
>Table 3. 		Mapping of Windows device names into Unix device names
	      </B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Windows device name</TH
><TH
>NT device name</TH
><TH
>Mapping to Unix device name</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="FILENAME"
>&#60;any_path&#62;AUX</TT
></TD
><TD
><TT
CLASS="FILENAME"
>&#62;\Global??\AUX</TT
></TD
><TD
>		      Treated as an alias to <TT
CLASS="FILENAME"
>COM1</TT
>
		    </TD
></TR
><TR
><TD
><TT
CLASS="FILENAME"
>&#60;any_path&#62;PRN</TT
></TD
><TD
><TT
CLASS="FILENAME"
>\Global??\PRN</TT
></TD
><TD
>Treated as an alias to <TT
CLASS="FILENAME"
>LPT1</TT
></TD
></TR
><TR
><TD
><TT
CLASS="FILENAME"
>&#60;any_path&#62;COM1</TT
></TD
><TD
><TT
CLASS="FILENAME"
>\Global??\COM1</TT
></TD
><TD
>		      <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/com1</TT
>
		      (if the symbol link exists) or the Nth serial
		      line in the system (on Linux,
		      <TT
CLASS="FILENAME"
>/dev/ttyS0</TT
>).
		    </TD
></TR
><TR
><TD
><TT
CLASS="FILENAME"
>&#60;any_path&#62;LPT1</TT
></TD
><TD
><TT
CLASS="FILENAME"
>\Global??\LPT1</TT
></TD
><TD
>		      <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/lpt1</TT
>
		      (if the symbol link exists) or the Nth printer
		      in the system (on Linux,
		      <TT
CLASS="FILENAME"
>/dev/lp0</TT
>).
		    </TD
></TR
><TR
><TD
><TT
CLASS="FILENAME"
>&#60;any_path&#62;NUL</TT
></TD
><TD
><TT
CLASS="FILENAME"
>\Global??\NUL</TT
></TD
><TD
><TT
CLASS="FILENAME"
>/dev/null</TT
></TD
></TR
><TR
><TD
><TT
CLASS="FILENAME"
>\\.\E:</TT
></TD
><TD
><TT
CLASS="FILENAME"
>\Global??\E:</TT
></TD
><TD
>		      <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/e::</TT
> (if the
		      symbolic link exists) or guessing the device from
		      <TT
CLASS="FILENAME"
>/etc/mtab</TT
> or
		      <TT
CLASS="FILENAME"
>/etc/fstab</TT
>.
		    </TD
></TR
><TR
><TD
><TT
CLASS="FILENAME"
>\\.\&#60;device_name&#62;</TT
></TD
><TD
>		      <TT
CLASS="FILENAME"
>\Global??\&#60;device_name&#62;</TT
>
		    </TD
><TD
>		      <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/&#60;device_name&#62;</TT
>
		      (if the symbol link exists).
		    </TD
></TR
></TBODY
></TABLE
></DIV
>
	  </P
><P
>	    Now that we know which Unix device to open for a given Windows
	    device, let's cover the operation on it.  Those operations can either
	    be read / write, io control (and even others).
	  </P
><P
>	    Read and write operations are supported on Real disks &#38; CDROM
	    devices, under several conditions:
	    <P
></P
><UL
><LI
><P
>		  Foremost, as the <TT
CLASS="FUNCTION"
>ReadFile()</TT
> and
		  <TT
CLASS="FUNCTION"
>WriteFile()</TT
> calls are mapped onto the
		  Unix <TT
CLASS="FUNCTION"
>read()</TT
> and
		  <TT
CLASS="FUNCTION"
>write()</TT
> calls, the user (from the Unix
		  perspective of the one running the Wine executable) must have
		  read (resp.  write) access to the device.  It wouldn't be wise
		  to let a user write directly to a hard disk!!!
		</P
></LI
><LI
><P
>		  Blocks' size for read and write but be of the size of a
		  physical block (generally 512 for a hard disk, depends on the
		  type of CD used), and offsets must also be a multiple of the
		  block size.
		</P
></LI
></UL
>
	  </P
><P
>	    Wine also reads (if the first condition above about access rights is
	    met) the volume information from a hard disk or a CD ROM to be
	    displayed to a user.
	  </P
><P
>	    Wine also recognizes VxD as devices.  But those VxD must be the
	    Wine builtin ones (Wine will never allow to load native VxD).  Those
	    are configured with symbolic links in the
	    <TT
CLASS="FILENAME"
>$(WINEPREFIX)/dosdevices/</TT
> directory, and point
	    to the actual builtin DLL.  This DLL exports a single entry point,
	    that Wine will use when a call to
	    <TT
CLASS="FUNCTION"
>DeviceIoControl</TT
> is made, with a handle opened
	    to this VxD.  This allows to provide some kind of compatibility for
	    old Win9x apps, still talking directly to VxD.  This is no longer
	    supported on Windows NT, newest programs are less likely to make use
	    of this feature, so we don't expect lots of development in this
	    area, even though the framework is there and working.  Note also that
	    Wine doesn't provide support for native VxDs (as a game, report how
	    many times this information is written in the documentation;  as an
	    advanced exercise, find how many more occurrences we need in order to
	    stop questions whether it's possible or not).
	  </P
></DIV
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="NTDLL"
><TT
CLASS="FILENAME"
>NTDLL</TT
> module</A
></H2
><P
>	<TT
CLASS="FILENAME"
>NTDLL</TT
> provides most of the services you'd expect
	from a kernel. In lots of cases, <TT
CLASS="FILENAME"
>KERNEL32</TT
> APIs are
	just wrappers to <TT
CLASS="FILENAME"
>NTDLL</TT
> APIs. There are however,
	some difference in the APIs (the <TT
CLASS="FILENAME"
>NTDLL</TT
> ones have
	quite often a bit wider semantics than their
	<TT
CLASS="FILENAME"
>KERNEL32</TT
> counterparts). All the detailed functions
	we've described since the beginning of this chapter are in fact
	implemented in <TT
CLASS="FILENAME"
>NTDLL</TT
>, plus a great numbers of
	others we haven's written about yet.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN3500"
><TT
CLASS="FILENAME"
>KERNEL32</TT
> Module</A
></H2
><P
>	As already explained, <TT
CLASS="FILENAME"
>KERNEL32</TT
> maps quite a few of
	its APIs to <TT
CLASS="FILENAME"
>NTDLL</TT
>. There are however a couple of
	things which are handled directly in
	<TT
CLASS="FILENAME"
>KERNEL32</TT
>. Let's cover a few of them...
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="CONSOLES"
>Console</A
></H3
><DIV
CLASS="SECT3"
><H4
CLASS="SECT3"
><A
NAME="AEN3509"
>NT implementation</A
></H4
><P
>	    Windows implements console solely in the Win32 subsystem.  Under NT,
	    the real implementation uses a dedicated subsystem
	    <TT
CLASS="FILENAME"
>csrss.exe</TT
> Client/Server Run-time SubSystem)
	    which is in charge, amont other things, of animating the consoles.
	    Animating includes for example handling several processes on the
	    same console (write operations must be atomic, but also a character
	    keyed on the console must be read by a single process), or sending
	    some information back to the processes (changing the size or
	    attributes of the console, closing the console).  Windows NT uses a
	    dedicated (RPC based) protocol between each process being attached
	    to a console and the <B
CLASS="COMMAND"
>csrss.exe</B
> subsystem, which
	    is in charge of the UI of every console in the system.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3514"
>Wine implementation</A
></H4
><P
>	    Wine tries to integrate as much as possible into the Unix consoles,
	    but the overall situation isn't perfect yet.  Basically, Wine
	    implements three kinds of consoles:
	    <P
></P
><UL
><LI
><P
>		  the first one is a direct mapping of the Unix console into the
		  Windows environment.  From the windows program point of view,
		  it won't run in a Windows console, but it will see its
		  standard input and output streams redirected to files; thoses
		  files are hooked into the Unix console's output and input
		  streams respectively.  This is handy for running programs from
		  a Unix command line (and use the result of the program as it
		  was a Unix programs), but it lacks all the semantics of the
		  Windows consoles.
		</P
></LI
><LI
><P
>		  the second and third ones are closer to the NT scheme, albeit
		  different from what NT does.  The <B
CLASS="COMMAND"
>wineserver</B
>
		  plays the role of the <TT
CLASS="FILENAME"
>csrss.exe</TT
> subsystem
		  (all requests are sent to it), and are then dispatched to a
		  dedicated wine process, called (surprise!)
		  <B
CLASS="COMMAND"
>wineconsole</B
> which manages the UI of the
		  console.  There is a running instance of
		  <B
CLASS="COMMAND"
>wineconsole</B
> for every console in the
		  system.  Two  flavors of this scheme are actually implemented:
		  they vary on the backend for the
		  <B
CLASS="COMMAND"
>wineconsole</B
>.  The first one,  dubbed
		  <CODE
CLASS="CONSTANT"
>user</CODE
>, creates a real GUI window
		  (hence the USER name) and renders the console in this window.
		  The second one uses the <TT
CLASS="FILENAME"
>(n)curses</TT
> library
		  to take full control of an existing Unix console; of course,
		  interaction with other Unix programs will not be as smooth as
		  the first solution.
		</P
></LI
></UL
>
	  </P
><P
>	    The following table describes the main implementation differences
	    between the three approaches.
	    <DIV
CLASS="TABLE"
><A
NAME="AEN3530"
></A
><P
><B
>Table 4. Function consoles implementation comparison</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Function</TH
><TH
>Bare streams</TH
><TH
>		      <B
CLASS="COMMAND"
>Wineconsole</B
> &#38; user backend
		    </TH
><TH
>		      <B
CLASS="COMMAND"
>Wineconsole</B
> &#38; curses backend
		    </TH
></TR
></THEAD
><TBODY
><TR
><TD
>		      Console as a Win32 Object (and associated handles)
		    </TD
><TD
>		      No specific Win32 object is used in this case.  The
		      handles manipulated for the standard Win32 streams are in
		      fact "bare handles" to their corresponding Unix streams.
		      The mode manipulation functions
		      (<TT
CLASS="FUNCTION"
>GetConsoleMode()</TT
> /
		      <TT
CLASS="FUNCTION"
>SetConsoleMode()</TT
>) are not supported.
		    </TD
><TD
>		      Implemented in server, and a specific Winelib program
		      (<B
CLASS="COMMAND"
>wineconsole</B
>) is in charge of the
		      rendering and user input.  The mode manipulation functions
		      behave as expected.
		    </TD
><TD
>		      Implemented in server, and a specific Winelib program
		      (<B
CLASS="COMMAND"
>wineconsole</B
>) is in charge of the
		      rendering and user input.  The mode manipulation functions
		      behave as expected.
		    </TD
></TR
><TR
><TD
>		      Inheritance (including handling in
		      <TT
CLASS="FUNCTION"
>CreateProcess()</TT
> of
		      <CODE
CLASS="CONSTANT"
>CREATE_DETACHED</CODE
>,
		      <CODE
CLASS="CONSTANT"
>CREATE_NEW_CONSOLE</CODE
> flags).
		    </TD
><TD
>		      Not supported.  Every process child of a process will
		      inherit the Unix streams, so will also inherit the Win32
		      standard streams.
		    </TD
><TD
>		      Fully supported (each new console creation will be handled
		      by the creation of a new <TT
CLASS="FILENAME"
>USER32</TT
> window)
		    </TD
><TD
>		      Fully supported, except for the creation of a new console,
		      which will be rendered on the same Unix terminal as the
		      previous one, leading to unpredictable results.
		    </TD
></TR
><TR
><TD
>		      <TT
CLASS="FUNCTION"
>ReadFile()</TT
> / 
		      <TT
CLASS="FUNCTION"
>WriteFile()</TT
> operations
		    </TD
><TD
>Fully supported</TD
><TD
>Fully supported</TD
><TD
>Fully supported</TD
></TR
><TR
><TD
>		      Screen-buffer manipulation (creation, deletion, resizing...)
		    </TD
><TD
>Not supported</TD
><TD
>Fully supported</TD
><TD
>		      Partly supported (this won't work too well as we don't
		      control (so far) the size of underlying Unix terminal
		    </TD
></TR
><TR
><TD
>		      APIs for reading/writing screen-buffer content, cursor position
		    </TD
><TD
>Not supported</TD
><TD
>Fully supported</TD
><TD
>Fully supported</TD
></TR
><TR
><TD
>APIs for manipulating the rendering window size</TD
><TD
>Not supported</TD
><TD
>Fully supported</TD
><TD
>		      Partly supported (this won't work too well as we don't
		      control (so far) the size of underlying Unix terminal
		    </TD
></TR
><TR
><TD
>		      Signaling (in particular, Ctrl-C handling)
		    </TD
><TD
>		      Nothing is done, which means that Ctrl-C will generate (as
		      usual) a <CODE
CLASS="CONSTANT"
>SIGINT</CODE
> which will terminate
		      the program.
		    </TD
><TD
>		      Partly supported (Ctrl-C behaves as expected, however the
		      other Win32 CUI signaling isn't properly implemented).
		    </TD
><TD
>		      Partly supported (Ctrl-C behaves as expected, however the
		      other Win32 CUI signaling isn't properly implemented).
		    </TD
></TR
></TBODY
></TABLE
></DIV
>
	  </P
><P
>	    The Win32 objects behind a console can be created in several
	    occasions:
	    <P
></P
><UL
><LI
><P
>		  When the program is started from
		  <B
CLASS="COMMAND"
>wineconsole</B
>, a new console object is
		  created and will be used (inherited) by the process launched
		  from <B
CLASS="COMMAND"
>wineconsole</B
>.
		</P
></LI
><LI
><P
>		  When a program, which isn't attached to a console, calls
		  <TT
CLASS="FUNCTION"
>AllocConsole()</TT
>, Wine then launches
		  <B
CLASS="COMMAND"
>wineconsole</B
>, and attaches the current
		  program to this console.  In this mode, the
		  <TT
CLASS="FILENAME"
>USER32</TT
> mode is always selected as Wine
		  cannot tell the current state of the Unix console.
		</P
></LI
></UL
>
	  </P
><P
>	    Please also note, that starting a child process with the
	    <CODE
CLASS="CONSTANT"
>CREATE_NEW_CONSOLE</CODE
> flag, will end-up calling
	    <TT
CLASS="FUNCTION"
>AllocConsole()</TT
> in the child process, hence
	    creating a <B
CLASS="COMMAND"
>wineconsole</B
> with the
	    <TT
CLASS="FILENAME"
>USER32</TT
> backend.
	  </P
><P
>	    Another interesting point to note is that Windows implements handles
	    to console objects (input and screen buffers) only in the
	    <TT
CLASS="FILENAME"
>KERNEL32</TT
> DLL, and those are not sent nor seen
	    from the <TT
CLASS="FILENAME"
>NTDLL</TT
> level, albeit, for example,
	    console are waitable on input.  How is this possible?  Well, Windows
	    NT is a bit tricky here.  Regular handles have an interesting
	    property: their integral value is always a multiple of four (they
	    are likely to be offsets from the beginning of a table).  Console
	    handles, on the other hand, are not multiple of four, but have the
	    two lower bit set (being a multiple of four means having the two
	    lower bits reset).  When <TT
CLASS="FILENAME"
>KERNEL32</TT
> sees a handle
	    with the two lower bits set, it then knows it's a console handle and
	    takes appropriate decisions.  For example, in the various
	    <TT
CLASS="FUNCTION"
>kernel32!WaitFor*()</TT
> functions, it transforms
	    any console handle (input and <I
CLASS="EMPHASIS"
>output</I
> -
	    strangely enough handles to console's screen buffers are waitable)
	    into a dedicated wait event for the targetted console.  There's an
	    (undocumented) <TT
CLASS="FILENAME"
>KERNEL32</TT
> function
	    <TT
CLASS="FUNCTION"
>GetConsoleInputWaitHandle()</TT
> which returns the
	    handle to this event in case you need it.  Another interesting
	    handling of those console's handles is in
	    <TT
CLASS="FUNCTION"
>ReadFile()</TT
>
	    (resp. <TT
CLASS="FUNCTION"
>WriteFile()</TT
>), which behavior, for
	    console's handles, is transferred to
	    <TT
CLASS="FUNCTION"
>ReadConsole()</TT
> (resp.
	    <TT
CLASS="FUNCTION"
>WriteConsole()</TT
>).  Note that's always the ANSI
	    version of
	    <TT
CLASS="FUNCTION"
>ReadConsole()</TT
> /
	    <TT
CLASS="FUNCTION"
>WriteConsole()</TT
>
	    which is called, hence using the default console's code page.  There
	    are some other spots affected, but you can look in
	    <TT
CLASS="FILENAME"
>dlls/kernel</TT
> to find them all.  All of this is
	    implemented in Wine.
	  </P
><P
>	    Wine also implements the same layout of the registry for storing the
	    preferences of the console as Windows does.  Those settings can
	    either be defined globally, or on a per process name basis.
	    <B
CLASS="COMMAND"
>wineconsole</B
> provides the choice to the user to
	    pick you which registry part (global, current running program) it
	    wishes to modify the settings for.
	    <DIV
CLASS="TABLE"
><A
NAME="AEN3621"
></A
><P
><B
>Table 5. Console registry settings</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Name</TH
><TH
>Default value</TH
><TH
>Purpose</TH
></TR
></THEAD
><TBODY
><TR
><TD
>CursorSize</TD
><TD
>25</TD
><TD
>		      Percentage of cell height to which the cursor extents
		    </TD
></TR
><TR
><TD
>CursorVisible</TD
><TD
>1</TD
><TD
>Whether the cursor is visible or not</TD
></TR
><TR
><TD
>EditionMode</TD
><TD
>0</TD
><TD
>		      The way the edition takes place in the console: 0 is
		      insertion mode, 1 is overwrite mode.
		    </TD
></TR
><TR
><TD
>ExitOnDie</TD
><TD
>1</TD
><TD
>		      Whether the console should close itself when last running
		      program attached to it dies
		    </TD
></TR
><TR
><TD
>FaceName</TD
><TD
>No default</TD
><TD
>		      Name of the font to be used for display.  When none is
		      given, <B
CLASS="COMMAND"
>wineconsole</B
> tries its best to
		      pick up a decent font
		    </TD
></TR
><TR
><TD
>FontSize</TD
><TD
>0x0C08</TD
><TD
>		      The high word in the font's cell height, and the low word
		      is the font cell's width.  The default value is 12 pixels
		      in height and 8 pixels in width.
		    </TD
></TR
><TR
><TD
>FontWeight</TD
><TD
>0</TD
><TD
>		      Weigth of the font.  If none is given (or 0)
		      <B
CLASS="COMMAND"
>wineconsole</B
> picks up a decent font size
		    </TD
></TR
><TR
><TD
>HistoryBufferSize</TD
><TD
>50</TD
><TD
>		      Number of entries in history buffer (not actually used)
		    </TD
></TR
><TR
><TD
>HistoryNoDup</TD
><TD
>0</TD
><TD
>		      Whether the history should store twice the same entry
		    </TD
></TR
><TR
><TD
>MenuMask</TD
><TD
>0</TD
><TD
>		      This mask only exists for Wine console handling.  It
		      allows to know which combination of extra keys are need to
		      open the configuration window on right click.  The mask
		      can include <CODE
CLASS="CONSTANT"
>MK_CONTROL</CODE
> or
		      <CODE
CLASS="CONSTANT"
>MK_SHIFT</CODE
> bits.  This can be needed
		      when programs actually need the right click to be passed
		      to them instead of being intercepted by
		      <B
CLASS="COMMAND"
>wineconsole</B
>.
		    </TD
></TR
><TR
><TD
>QuickEdit</TD
><TD
>0</TD
><TD
>		      If null, mouse events are sent to the application.  If non
		      null, mouse events are used to select text on the window.
		      This setting must really be set on a application per
		      application basis, because it deals with the fact the CUI
		      application will use or not the mouse events.
		    </TD
></TR
><TR
><TD
>ScreenBufferSize</TD
><TD
>0x1950</TD
><TD
>		      The high word is the number of font cells in the height of
		      the screen buffer, while the low word is the number of
		      font cells in the width of the screen buffer.
		    </TD
></TR
><TR
><TD
>ScreenColors</TD
><TD
>0x000F</TD
><TD
>		      Default color attribute for the screen buffer (low char is
		      the foreground color, and high char is the background
		      color)
		    </TD
></TR
><TR
><TD
>WindowSize</TD
><TD
>0x1950</TD
><TD
>		      The high word is the number of font cells in the height of
		      the window, while the low word is the number of font cells
		      in the width of the window.  This window is the visible
		      part of the screen buffer: this implies that a screen
		      buffer must always be bigger than its window, and that the
		      screen buffer can be scrolled so that every cell of the
		      screen buffer can be seen in the window.
		    </TD
></TR
></TBODY
></TABLE
></DIV
>
	  </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3691"
>Win16 processes support</A
></H3
><DIV
CLASS="SECT3"
><H4
CLASS="SECT3"
><A
NAME="AEN3693"
>Starting a NE (Win16) process</A
></H4
><P
>	    Wine is also able to run 16 bit processes, but this feature is only
	    supported on Intel IA-32 architectures.
	  </P
><P
>	    When Wine is requested to run a NE (Win 16 process), it will in fact
	    hand over the execution of it to a specific executable
	    <B
CLASS="COMMAND"
>winevdm</B
>. VDM stands for Virtual DOS
	    Machine. This <B
CLASS="COMMAND"
>winevdm</B
> is a Winelib application,
	    but will in fact set up the correct 16 bit environment to run the
	    executable. We will get back later on in details to what this means.
	  </P
><P
>	    Any new 16 bit process created by this executable (or its children)
	    will run into the same <B
CLASS="COMMAND"
>winevdm</B
> instance. Among
	    one instance, several functionalities will be provided to those 16
	    bit processes, including the cooperative multitasking, sharing the
	    same address space, managing the selectors for the 16 bit segments
	    needed for code, data and stack.
	  </P
><P
>	    Note that several <B
CLASS="COMMAND"
>winevdm</B
> instances can run in
	    the same Wine session, but the functionalities described above are
	    only shared among a given instance, not among all the
	    instances. <B
CLASS="COMMAND"
>winevdm</B
> is built as Winelib
	    application, and hence has access to any facility a 32 bit
	    application has.
	  </P
><P
>	    Each Win16 application is implemented in
	    <B
CLASS="COMMAND"
>winevdm</B
> as a Win32
	    thread. <B
CLASS="COMMAND"
>winevdm</B
> then implements its own
	    scheduling facilities (in fact, the code for this feature is in the
	    <TT
CLASS="FILENAME"
>krnl386.exe</TT
> DLL). Since the required Win16
	    scheduling is non pre-emptive, this doesn't require any underlying
	    OS kernel support.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3708"
>SysLevels</A
></H4
><P
>	    SysLevels are an undocumented Windows-internal thread-safety system
	    dedicated to 16 bit applications (or 32 bit applications that call -
	    directly or indirectly - 16 bit code). They are basically critical
	    sections which must be taken in a particular order. The mechanism is
	    generic but there are always three syslevels:
	    <P
></P
><UL
><LI
><P
>level 1 is the Win16 mutex,</P
></LI
><LI
><P
>level 2 is the <TT
CLASS="FILENAME"
>USER</TT
> mutex,</P
></LI
><LI
><P
>level 3 is the <TT
CLASS="FILENAME"
>GDI</TT
> mutex.</P
></LI
></UL
>
	  </P
><P
>	    When entering a syslevel, the code (in
	    <TT
CLASS="FILENAME"
>dlls/kernel/syslevel.c</TT
>) will check that a
	    higher syslevel is not already held and produce an error if so. This
	    is because it's not legal to enter level 2 while holding level 3 -
	    first, you must leave level 3.
	  </P
><P
>	    Throughout the code you may see calls to
	    <TT
CLASS="FUNCTION"
>_ConfirmSysLevel()</TT
> and
	    <TT
CLASS="FUNCTION"
>_CheckNotSysLevel()</TT
>. These functions are
	    essentially assertions about the syslevel states and can be used to
	    check that the rules have not been accidentally violated. In
	    particular, <TT
CLASS="FUNCTION"
>_CheckNotSysLevel()</TT
> will break
	    probably into the debugger) if the check fails. If this happens the
	    solution is to get a backtrace and find out, by reading the source
	    of the wine functions called along the way, how Wine got into the
	    invalid state.
	  </P
></DIV
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3726"
>Memory management</A
></H4
><P
>	    Every Win16 address is expressed in the form of selector:offset. The
	    selector is an entry in the LDT, but a 16 bit entry, limiting each
	    offset to 64 KB. Hence, the maximum available memory to a Win16
	    process is 512 MB. Note, that the processor runs in protected mode,
	    but using 16 bit selectors.
	  </P
><P
>	    Windows, for a 16 bit process, defines a few selectors to access the
	    "real" memory (the one provided) by DOS. Basically, Wine also
	    provides this area of memory.
	  </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3730"
>DOS processes support</A
></H3
><P
>	  The behaviour we just described also applies to DOS executables, which
	  are handled the same way by <B
CLASS="COMMAND"
>winevdm</B
>. This is only
	  supported on Intel IA-32 architectures.
	</P
><P
>	  Wine implements also most of the DOS support in a Wine specific DLL
	  (<TT
CLASS="FILENAME"
>winedos</TT
>). This DLL is called under certain
	  conditions, like:
	  <P
></P
><UL
><LI
><P
>		In <B
CLASS="COMMAND"
>winevdm</B
>, when trying to launch a DOS
		application (<TT
CLASS="FILENAME"
>.EXE</TT
> or
		<TT
CLASS="FILENAME"
>.COM</TT
>, <TT
CLASS="FILENAME"
>.PIF</TT
>).
	      </P
></LI
><LI
><P
>		In <TT
CLASS="FILENAME"
>kernel32</TT
>, when an attempt is made in the
		binary code to call some DOS or BIOS interrupts (like Int 21h
		for example).
	      </P
></LI
></UL
>
	</P
><P
>	  When <B
CLASS="COMMAND"
>winevdm</B
> runs a DOS program, this one runs in
	  real mode (in fact in V86 mode from the IA-32 point of view).
	</P
><P
>	  Wine also supports part of the DPMI (DOS Protected Mode Interface).
	</P
><P
>	  Wine, when running a DOS programs, needs to map the 1 MB of virtual
	  memory to the real memory (as seen by the DOS program). When this is
	  not possible (like when someone else is already using this area), the
	  DOS support is not possible. Not also that by doing so, access to
	  linear address 0 is enabled (as it's also real mode address 0 which is
	  valid). Hence, NULL pointer dereference faults are no longer caught.
	</P
></DIV
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="AEN3750"
></A
>Graphical modules</H1
><DIV
CLASS="SECT1"
><H2
CLASS="SECT1"
><A
NAME="AEN3752"
>GDI Module</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN3754"
>X Windows System interface</A
></H3
><P
>	  The X libraries used to implement X clients (such as Wine)
	  do not work properly if multiple threads access the same
	  display concurrently. It is possible to compile the X
	  libraries to perform their own synchronization (initiated
	  by calling <TT
CLASS="FUNCTION"
>XInitThreads()</TT
>). However,
	  Wine does not use this approach. Instead Wine performs its
	  own synchronization using the
	  <TT
CLASS="FUNCTION"
>wine_tsx11_lock()</TT
> / <TT
CLASS="FUNCTION"
>wine_tsx11_unlock()</TT
>
	  functions.  This locking protects library access
	  with a critical section, and also arranges things so that
	  X libraries compiled without <CODE
CLASS="OPTION"
>-D_REENTRANT</CODE
>
	  (eg. with global <CODE
CLASS="VARNAME"
>errno</CODE
> variable) will
	  work with Wine.
	</P
><P
>	  In the past, all calls to X used to go through a wrapper called
	  <TT
CLASS="FUNCTION"
>TSX...()</TT
> (for "Thread Safe X ...").
	  While it is still being used in the code, it's inefficient
	  as the lock is potentially acquired and released unnecessarily.
	  New code should explicitly acquire the lock.
	</P
></DIV
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="AEN3764"
></A
>Windowing system</H1
><DIV
CLASS="SECT1"
><H2
CLASS="SECT1"
><A
NAME="AEN3766"
>USER Module</A
></H2
><P
>	USER implements windowing and messaging subsystems. It also
	contains code for common controls and for other
	miscellaneous  stuff (rectangles, clipboard, WNet, etc).
	Wine USER code is  located in <TT
CLASS="FILENAME"
>windows/</TT
>,
	<TT
CLASS="FILENAME"
>controls/</TT
>, and
	<TT
CLASS="FILENAME"
>misc/</TT
> directories.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3772"
>Windowing subsystem</A
></H3
><P
><TT
CLASS="FILENAME"
>windows/win.c</TT
></P
><P
><TT
CLASS="FILENAME"
>windows/winpos.c</TT
></P
><P
>	  Windows are arranged into parent/child hierarchy with one
	  common ancestor for all windows (desktop window). Each
	  window structure contains a pointer to the immediate
	  ancestor (parent window if <CODE
CLASS="CONSTANT"
>WS_CHILD</CODE
>
	  style bit is set), a pointer to the sibling (returned by
	  <TT
CLASS="FUNCTION"
>GetWindow(..., GW_NEXT)</TT
>), a pointer
	  to the owner  window (set only for popup window if it was
	  created with valid  <CODE
CLASS="VARNAME"
>hwndParent</CODE
>
	  parameter), and a pointer to the first child window
	  (<TT
CLASS="FUNCTION"
>GetWindow(.., GW_CHILD)</TT
>). All popup
	  and non-child windows are therefore placed in the first
	  level of this hierarchy and their ancestor link
	  (<CODE
CLASS="VARNAME"
>wnd-&#62;parent</CODE
>) points to the desktop
	  window.
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>   Desktop window			- root window
    |     \      `-.
    |      \        `-.
   popup -&#62; wnd1  -&#62;  wnd2		- top level windows    
    |       \   `-.      `-.
    |        \     `-.      `-.
   child1  child2 -&#62; child3  child4     - child windows
	</PRE
></TD
></TR
></TABLE
><P
>	  Horizontal arrows denote sibling relationship, vertical
	  lines - ancestor/child. To summarize, all windows with the
	  same immediate ancestor are sibling windows, all windows
	  which do not have desktop as their immediate ancestor are
	  child windows. Popup windows behave as topmost top-level
	  windows unless they are owned. In this case the only
	  requirement is that they must precede their owners in the
	  top-level sibling list (they are not topmost). Child
	  windows are confined to the client area of their parent
	  windows (client area is where window gets to do its own
	  drawing, non-client area consists of caption, menu,
	  borders, intrinsic scrollbars, and
	  minimize/maximize/close/help buttons). 
	</P
><P
>	  Another fairly important concept is
	  <I
CLASS="FIRSTTERM"
>z-order</I
>. It is derived from the
	  ancestor/child hierarchy and is used to determine
	  "above/below" relationship. For instance, in the example
	  above, z-order is
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>child1-&#62;popup-&#62;child2-&#62;child3-&#62;wnd1-&#62;child4-&#62;wnd2-&#62;desktop.
	</PRE
></TD
></TR
></TABLE
><P
>	  Current active window ("foreground window" in Win32) is
	  moved to the front of z-order unless its top-level
	  ancestor owns popup windows.
	</P
><P
>	  All these issues are dealt with (or supposed to be) in
	  <TT
CLASS="FILENAME"
>windows/winpos.c</TT
> with
	  <TT
CLASS="FUNCTION"
>SetWindowPos()</TT
> being the primary
	  interface to the window manager.
	</P
><P
>	  Wine specifics: in default and managed mode each top-level
	  window gets its own X counterpart with desktop window
	  being basically a fake stub. In desktop mode, however,
	  only desktop window has an X window associated with it.
	  Also, <TT
CLASS="FUNCTION"
>SetWindowPos()</TT
> should
	  eventually be implemented via
	  <TT
CLASS="FUNCTION"
>Begin/End/DeferWindowPos()</TT
> calls and
	  not the other way around.
	</P
><DIV
CLASS="SECT3"
><HR><H4
CLASS="SECT3"
><A
NAME="AEN3796"
>Visible region, clipping region and update region</A
></H4
><P
><TT
CLASS="FILENAME"
>windows/dce.c</TT
></P
><P
><TT
CLASS="FILENAME"
>windows/winpos.c</TT
></P
><P
><TT
CLASS="FILENAME"
>windows/painting.c</TT
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    ________________________
   |_________               |  A and B are child windows of C
   |    A    |______        | 
   |         |      |       |
   |---------'      |       |
   |   |      B     |       |
   |   |            |       |
   |   `------------'       |
   |                   C    |
   `------------------------'
	  </PRE
></TD
></TR
></TABLE
><P
>	    Visible region determines which part of the window is
	    not obscured by other windows. If a window has the
	    <CODE
CLASS="CONSTANT"
>WS_CLIPCHILDREN</CODE
> style then all
	    areas below its children are considered invisible.
	    Similarly, if the <CODE
CLASS="CONSTANT"
>WS_CLIPSIBLINGS</CODE
>
	    bit is in effect then all areas obscured by its siblings
	    are invisible. Child windows are always clipped by the
	    boundaries of their parent windows.
	  </P
><P
>	    B has a <CODE
CLASS="CONSTANT"
>WS_CLIPSIBLINGS</CODE
> style:
	  </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>   .          ______ 
   :         |      |
   |   ,-----'      |
   |   |      B     | - visible region of B
   |   |            |
   :   `------------'
	  </PRE
></TD
></TR
></TABLE
><P
>	    When the program requests a <I
CLASS="FIRSTTERM"
>display
	      context</I
> (DC) for a window it  can specify
	    an optional clipping region that further restricts the
	    area where the graphics output can appear. This area is
	    calculated as an intersection of the visible region and
	    a clipping region. 
	  </P
><P
>	    Program asked for a DC with a clipping region:
	  </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>          ______
      ,--|--.   |     .    ,--. 
   ,--+--'  |   |     :   _:  |
   |  |   B |   |  =&#62; |  |    | - DC region where the painting will
   |  |     |   |     |  |    |   be visible
   `--|-----|---'     :  `----'
      `-----'
	  </PRE
></TD
></TR
></TABLE
><P
>	    When the window manager detects that some part of the window
	    became visible it adds this area to the update region of this
	    window and then generates <CODE
CLASS="CONSTANT"
>WM_ERASEBKGND</CODE
> and
	    <CODE
CLASS="CONSTANT"
>WM_PAINT</CODE
> messages.  In addition,
	    <CODE
CLASS="CONSTANT"
>WM_NCPAINT</CODE
> message is sent when the
	    uncovered area  intersects a nonclient part of the window.
	    Application must reply to the <CODE
CLASS="CONSTANT"
>WM_PAINT</CODE
>
	    message by calling the
	    <TT
CLASS="FUNCTION"
>BeginPaint()</TT
>/<TT
CLASS="FUNCTION"
>EndPaint()</TT
>
	    pair of functions. <TT
CLASS="FUNCTION"
>BeginPaint()</TT
> returns a DC
	    that uses accumulated update region as a clipping region. This
	    operation cleans up invalidated area and the window will not
	    receive another <CODE
CLASS="CONSTANT"
>WM_PAINT</CODE
> until the window
	    manager creates a new update region.
	  </P
><P
>	    A was moved to the left:
	  </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    ________________________       ...          / C update region
   |______                  |     :      .___ /
   | A    |_________        |  =&#62; |   ...|___|..
   |      |         |       |     |   :  |   |
   |------'         |       |     |   :  '---' 
   |   |      B     |       |     |   :      \
   |   |            |       |     :            \
   |   `------------'       |                    B update region
   |                   C    |
   `------------------------'
	  </PRE
></TD
></TR
></TABLE
><P
>	    Windows maintains a display context cache consisting of
	    entries that include the DC itself, the window to which
	    it belongs, and an optional clipping region (visible
	    region is stored in the DC itself). When an API call
	    changes the state of the window tree, window manager has
	    to go through the DC cache to recalculate visible
	    regions for entries whose windows were involved in the
	    operation. DC entries (DCE) can be either private to the
	    window, or private to the window class, or shared
	    between all windows (Windows 3.1 limits the number of
	    shared DCEs to 5).
	  </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3827"
>Messaging subsystem</A
></H3
><P
><TT
CLASS="FILENAME"
>windows/queue.c</TT
></P
><P
><TT
CLASS="FILENAME"
>windows/message.c</TT
></P
><P
>	  Each Windows task/thread has its own message queue - this
	  is where it gets messages from. Messages can be:
	  <P
></P
><OL
TYPE="1"
><LI
><P
>		generated on the fly (<CODE
CLASS="CONSTANT"
>WM_PAINT</CODE
>,
		<CODE
CLASS="CONSTANT"
>WM_NCPAINT</CODE
>,
		<CODE
CLASS="CONSTANT"
>WM_TIMER</CODE
>)
	      </P
></LI
><LI
><P
>		created by the system (hardware messages)
	      </P
></LI
><LI
><P
>		posted by other tasks/threads (<TT
CLASS="FUNCTION"
>PostMessage</TT
>)
	      </P
></LI
><LI
><P
>		sent by other tasks/threads (<TT
CLASS="FUNCTION"
>SendMessage</TT
>)
	      </P
></LI
></OL
>
	</P
><P
>	  Message priority:
	</P
><P
>	  First the system looks for sent messages, then for posted
	  messages, then for hardware messages, then it checks if
	  the queue has the "dirty window" bit set, and, finally, it
	  checks for expired timers. See
	  <TT
CLASS="FILENAME"
>windows/message.c</TT
>.
	</P
><P
>	  From all these different types of messages, only posted
	  messages go directly into the private message queue.
	  System messages (even in Win95) are first collected in the
	  system message queue and then they either sit there until
	  <TT
CLASS="FUNCTION"
>Get/PeekMessage</TT
> gets to process them
	  or, as in Win95, if system queue is getting clobbered, a
	  special thread ("raw input thread") assigns them to the
	  private queues. Sent messages are queued separately and
	  the sender sleeps until it gets a reply. Special messages
	  are generated on the fly depending on the window/queue
	  state. If the window update region is not empty, the
	  system sets the <CODE
CLASS="CONSTANT"
>QS_PAINT</CODE
> bit in the
	  owning queue and eventually this window receives a
	  <CODE
CLASS="CONSTANT"
>WM_PAINT</CODE
> message
	  (<CODE
CLASS="CONSTANT"
>WM_NCPAINT</CODE
> too if the update region
	  intersects with the non-client area). A timer event is
	  raised when one of the queue timers expire. Depending on
	  the timer parameters <TT
CLASS="FUNCTION"
>DispatchMessage</TT
>
	  either calls the callback function or the window
	  procedure. If there are no messages pending the
	  task/thread sleeps until messages appear.
	</P
><P
>	  There are several tricky moments (open for discussion) - 
	</P
><P
></P
><UL
><LI
><P
>	      System message order has to be honored and messages
	      should be processed within correct task/thread
	      context. Therefore when <TT
CLASS="FUNCTION"
>Get/PeekMessage</TT
> encounters
	      unassigned system message and this message appears not
	      to be for the current task/thread it should either
	      skip it (or get rid of it by moving it into the
	      private message queue of the target task/thread -
	      Win95, AFAIK) and look further or roll back and then
	      yield until this message gets processed when system
	      switches to the correct context (Win16). In the first
	      case we lose correct message ordering, in the second
	      case we have the infamous synchronous system message
	      queue. Here is a post to one of the OS/2 newsgroup I
	      found to be relevant:
	    </P
><A
NAME="AEN3862"
></A
><TABLE
BORDER="0"
WIDTH="100%"
CELLSPACING="0"
CELLPADDING="0"
CLASS="BLOCKQUOTE"
><TR
><TD
WIDTH="10%"
VALIGN="TOP"
>&nbsp;</TD
><TD
VALIGN="TOP"
><P
>		" Here's the problem in a nutshell, and there is no
		good solution. Every possible solution creates a
		different problem.
	      </P
><P
>		With a windowing system, events can go to many
		different windows. Most are sent by applications or
		by the OS when things relating to that window happen
		(like repainting, timers, etc.)
	      </P
><P
>		Mouse input events go to the window you click on
		(unless some window captures the mouse).
	      </P
><P
>		So far, no problem.  Whenever an event happens, you
		put a message on the target window's message queue.
		Every process has a message queue.  If the process
		queue fills up, the messages back up onto the system
		queue.
	      </P
><P
>		This is the first cause of apps hanging the GUI.  If
		an app doesn't handle messages and they back up into
		the system queue, other apps can't get any more
		messages.  The reason is that the next message in
		line can't go anywhere, and the system won't skip
		over it.
	      </P
><P
>		This can be fixed by making apps have bigger private
		message queues. The SIQ fix does this.  PMQSIZE does
		this for systems without the SIQ fix.  Applications
		can also request large queues on their own.
	      </P
><P
>		Another source of the problem, however, happens when
		you include keyboard events.  When you press a key,
		there's no easy way to know what window the
		keystroke message should be delivered to.
	      </P
><P
>		Most windowing systems use a concept known as
		"focus".  The window with focus gets all incoming
		keyboard messages.  Focus can be changed from window
		to window by apps or by users clicking on windows.
	      </P
><P
>		This is the second source of the problem.  Suppose
		window A has focus. You click on window B and start
		typing before the window gets focus. Where should
		the keystrokes go?  On the one hand, they should go
		to A until the focus actually changes to B.  On the
		other hand, you probably want the keystrokes to go
		to B, since you clicked there first.
	      </P
><P
>		OS/2's solution is that when a focus-changing event
		happens (like clicking on a window), OS/2 holds all
		messages in the system queue until the focus change
		actually happens.  This way, subsequent keystrokes
		go to the window you clicked on, even if it takes a
		while for that window to get focus.
	      </P
><P
>		The downside is that if the window takes a real long
		time to get focus (maybe it's not handling events,
		or maybe the window losing focus isn't handling
		events), everything backs up in the system queue and
		the system appears hung.
	      </P
><P
>		There are a few solutions to this problem.
	      </P
><P
>		One is to make focus policy asynchronous.  That is,
		focus changing has absolutely nothing to do with the
		keyboard.  If you click on a window and start typing
		before the focus actually changes, the keystrokes go
		to the first window until focus changes, then they
		go to the second. This is what X-windows does.
	      </P
><P
>		Another is what NT does.  When focus changes,
		keyboard events are held in the system message
		queue, but other events are allowed through. This is
		"asynchronous" because the messages in the system
		queue are delivered to the application queues in a
		different order from that with which they were
		posted.  If a bad app won't handle the "lose focus"
		message, it's of no consequence - the app receiving
		focus will get its "gain focus" message, and the
		keystrokes will go to it.
	      </P
><P
>		The NT solution also takes care of the application
		queue filling up problem.  Since the system delivers
		messages asynchronously, messages waiting in the
		system queue will just sit there and the rest of the
		messages will be delivered to their apps.
	      </P
><P
>		The OS/2 SIQ solution is this:  When a
		focus-changing event happens, in addition to
		blocking further messages from the application
		queues, a timer is started.  When the timer goes
		off, if the focus change has not yet happened, the
		bad app has its focus taken away and all messages
		targeted at that window are skipped.  When the bad
		app finally handles the focus change message, OS/2
		will detect this and stop skipping its messages.
	      </P
><P
>		As for the pros and cons:
	      </P
><P
>		The X-windows solution is probably the easiest.  The
		problem is that users generally don't like having to
		wait for the focus to change before they start
		typing.  On many occasions, you can type and the
		characters end up in the wrong window because
		something (usually heavy system load) is preventing
		the focus change from happening in a timely manner.
	      </P
><P
>		The NT solution seems pretty nice, but making the
		system message queue asynchronous can cause similar
		problems to the X-windows problem. Since messages
		can be delivered out of order, programs must not
		assume that two messages posted in a particular
		order will be delivered in that same order.  This
		can break legacy apps, but since Win32 always had an
		asynchronous queue, it is fair to simply tell app
		designers "don't do that".  It's harder to tell app
		designers something like that on OS/2 - they'll
		complain "you changed the rules and our apps are
		breaking."
	      </P
><P
>		The OS/2 solution's problem is that nothing happens
		until you try to change window focus, and then wait
		for the timeout.  Until then, the bad app is not
		detected and nothing is done."
	      </P
></TD
><TD
WIDTH="10%"
VALIGN="TOP"
>&nbsp;</TD
></TR
><TR
><TD
COLSPAN="2"
ALIGN="RIGHT"
VALIGN="TOP"
>--<SPAN
CLASS="ATTRIBUTION"
>by David Charlap</SPAN
></TD
><TD
WIDTH="10%"
>&nbsp;</TD
></TR
></TABLE
></LI
><LI
><P
>	      Intertask/interthread
	      <TT
CLASS="FUNCTION"
>SendMessage</TT
>. The system has to
	      inform the target queue about the forthcoming message,
	      then it has to carry out the context switch and wait
	      until the result is available.  Win16 stores necessary
	      parameters in the queue structure and then calls
	      <TT
CLASS="FUNCTION"
>DirectedYield()</TT
> function.
	      However, in Win32 there could be  several messages
	      pending sent by preemptively executing threads, and in
	      this case <TT
CLASS="FUNCTION"
>SendMessage</TT
> has to
	      build some sort of message queue for sent messages.
	      Another issue is what to do with messages sent to the
	      sender when it is blocked inside its own
	      <TT
CLASS="FUNCTION"
>SendMessage</TT
>. 
	    </P
></LI
></UL
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="ACCEL-IMPL"
>Accelerators</A
></H3
><P
>	  There are <I
CLASS="EMPHASIS"
>three</I
> differently sized
	  accelerator structures exposed to the user: 
	</P
><P
></P
><OL
TYPE="1"
><LI
><P
>	      Accelerators in NE resources.  This is also the internal
	      layout of the global handle <SPAN
CLASS="TYPE"
>HACCEL</SPAN
> (16 and
	      32) in Windows 95 and Wine. Exposed to the user as Win16
	      global handles <SPAN
CLASS="TYPE"
>HACCEL16</SPAN
> and
	      <SPAN
CLASS="TYPE"
>HACCEL32</SPAN
> by the Win16/Win32 API.
	      These are 5 bytes long, with no padding:
	      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>BYTE   fVirt;
WORD   key;
WORD   cmd;
	      </PRE
></TD
></TR
></TABLE
>
	    </P
></LI
><LI
><P
>	      Accelerators in PE resources. They are exposed to the
	      user only by direct accessing PE resources. These have a
	      size of 8 bytes: 
	    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>BYTE   fVirt;
BYTE   pad0;
WORD   key;
WORD   cmd;
WORD   pad1;
	    </PRE
></TD
></TR
></TABLE
></LI
><LI
><P
>	      Accelerators in the Win32 API.  These are exposed to the 
	      user by the  <TT
CLASS="FUNCTION"
>CopyAcceleratorTable</TT
>
	      and  <TT
CLASS="FUNCTION"
>CreateAcceleratorTable</TT
> functions
	      in the Win32 API.
	      These have a size of 6 bytes:
	    </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>BYTE   fVirt;
BYTE   pad0;
WORD   key;
WORD   cmd;
	    </PRE
></TD
></TR
></TABLE
></LI
></OL
><P
>	  Why two types of accelerators in the Win32 API? We can only
	  guess, but my best bet is that the Win32 resource compiler
	  can/does not handle struct packing. Win32 <SPAN
CLASS="TYPE"
>ACCEL</SPAN
>
	  is defined using <TT
CLASS="FUNCTION"
>#pragma(2)</TT
> for the
	  compiler but without any packing for RC, so it will assume
	  <TT
CLASS="FUNCTION"
>#pragma(4)</TT
>.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="AEN3913"
>X Windows System interface</A
></H2
><P
></P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3916"
>Keyboard mapping</A
></H3
><P
>	  Wine now needs to know about your keyboard layout. This
	  requirement comes from a need from many apps to have the
	  correct scancodes available, since they read these directly,
	  instead of just taking the characters returned by the X
	  server. This means that Wine now needs to have a mapping
	  from X keys to the scancodes these programs expect.
	</P
><P
>	  On startup, Wine will try to recognize the active X layout
	  by seeing if it matches any of the defined tables. If it
	  does, everything is alright. If not, you need to define it.
	</P
><P
>	  To do this, open the file
	  <TT
CLASS="FILENAME"
>dlls/x11drv/keyboard.c</TT
> and take a look
	  at the existing tables. Make a backup copy of it, especially
	  if you don't use CVS.
	</P
><P
>	  What you really would need to do, is find out which scancode
	  each key needs to generate.  Find it in the
	  <TT
CLASS="FUNCTION"
>main_key_scan</TT
> table, which looks like
	  this:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>static const int main_key_scan[MAIN_LEN] =
{
/* this is my (102-key) keyboard layout, sorry if it doesn't quite match yours */
0x29,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,
0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x2B,
0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
0x56 /* the 102nd key (actually to the right of l-shift) */
};
	</PRE
></TD
></TR
></TABLE
><P
>	  Next, assign each scancode the characters imprinted on the
	  keycaps. This was done (sort of) for the US 101-key keyboard,
	  which you can find near the top in
	  <TT
CLASS="FILENAME"
>keyboard.c</TT
>. It also shows that if there
	  is no 102nd key, you can skip that.
	</P
><P
>	  However, for most international 102-key keyboards, we have
	  done it easy for you. The scancode layout for these already
	  pretty much matches the physical layout in the
	  <TT
CLASS="FUNCTION"
>main_key_scan</TT
>, so all you need to do is
	  to go through all the keys that generate characters on your
	  main keyboard (except spacebar), and stuff those into an
	  appropriate table. The only exception is that the 102nd key,
	  which is usually to the left of the first key of the last
	  line (usually <B
CLASS="KEYCAP"
>Z</B
>), must be placed on a
	  separate line after the last line.
	</P
><P
>	  After you have written such a table, you need to add it to the
	  <TT
CLASS="FUNCTION"
>main_key_tab[]</TT
> layout index table. This
	  will look like this:
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>static struct {
WORD lang, ansi_codepage, oem_codepage;
const char (*key)[MAIN_LEN][4];
} main_key_tab[]={
...
...
{MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT),  1252, 865, &#38;main_key_NO},
...
	</PRE
></TD
></TR
></TABLE
><P
>	  After you have added your table, recompile Wine and test that
	  it works. If it fails to detect your table, try running
	</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>WINEDEBUG=+key,+keyboard wine &#62; key.log 2&#62;&#38;1
	</PRE
></TD
></TR
></TABLE
><P
>	  and look in the resulting <TT
CLASS="FILENAME"
>key.log</TT
> file to
	  find the error messages it gives for your layout.
	</P
><P
>	  Note that the <CODE
CLASS="CONSTANT"
>LANG_*</CODE
> and
	  <CODE
CLASS="CONSTANT"
>SUBLANG_*</CODE
> definitions are in
	  <TT
CLASS="FILENAME"
>include/winnls.h</TT
>, which you might need
	  to know to find out which numbers your language is assigned,
	  and find it in the WINEDEBUG output. The numbers will be
	  <TT
CLASS="LITERAL"
>(SUBLANG * 0x400 + LANG)</TT
>, so, for example
	  the combination <TT
CLASS="LITERAL"
>LANG_NORWEGIAN (0x14)</TT
> and
	  <TT
CLASS="LITERAL"
>SUBLANG_DEFAULT (0x1)</TT
> will be (in hex)
	  <TT
CLASS="LITERAL"
>14 + 1*400 = 414</TT
>, so since I'm Norwegian,
	  I could look for <TT
CLASS="LITERAL"
>0414</TT
> in the WINEDEBUG
	  output to find out why my keyboard won't detect.
	</P
></DIV
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="OLE"
></A
>COM in Wine</H1
><DIV
CLASS="SECT1"
><H2
CLASS="SECT1"
><A
NAME="COM-WRITING"
>Writing COM Components for Wine</A
></H2
><P
>        This section describes how to create your own natively
        compiled COM components.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3951"
>Macros to define a COM interface</A
></H3
><P
>          The goal of the following set of definitions is to provide a
          way to use the same header file definitions to provide both
          a C interface and a C++ object oriented interface to COM
          interfaces. The type of interface is selected automatically
          depending on the language but it is always possible to get
          the C interface in C++ by defining CINTERFACE.
        </P
><P
>          It is based on the following assumptions:
        </P
><P
></P
><UL
><LI
><P
>              all COM interfaces derive from IUnknown, this should not
              be a problem.
            </P
></LI
><LI
><P
>              the header file only defines the interface, the actual
              fields are defined separately in the C file implementing
              the interface.
            </P
></LI
></UL
><P
>          The natural approach to this problem would be to make sure
          we get a C++ class and virtual methods in C++ and a
          structure with a table of pointer to functions in C.
          Unfortunately the layout of the virtual table is compiler
          specific, the layout of g++ virtual tables is not the same
          as that of an egcs virtual table which is not the same as
          that generated by Visual C++. There are work arounds to make
          the virtual tables compatible via padding but unfortunately
          the one which is imposed to the Wine emulator by the Windows
          binaries, i.e. the Visual C++ one, is the most compact of
          all.
        </P
><P
>          So the solution I finally adopted does not use virtual
          tables. Instead I use in-line non virtual methods that
          dereference the method pointer themselves and perform the
          call.
        </P
><P
>          Let's take Direct3D as an example:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>#define ICOM_INTERFACE IDirect3D
#define IDirect3D_METHODS \
    ICOM_METHOD1(HRESULT,Initialize,    REFIID,) \
    ICOM_METHOD2(HRESULT,EnumDevices,   LPD3DENUMDEVICESCALLBACK,, LPVOID,) \
    ICOM_METHOD2(HRESULT,CreateLight,   LPDIRECT3DLIGHT*,, IUnknown*,) \
    ICOM_METHOD2(HRESULT,CreateMaterial,LPDIRECT3DMATERIAL*,, IUnknown*,) \
    ICOM_METHOD2(HRESULT,CreateViewport,LPDIRECT3DVIEWPORT*,, IUnknown*,) \
    ICOM_METHOD2(HRESULT,FindDevice,    LPD3DFINDDEVICESEARCH,, LPD3DFINDDEVICERESULT,)
#define IDirect3D_IMETHODS \
    IUnknown_IMETHODS \
    IDirect3D_METHODS
ICOM_DEFINE(IDirect3D,IUnknown)
#undef ICOM_INTERFACE

#ifdef ICOM_CINTERFACE
// *** IUnknown methods *** //
#define IDirect3D_QueryInterface(p,a,b) ICOM_CALL2(QueryInterface,p,a,b)
#define IDirect3D_AddRef(p)             ICOM_CALL (AddRef,p)
#define IDirect3D_Release(p)            ICOM_CALL (Release,p)
// *** IDirect3D methods *** //
#define IDirect3D_Initialize(p,a)       ICOM_CALL1(Initialize,p,a)
#define IDirect3D_EnumDevices(p,a,b)    ICOM_CALL2(EnumDevice,p,a,b)
#define IDirect3D_CreateLight(p,a,b)    ICOM_CALL2(CreateLight,p,a,b)
#define IDirect3D_CreateMaterial(p,a,b) ICOM_CALL2(CreateMaterial,p,a,b)
#define IDirect3D_CreateViewport(p,a,b) ICOM_CALL2(CreateViewport,p,a,b)
#define IDirect3D_FindDevice(p,a,b)     ICOM_CALL2(FindDevice,p,a,b)
#endif</PRE
></TD
></TR
></TABLE
><P
>          Comments:
        </P
><P
>          The ICOM_INTERFACE macro is used in the ICOM_METHOD macros
          to define the type of the 'this' pointer. Defining this
          macro here saves us the trouble of having to repeat the
          interface name everywhere. Note however that because of the
          way macros work, a macro like ICOM_METHOD1 cannot use
          'ICOM_INTERFACE##_VTABLE' because this would give
          'ICOM_INTERFACE_VTABLE' and not 'IDirect3D_VTABLE'.
        </P
><P
>          ICOM_METHODS defines the methods specific to this
          interface. It is then aggregated with the inherited methods
          to form ICOM_IMETHODS.
        </P
><P
>          ICOM_IMETHODS defines the list of methods that are
          inheritable from this interface. It must be written manually
          (rather than using a macro to generate the equivalent code)
          to avoid macro recursion (which compilers don't like).
        </P
><P
>          The ICOM_DEFINE finally declares all the structures
          necessary for the interface. We have to explicitly use the
          interface name for macro expansion reasons again.  Inherited
          methods are inherited in C by using the IDirect3D_METHODS
          macro and the parent's Xxx_IMETHODS macro. In C++ we need
          only use the IDirect3D_METHODS since method inheritance is
          taken care of by the language.
        </P
><P
>          In C++ the ICOM_METHOD macros generate a function prototype
          and a call to a function pointer method. This means using
          once 't1 p1, t2 p2, ...' and once 'p1, p2' without the
          types. The only way I found to handle this is to have one
          ICOM_METHOD macro per number of parameters and to have it
          take only the type information (with const if necessary) as
          parameters.  The 'undef ICOM_INTERFACE' is here to remind
          you that using ICOM_INTERFACE in the following macros will
          not work. This time it's because the ICOM_CALL macro
          expansion is done only once the 'IDirect3D_Xxx' macro is
          expanded. And by that time ICOM_INTERFACE will be long gone
          anyway.
        </P
><P
>          You may have noticed the double commas after each parameter
          type. This allows you to put the name of that parameter
          which I think is good for documentation. It is not required
          and since I did not know what to put there for this example
          (I could only find doc about IDirect3D2), I left them blank.
        </P
><P
>          Finally the set of 'IDirect3D_Xxx' macros is a standard set
          of macros defined to ease access to the interface methods in
          C. Unfortunately I don't see any way to avoid having to
          duplicate the inherited method definitions there. This time
          I could have used a trick to use only one macro whatever the
          number of parameters but I preferred to have it work the same
          way as above.
        </P
><P
>          You probably have noticed that we don't define the fields we
          need to actually implement this interface: reference count,
          pointer to other resources and miscellaneous fields. That's
          because these interfaces are just that: interfaces. They may
          be implemented more than once, in different contexts and
          sometimes not even in Wine. Thus it would not make sense to
          impose that the interface contains some specific fields.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3973"
>Bindings in C</A
></H3
><P
>          In C this gives:
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>typedef struct IDirect3DVtbl IDirect3DVtbl;
struct IDirect3D {
    IDirect3DVtbl* lpVtbl;
};
struct IDirect3DVtbl {
    HRESULT (*fnQueryInterface)(IDirect3D* me, REFIID riid, LPVOID* ppvObj);
    ULONG (*fnAddRef)(IDirect3D* me);
    ULONG (*fnRelease)(IDirect3D* me);
    HRESULT (*fnInitialize)(IDirect3D* me, REFIID a);
    HRESULT (*fnEnumDevices)(IDirect3D* me, LPD3DENUMDEVICESCALLBACK a, LPVOID b);
    HRESULT (*fnCreateLight)(IDirect3D* me, LPDIRECT3DLIGHT* a, IUnknown* b);
    HRESULT (*fnCreateMaterial)(IDirect3D* me, LPDIRECT3DMATERIAL* a, IUnknown* b);
    HRESULT (*fnCreateViewport)(IDirect3D* me, LPDIRECT3DVIEWPORT* a, IUnknown* b);
    HRESULT (*fnFindDevice)(IDirect3D* me, LPD3DFINDDEVICESEARCH a, LPD3DFINDDEVICERESULT b);
}; 

#ifdef ICOM_CINTERFACE
// *** IUnknown methods *** //
#define IDirect3D_QueryInterface(p,a,b) (p)-&#62;lpVtbl-&#62;fnQueryInterface(p,a,b)
#define IDirect3D_AddRef(p)             (p)-&#62;lpVtbl-&#62;fnAddRef(p)
#define IDirect3D_Release(p)            (p)-&#62;lpVtbl-&#62;fnRelease(p)
// *** IDirect3D methods *** //
#define IDirect3D_Initialize(p,a)       (p)-&#62;lpVtbl-&#62;fnInitialize(p,a)
#define IDirect3D_EnumDevices(p,a,b)    (p)-&#62;lpVtbl-&#62;fnEnumDevice(p,a,b)
#define IDirect3D_CreateLight(p,a,b)    (p)-&#62;lpVtbl-&#62;fnCreateLight(p,a,b)
#define IDirect3D_CreateMaterial(p,a,b) (p)-&#62;lpVtbl-&#62;fnCreateMaterial(p,a,b)
#define IDirect3D_CreateViewport(p,a,b) (p)-&#62;lpVtbl-&#62;fnCreateViewport(p,a,b)
#define IDirect3D_FindDevice(p,a,b)     (p)-&#62;lpVtbl-&#62;fnFindDevice(p,a,b)
#endif</PRE
></TD
></TR
></TABLE
><P
>          Comments:
        </P
><P
>          IDirect3D only contains a pointer to the IDirect3D
          virtual/jump table. This is the only thing the user needs to
          know to use the interface. Of course the structure we will
          define to implement this interface will have more fields but
          the first one will match this pointer.
        </P
><P
>          The code generated by ICOM_DEFINE defines both the structure
          representing the interface and the structure for the jump
          table. ICOM_DEFINE uses the parent's Xxx_IMETHODS macro to
          automatically repeat the prototypes of all the inherited
          methods and then uses IDirect3D_METHODS to define the
          IDirect3D methods.
        </P
><P
>          Each method is declared as a pointer to function field in
          the jump table. The implementation will fill this jump table
          with appropriate values, probably using a static variable,
          and initialize the lpVtbl field to point to this variable.
        </P
><P
>          The IDirect3D_Xxx macros then just dereference the lpVtbl
          pointer and use the function pointer corresponding to the
          macro name. This emulates the behavior of a virtual table
          and should be just as fast.
        </P
><P
>          This C code should be quite compatible with the Windows
          headers both for code that uses COM interfaces and for code
          implementing a COM interface.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3983"
>Bindings in C++</A
></H3
><P
>          And in C++ (with gcc's g++):
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>typedef struct IDirect3D: public IUnknown {
    private: HRESULT (*fnInitialize)(IDirect3D* me, REFIID a);
    public: inline HRESULT Initialize(REFIID a) { return ((IDirect3D*)t.lpVtbl)-&#62;fnInitialize(this,a); };
    private: HRESULT (*fnEnumDevices)(IDirect3D* me, LPD3DENUMDEVICESCALLBACK a, LPVOID b);
    public: inline HRESULT EnumDevices(LPD3DENUMDEVICESCALLBACK a, LPVOID b)
        { return ((IDirect3D*)t.lpVtbl)-&#62;fnEnumDevices(this,a,b); };
    private: HRESULT (*fnCreateLight)(IDirect3D* me, LPDIRECT3DLIGHT* a, IUnknown* b);
    public: inline HRESULT CreateLight(LPDIRECT3DLIGHT* a, IUnknown* b)
        { return ((IDirect3D*)t.lpVtbl)-&#62;fnCreateLight(this,a,b); };
    private: HRESULT (*fnCreateMaterial)(IDirect3D* me, LPDIRECT3DMATERIAL* a, IUnknown* b);
    public: inline HRESULT CreateMaterial(LPDIRECT3DMATERIAL* a, IUnknown* b)
        { return ((IDirect3D*)t.lpVtbl)-&#62;fnCreateMaterial(this,a,b); };
    private: HRESULT (*fnCreateViewport)(IDirect3D* me, LPDIRECT3DVIEWPORT* a, IUnknown* b);
    public: inline HRESULT CreateViewport(LPDIRECT3DVIEWPORT* a, IUnknown* b)
        { return ((IDirect3D*)t.lpVtbl)-&#62;fnCreateViewport(this,a,b); };
    private:  HRESULT (*fnFindDevice)(IDirect3D* me, LPD3DFINDDEVICESEARCH a, LPD3DFINDDEVICERESULT b);
    public: inline HRESULT FindDevice(LPD3DFINDDEVICESEARCH a, LPD3DFINDDEVICERESULT b)
        { return ((IDirect3D*)t.lpVtbl)-&#62;fnFindDevice(this,a,b); };
};</PRE
></TD
></TR
></TABLE
><P
>          Comments:
        </P
><P
>          In C++ IDirect3D does double duty as both the virtual/jump
          table and as the interface definition. The reason for this
          is to avoid having to duplicate the method definitions: once
          to have the function pointers in the jump table and once to
          have the methods in the interface class. Here one macro can
          generate both. This means though that the first pointer,
          t.lpVtbl defined in IUnknown, must be interpreted as the
          jump table pointer if we interpret the structure as the
          interface class, and as the function pointer to the
          QueryInterface method, t.fnQueryInterface, if we interpret
          the structure as the jump table. Fortunately this gymnastic
          is entirely taken care of in the header of IUnknown.
        </P
><P
>          Of course in C++ we use inheritance so that we don't have to
          duplicate the method definitions.
        </P
><P
>          Since IDirect3D does double duty, each ICOM_METHOD macro
          defines both a function pointer and a non-virtual inline
          method which dereferences it and calls it. This way this
          method behaves just like a virtual method but does not
          create a true C++ virtual table which would break the
          structure layout. If you look at the implementation of these
          methods you'll notice that they would not work for void
          functions. We have to return something and fortunately this
          seems to be what all the COM methods do (otherwise we would
          need another set of macros).
        </P
><P
>          Note how the ICOM_METHOD generates both function prototypes
          mixing types and formal parameter names and the method
          invocation using only the formal parameter name. This is the
          reason why we need different macros to handle different
          numbers of parameters.
        </P
><P
>          Finally there is no IDirect3D_Xxx macro. These are not
          needed in C++ unless the CINTERFACE macro is defined in
          which case we would not be here.
        </P
><P
>          This C++ code works well for code that just uses COM
          interfaces. But it will not work with C++ code implement a
          COM interface. That's because such code assumes the
          interface methods are declared as virtual C++ methods which
          is not the case here.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN3994"
>Implementing a COM interface.</A
></H3
><P
>          This continues the above example. This example assumes that
          the implementation is in C.
        </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>typedef struct _IDirect3D {
    void* lpVtbl;
    // ...
 } _IDirect3D;

static ICOM_VTABLE(IDirect3D) d3dvt;

// implement the IDirect3D methods here

int IDirect3D_fnQueryInterface(IDirect3D* me)
{
    ICOM_THIS(IDirect3D,me);
    // ...
}

// ...

static ICOM_VTABLE(IDirect3D) d3dvt = {
    ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
    IDirect3D_fnQueryInterface,
    IDirect3D_fnAdd,
    IDirect3D_fnAdd2,
    IDirect3D_fnInitialize,
    IDirect3D_fnSetWidth
};</PRE
></TD
></TR
></TABLE
><P
>          Comments:
        </P
><P
>          We first define what the interface really contains. This is
          the _IDirect3D structure. The first field must of course be
          the virtual table pointer. Everything else is free.
        </P
><P
>          Then we predeclare our static virtual table variable, we
          will need its address in some methods to initialize the
          virtual table pointer of the returned interface objects.
        </P
><P
>          Then we implement the interface methods. To match what has
          been declared in the header file they must take a pointer to
          an IDirect3D structure and we must cast it to an _IDirect3D
          so that we can manipulate the fields. This is performed by
          the ICOM_THIS macro.
        </P
><P
>          Finally we initialize the virtual table.
        </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DCOM-1"
>A brief introduction to DCOM in Wine</A
></H2
><P
>        This section explains the basic principles behind DCOM remoting as used by InstallShield and others.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4006"
>Basics</A
></H3
><P
>          The basic idea behind DCOM is to take a COM object and make it location
          transparent. That means you can use it from other threads, processes and
          machines without having to worry about the fact that you can't just
          dereference the interface vtable pointer to call methods on it.
        </P
><P
>          You might be wondering about putting threads next to processes and
          machines in that last paragraph. You can access thread safe objects from
          multiple threads without DCOM normally, right? Why would you need RPC
          magic to do that?
        </P
><P
>          The answer is of course that COM doesn't assume that objects actually
          are thread-safe. Most real-world objects aren't, in fact, for various
          reasons. What these reasons are isn't too important here, though; it's
          just important to realize that the problem of thread-unsafe objects is
          what COM tries hard to solve with its apartment model. There are also
          ways to tell COM that your object is truly thread-safe (namely the
          free-threaded marshaller). In general, no object is truly thread-safe if
          it could potentially use another not so thread-safe object, though, so
          the free-threaded marshaller is less used than you'd think.
        </P
><P
>          For now, suffice it to say that COM lets you "marshal" interfaces into
          other "apartments". An apartment (you may see it referred to as a
          context in modern versions of COM) can be thought of as a location, and
          contains objects. 
        </P
><P
>          Every thread in a program that uses COM exists in an apartment. If a
          thread wishes to use an object from another apartment, marshalling and
          the whole DCOM infrastructure gets involved to make that happen behind
          the scenes.
        </P
><P
>          So. Each COM object resides in an apartment, and each apartment
          resides in a process, and each process resides in a machine, and each
          machine resides in a network. Allowing those objects to be used
          from <I
CLASS="EMPHASIS"
>any</I
> of these different places is what DCOM
          is all about.
        </P
><P
>          The process of marshalling refers to taking a function call in an
          apartment and actually performing it in another apartment. Let's say you
          have two machines, A and B, and on machine B there is an object sitting
          in a DLL on the hard disk. You want to create an instance of that object
          (activate it) and use it as if you had compiled it into your own
          program. This is hard, because the remote object is expecting to be
          called by code in its own address space - it may do things like accept
          pointers to linked lists and even return other objects.
        </P
><P
>          Very basic marshalling is easy enough to understand. You take a method
          on a remote interface (that is a COM interface that is
          implemented on the remote computer), copy each of its
          parameters into a buffer, and
          send it to the remote computer. On the other end, the remote server
          reads each parameter from the buffer, calls the method, writes the
          result into another buffer and sends it back.
        </P
><P
>          The tricky part is exactly how to encode those parameters in the buffer,
          and how to convert standard stdcall/cdecl method calls to network
          packets and back again. This is the job of the RPCRT4.DLL file - or the
          Remote Procedure Call Runtime. 
        </P
><P
>          The backbone of DCOM is this RPC runtime, which is an implementation
          of <A
HREF="http://www.opengroup.org/onlinepubs/009629399/toc.htm"
TARGET="_top"
>DCE
          RPC</A
>. DCE RPC is not naturally object oriented, so this
          protocol is extended with some new constructs and by assigning new
          meanings to some of the packet fields, to produce ORPC or Object
          RPC. You might see it called MS-RPC as well.
        </P
><P
>          RPC packets contain a buffer containing marshalled data in NDR format.
          NDR is short for "Network Data Representation" and is similar
          to the XDR
          format used in SunRPC (the closest native equivalent on Linux to DCE
          RPC). NDR/XDR are all based on the idea of graph serialization and were
          worked out during the 80s, meaning they are very powerful and can do
          things like marshal doubly linked lists and other rather tricky
          structures.
        </P
><P
>          In Wine, our DCOM implementation is <I
CLASS="EMPHASIS"
>not</I
>
          currently based on the
          RPC runtime, as while few programs use DCOM even fewer use
          RPC directly so it was developed some time after
          OLE32/OLEAUT32 were. Eventually this will have to be fixed,
          otherwise our DCOM will never be compatible with
          Microsoft's. Bear this in mind as you read through the code
          however.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4023"
>Proxies and Stubs</A
></H3
><P
>          Manually marshalling and unmarshalling each method call using the NDR
          APIs (NdrConformantArrayMarshall etc) is very tedious work, so the
          Platform SDK ships with a tool called "midl" which is an IDL compiler.
          IDL or the "Interface Definition Language" is a tool designed
          specifically for describing interfaces in a reasonably language neutral
          fashion, though in reality it bears a close resemblence to C++.
        </P
><P
>          By describing the functions you want to expose via RPC in IDL therefore,
          it becomes possible to pass this file to MIDL which spits out a huge
          amount of C source code. That code defines functions which have the same
          prototype as the functions described in your IDL but which internally
          take each argument, marshal it using Ndr, send the packet, and unmarshal
          the return.
        </P
><P
>          Because this code proxies the code from the client to the server, the
          functions are called proxies. Easy, right?
        </P
><P
>          Of course, in the RPC server process at the other end, you need some way
          to unmarshal the RPCs, so you have functions also generated by MIDL
          which are the inverse of the proxies; they accept an NDR buffer, extract
          the parameters, call the real function and then marshal the result back.
          They are called stubs, and stand in for the real calling code in the
          client process.
        </P
><P
>          The sort of marshalling/unmarshalling code that MIDL spits out can be
          seen in dlls/oleaut32/oaidl_p.c - it's not exactly what it would look
          like as that file contains DCOM proxies/stubs which are different, but
          you get the idea. Proxy functions take the arguments and feed them to
          the NDR marshallers (or picklers), invoke an NdrProxySendReceive and
          then convert the out parameters and return code. There's a ton of goop
          in there for dealing with buffer allocation, exceptions and so on - it's
          really ugly code. But, this is the basic concept behind DCE RPC.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4030"
>Interface Marshalling</A
></H3
><P
>          Standard NDR only knows about C style function calls - they
          can accept and even return structures, but it has no concept
          of COM interfaces.  Confusingly DCE RPC <I
CLASS="EMPHASIS"
>does</I
> have a
          concept of RPC interfaces which are just convenient ways to
          bundle function calls together into namespaces, but let's
          ignore that for now as it just muddies the water. The
          primary extension made by Microsoft to NDR then was the
          ability to take a COM interface pointer and marshal that
          into the NDR stream.
        </P
><P
>          The basic theory of proxies and stubs and IDL is still here, but it's
          been modified slightly. Whereas before you could define a bunch of
          functions in IDL, now a new "object" keyword has appeared. This tells
          MIDL that you're describing a COM interface, and as a result the
          proxies/stubs it generates are also COM objects.
        </P
><P
>          That's a very important distinction. When you make a call to a remote
          COM object you do it via a proxy object that COM has constructed on the
          fly. Likewise, a stub object on the remote end unpacks the RPC packet
          and makes the call. 
        </P
><P
>          Because this is object-oriented RPC, there are a few complications: for
          instance, a call that goes via the same proxies/stubs may end up at a
          different object instance, so the RPC runtime keeps track of "this" and
          "that" in the RPC packets.
        </P
><P
>          This leads naturally onto the question of how we got those proxy/stub
          objects in the first place, and where they came from. You can use the
          CoCreateInstanceEx API to activate COM objects on a remote machine, this
          works like CoCreateInstance API. Behind the scenes, a lot of stuff is
          involved to do this (like IRemoteActivation, IOXIDResolver and so on)
          but let's gloss over that for now.
        </P
><P
>          When DCOM creates an object on a remote machine, the DCOM runtime on
          that machine activates the object in the usual way (by looking it up in
          the registry etc) and then marshals the requested interface back to the
          client. Marshalling an interface takes a pointer, and produces a buffer
          containing all the information DCOM needs to construct a proxy object in
          the client, a stub object in the server and link the two together.
        </P
><P
>          The structure of a marshalled interface pointer is somewhat complex.
          Let's ignore that too. The important thing is how COM proxies/stubs are
          loaded.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4040"
>COM Proxy/Stub System</A
></H3
><P
>          COM proxies are objects that implement both the interfaces needing to be
          proxied and also IRpcProxyBuffer. Likewise, COM stubs implement
          IRpcStubBuffer and understand how to invoke the methods of the requested
          interface.
        </P
><P
>          You may be wondering what the word "buffer" is doing in those interface
          names. I'm not sure either, except that a running theme in DCOM is that
          interfaces which have nothing to do with buffers have the word Buffer
          appended to them, seemingly at random. Ignore it and <I
CLASS="EMPHASIS"
>don't let it
            confuse you</I
> :) This stuff is convoluted enough ...
        </P
><P
>          The IRpc[Proxy/Stub]Buffer interfaces are used to control the proxy/stub
          objects and are one of the many semi-public interfaces used in DCOM.
        </P
><P
>          DCOM is theoretically an internet RFC and is
          specced out, but in reality the only implementation of it apart from
          ours is Microsoft's, and as a result there are lots of interfaces
          which <I
CLASS="EMPHASIS"
>can</I
> be used if you want to customize or
          control DCOM but in practice are badly documented or not documented at
          all, or exist mostly as interfaces between MIDL generated code and COM
          itself. Don't pay too much attention to the MSDN definitions of these
          interfaces and APIs.
        </P
><P
>          COM proxies and stubs are like any other normal COM object - they are
          registered in the registry, they can be loaded with CoCreateInstance and
          so on. They have to be in process (in DLLs) however. They aren't
          activated directly by COM however, instead the process goes something
          like this:
          
          <P
></P
><UL
><LI
><P
> COM receives a marshalled interface packet, and retrieves the IID of
                the marshalled interface from it </P
></LI
><LI
><P
> COM looks in
              HKEY_CLASSES_ROOT/Interface/{whatever-iid}/ProxyStubClsId32
              to retrieve the CLSID of another COM object, which
              implements IPSFactoryBuffer. </P
></LI
><LI
><P
> IPSFactoryBuffer has only two methods, CreateProxy and CreateStub. COM
                calls whichever is appropriate: CreateStub for the server, CreateProxy
                for the client. MIDL will normally provide an implementation of this
                object for you in the code it generates. </P
></LI
></UL
>
          
        </P
><P
>          Once CreateProxy has been called, the resultant object is QueryInterfaced to
          IRpcProxyBuffer, which only has 1 method, IRpcProxyBuffer::Connect.
          This method only takes one parameter, the IRpcChannelBuffer object which
          encapsulates the "RPC Channel" between the client and server.
        </P
><P
>          On the server side, a similar process is performed - the PSFactoryBuffer
          is created, CreateStub is called, result is QId to IRpcStubBuffer, and
          IRpcStubBuffer::Connect is used to link it to the RPC channel.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4058"
>RPC Channels</A
></H3
><P
>          Remember the RPC runtime? Well, that's not just responsible for
          marshalling stuff, it also controls the connection and protocols between
          the client and server. We can ignore the details of this for now,
          suffice it to say that an RPC Channel is a COM object that implements
          IRpcChannelBuffer, and it's basically an abstraction of different RPC
          methods. For instance, in the case of inter-thread marshalling (not
          covered here) the RPC connection code isn't used, only the NDR
          marshallers are, so IRpcChannelBuffer in that case isn't actually
          implemented by RPCRT4 but rather just by the COM/OLE DLLS.
        </P
><P
>          On this topic, Ove Kaaven says: It depends on the Windows version, I
          think. Windows 95 and Windows NT 4 certainly had very different models
          when I looked. I'm pretty sure the Windows 98 version of RPCRT4 was
          able to dispatch messages directly to individual apartments. I'd be
          surprised if some similar functionality was not added to Windows
          2000. After all, if an object on machine A wanted to use an object on
          machine B in an apartment C, wouldn't it be most efficient if the RPC
          system knew about apartments and could dispatch the message directly
          to it? And if RPC does know how to efficiently dispatch to apartments,
          why should COM duplicate this functionality? There were, however, no
          unified way to tell RPC about them across Windows versions, so in that
          old patch of mine, I let the COM/OLE dlls do the apartment dispatch,
          but even then, the RPC runtime was always involved. After all, it
          could be quite tricky to tell whether the call is merely interthread,
          without involving the RPC runtime...
        </P
><P
>          RPC channels are constructed on the fly by DCOM as part of the
          marshalling process. So, when you make a call on a COM proxy, it goes
          like this:
        </P
><P
>          Your code -&#62; COM proxy object -&#62; RPC Channel -&#62; COM stub object -&#62; Their code
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4064"
>How this actually works in Wine</A
></H3
><P
>          Right now, Wine does not use the NDR marshallers or RPC to implement its
          DCOM. When you marshal an interface in Wine, in the server process a
          _StubMgrThread thread is started. I haven't gone into the stub manager
          here. The important thing is that eventually a _StubReaderThread is
          started which accepts marshalled DCOM RPCs, and then passes them to
          IRpcStubBuffer::Invoke on the correct stub object which in turn
          demarshals the packet and performs the call. The threads started by our
          implementation of DCOM are never terminated, they just hang around until
          the process dies.
        </P
><P
>          Remember that I said our DCOM doesn't use RPC? Well, you might be
          thinking "but we use IRpcStubBuffer like we're supposed to ... isn't
          that provided by MIDL which generates code that uses the NDR APIs?". If
          so pat yourself on the back, you're still with me. Go get a cup of
          coffee.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4068"
>Typelib Marshaller</A
></H3
><P
>          In fact, the reason for the PSFactoryBuffer layer of indirection is
          because not all interfaces are marshalled using MIDL generated code.
          Why not? Well, to understand <I
CLASS="EMPHASIS"
>that</I
>
          you have to see that one of the
          driving forces behind OLE and by extension DCOM was the development of
          Visual Basic. Microsoft wanted VB developers to be first class citizens
          in the COM world, but things like writing IDL and compiling them with a
          C compiler into DLLs wasn't easy enough.
        </P
><P
>          So, type libraries were invented. Actually they were invented as part of
          a parallel line of COM development known as "OLE Automation", but let's
          not get into that here. Type libraries are basically binary IDL files,
          except that despite there being two type library formats neither of them
          can fully express everything expressable in IDL. Anyway, with a type
          library (which can be embedded as a resource into a DLL) you have
          another option beyond compiling MIDL output - you can set the
          ProxyStubClsId32 registry entry for your interfaces to the CLSID of the
          "type library marshaller" or "universal marshaller". Both terms are
          used, but in the Wine source it's called the typelib marshaller.
        </P
><P
>          The type library marshaller constructs proxy and stub objects on the
          fly. It does so by having generic marshalling glue which reads the
          information from the type libraries, and takes the parameters directly
          off the stack. The CreateProxy method actually builds a vtable out of
          blocks of assembly stitched together which pass control to _xCall, which
          then does the marshalling. You can see all this magic in
          dlls/oleaut32/tmarshal.c
        </P
><P
>          In the case of InstallShield, it actually comes with typelibs for all
          the interfaces it needs to marshal (fixme: is this right?), but they
          actually use a mix of MIDL and typelib marshalling. In order to cover up
          for the fact that we don't really use RPC they're all forced to go via
          the typelib marshaller - that's what the 1 || hack is for and what the
          "Registering non-automation type library!" warning is about (I think).
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4075"
>Apartments</A
></H3
><P
>          Before a thread can use COM it must enter an apartment. Apartments are 
          an abstraction of a COM objects thread safety level. There are many types
          of apartment but the only two we care about right now are single threaded
          apartments (STAs) and the multi-threaded apartment (MTA).
        </P
><P
>          Any given process may contain at most one MTA and potentially many STAs.
          This is because all objects in MTAs never care where they are invoked from
          and hence can all be treated the same. Since objects in STAs do care, they
          cannot be treated the same.
        </P
><P
>          You enter an apartment by calling <TT
CLASS="FUNCTION"
>CoInitializeEx()</TT
> and 
          passing the desired thread model in as a parameter. The default if you use 
          the deprecated <TT
CLASS="FUNCTION"
>CoInitialize()</TT
> is a STA, and this is the 
          most common type of apartment used in COM.
        </P
><P
>          An object in the multi-threaded apartment may be accessed concurrently by
          multiple threads: eg, it's supposed to be entirely thread safe. It must also
          not care about thread-affinity, the object should react the same way no matter
          which thread is calling it.
        </P
><P
>          An object inside a STA does not have to be thread safe, and all calls upon it
          should come from the same thread - the thread that entered the apartment in
          the first place.
        </P
><P
>          The apartment system was originally designed to deal with the disparity between
          the Windows NT/C++ world in which threading was given a strong emphasis, and the
          Visual Basic world in which threading was barely supported and even if it had
          been fully supported most developers would not have used it. Visual Basic code
          is not truly multi-threaded, instead if you start a new thread you get an entirely
          new VM, with separate sets of global variables. Changes made in one thread do
          <I
CLASS="EMPHASIS"
>not</I
> reflect in another, which pretty much violates the
          expected semantics of multi-threading entirely but this is Visual Basic, so what
          did you expect? If you access a VB object concurrently from multiple threads,
          behind the scenes each VM runs in a STA and the calls are marshaled between the
          threads using DCOM.
        </P
><P
>          In the Windows 2000 release of COM, several new types of apartment were added, the
          most important of which are RTAs (the rental threaded apartment) in which concurrent
          access are serialised by COM using an apartment-wide lock but thread affinity is
          not guaranteed. 
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4087"
>Structure of a marshaled interface pointer</A
></H3
><P
>          When an interface is marshaled using <TT
CLASS="FUNCTION"
>CoMarshalInterface()</TT
>,
          the result is a serialized OBJREF structure. An OBJREF actually contains a union,
          but we'll be assuming the variant that embeds a STDOBJREF here which is what's
          used by the system provided standard marshaling. A STDOBJREF (standard object
          reference) consists of the magic signature 'MEOW', then some flags, then the IID
          of the marshaled interface. Quite what MEOW stands for is a mystery, but it's
          definitely not "Microsoft Extended Object Wire". Next comes the STDOBJREF flags,
          identified by their SORF_ prefix. Most of these are reserved, and their purpose
          (if any) is unknown, but a few are defined.
        </P
><P
>          After the SORF flags comes a count of the references represented by this marshaled
          interface. Typically this will be 5 in the case of a normal marshal, but may be 0
          for table-strong and  table-weak marshals (the difference between these is explained below).
          The reasoning is this: In the general case, we want to know exactly when an object
          is unmarshaled and released, so we can accurately control the lifetime of the stub
          object. This is what happens when cPublicRefs is zero. However, in many cases, we
          only want to unmarshal an object once. Therefore, if we strengthen the rules to say
          when marshaling that we will only unmarshal once, then we no longer have to know when
          it is unmarshaled. Therefore, we can give out an arbitrary number of references when
          marshaling and basically say "don't call me, except when you die."
        </P
><P
>          The most interesting part of a STDOBJREF is the OXID, OID, IPID triple. This triple
          identifies any given marshaled interface pointer in the network. OXIDs are apartment
          identifiers, and are supposed to be unique network-wide. How this is guaranteed is
          currently unknown: the original algorithm Windows used was something like the current
          UNIX time and a local counter.
        </P
><P
>          OXIDs are generated and registered with the OXID resolver by performing local RPCs
          to the RPC subsystem (rpcss.exe). In a fully security-patched Windows system they
          appear to be randomly generated. This registration is done using the
          <TT
CLASS="FUNCTION"
>ILocalOxidResolver</TT
> interface, however the exact structure of
          this interface is currently unknown.
        </P
><P
>          OIDs are object identifiers, and identify a stub manager. The stub manager manages
          interface stubs. For each exported COM object there are multiple interfaces and
          therefore multiple interface stubs (<TT
CLASS="FUNCTION"
>IRpcStubBuffer</TT
> implementations).
          OIDs are apartment scoped. Each ifstub is identified by an IPID, which identifies
          a marshaled interface pointer. IPIDs are apartment scoped.
        </P
><P
>          Unmarshaling one of these streams therefore means setting up a connection to the
          object exporter (the apartment holding the marshaled interface pointer) and being
          able to send RPCs to the right ifstub. Each apartment has its own RPC endpoint and
          calls can be routed to the correct interface pointer by embedding the IPID into the
          call using RpcBindingSetObject. IRemUnknown, discussed below, uses a reserved IPID.
          Please note that this is true only in the current implementation. The native version
          generates an IPID as per any other object and simply notifies the SCM of this IPID.
        </P
><P
>          Both standard and handler marshaled OBJREFs contains an OXID resolver endpoint which
          is an RPC string binding in a DUALSTRINGARRAY. This is necessary because an OXID
          alone is not enough to contact the host, as it doesn't contain any network address
          data. Instead, the combination of the remote OXID resolver RPC endpoint and the OXID
          itself are passed to the local OXID resolver. It then returns the apartment string binding.
        </P
><P
>          This step is an optimisation: technically the OBJREF itself could contain the string
          binding of the apartment endpoint and the OXID resolver could be bypassed, but by using
          this DCOM can optimise out a server round-trip by having the local OXID resolver cache
          the query results. The OXID resolver is a service in the RPC subsystem (rpcss.exe) which
          implements a raw (non object-oriented) RPC interface called <TT
CLASS="FUNCTION"
>IOXIDResolver</TT
>.
          Despite the identical naming convention this is not a COM interface.
        </P
><P
>          Unmarshaling an interface pointer stream therefore consists of
          reading the OXID, OID and IPID from the STDOBJREF, then reading
          one or more RPC string bindings for the remote OXID resolver.
          Then <TT
CLASS="FUNCTION"
>RpcBindingFromStringBinding</TT
> is used
          to convert this remote string binding into an RPC binding handle
          which can be passed to the local 
          <TT
CLASS="FUNCTION"
>IOXIDResolver::ResolveOxid</TT
> implementation
          along with the OXID. The local OXID resolver consults its list
          of same-machine OXIDs, then its cache of remote OXIDs, and if
          not found does an RPC to the remote OXID resolver using the
          binding handle passed in earlier. The result of the query is
          stored for future reference in the cache, and finally the
          unmarshaling application gets back the apartment string binding,
          the IPID of that apartments <TT
CLASS="FUNCTION"
>IRemUnknown</TT
>
          implementation, and a security hint (let's ignore this for now).
        </P
><P
>          Once the remote apartments string binding has been located the
          unmarshalling process constructs an RPC Channel Buffer
          implementation with the connection handle and the IPID of the
          needed interface, loads and constructs the 
          <TT
CLASS="FUNCTION"
>IRpcProxyBuffer</TT
> implementation for that
          IID and connects it to the channel. Finally the proxy is passed
          back to the application. 
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4107"
>Handling IUnknown</A
></H3
><P
>          There are some subtleties here with respect to IUnknown. IUnknown
          itself is never marshaled directly: instead a version of it
          optimised for network usage is used. IRemUnknown is similar in
          concept to IUnknown except that it allows you to add and release
          arbitrary numbers of references at once, and it also allows you to
          query for multiple interfaces at once.
        </P
><P
>          IRemUnknown is used for lifecycle management, and for marshaling
          new interfaces on an object back to the client. Its definition can
          be seen in dcom.idl - basically the IRemUnknown::RemQueryInterface
          method takes an IPID and a list of IIDs, then returns STDOBJREFs
          of each new marshaled interface pointer.
        </P
><P
>          There is one IRemUnknown implementation per apartment, not per
          stub manager as you might expect. This is OK because IPIDs are
          apartment not object scoped (In fact, according to the DCOM draft
          spec, they are machine-scoped, but this implies apartment-scoped).
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4112"
>Table marshaling</A
></H3
><P
>          Normally once you have unmarshaled a marshaled interface pointer
          that stream is dead, you can't unmarshal it again. Sometimes this
          isn't what you want. In this case, table marshaling can be used. 
          There are two types: strong and weak. In table-strong marshaling,
          selected by a specific flag to <TT
CLASS="FUNCTION"
>CoMarshalInterface()</TT
>,
          a stream can be unmarshaled as many times as you like. Even if
          all the proxies are released, the marshaled object reference is
          still valid. Effectively the stream itself holds a ref on the object.
          To release the object entirely so its server can shut down, you 
          must use <TT
CLASS="FUNCTION"
>CoReleaseMarshalData()</TT
> on the stream.
        </P
><P
>          In table-weak marshaling the stream can be unmarshaled many times,
          however the stream does not hold a ref. If you unmarshal the
          stream twice, once those two proxies have been released remote
          object will also be released. Attempting to unmarshal the stream
          at this point will yield <TT
CLASS="FUNCTION"
>CO_E_DISCONNECTED</TT
>.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4119"
>RPC dispatch</A
></H3
><P
>          Exactly how RPC dispatch occurs depends on whether the exported
          object is in a STA or the MTA. If it's in the MTA then all is
          simple: the RPC dispatch thread can temporarily enter the MTA,
          perform the remote call, and then leave it again. If it's in a
          STA things get more complex, because of the requirement that only
          one thread can ever access the object.
        </P
><P
>          Instead, when entering a STA a hidden window is created implicitly
          by COM, and the user must manually pump the message loop in order
          to service incoming RPCs. The RPC dispatch thread performs the
          context switch into the STA by sending a message to the apartments
          window, which then proceeds to invoke the remote call in the right
          thread.
        </P
><P
>          RPC dispatch threads are pooled by the RPC runtime. When an incoming
          RPC needs to be serviced, a thread is pulled from the pool and
          invokes the call. The main RPC thread then goes back to listening
          for new calls. It's quite likely for objects in the MTA to therefore
          be servicing more than one call at once.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4124"
>Message filtering and re-entrancy</A
></H3
><P
>          When an outgoing call is made from a STA, it's possible that the
          remote server will re-enter the client, for instance to perform a
          callback. Because of this potential re-entrancy, when waiting for
          the reply to an RPC made inside a STA, COM will pump the message loop.
          That's because while this thread is blocked, the incoming callback
          will be dispatched by a thread from the RPC dispatch pool, so it
          must be processing messages.
        </P
><P
>          While COM is pumping the message loop, all incoming messages from
          the operating system are filtered through one or more message filters.
          These filters are themselves COM objects which can choose to discard,
          hold or forward window messages. The default message filter drops all
          input messages and forwards the rest. This is so that if the user
          chooses a menu option which triggers an RPC, they then cannot choose
          that menu option *again* and restart the function from the beginning.
          That type of unexpected re-entrancy is extremely difficult to debug,
          so it's disallowed.
        </P
><P
>          Unfortunately other window messages are allowed through, meaning that
          it's possible your UI will be required to repaint itself during an
          outgoing RPC. This makes programming with STAs more complex than it
          may appear, as you must be prepared to run all kinds of code any time
          an outgoing call is made. In turn this breaks the idea that COM
          should abstract object location from the programmer, because an
          object that was originally free-threaded and is then run from a STA
          could trigger new and untested codepaths in a program.  
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4129"
>Wrapup</A
></H3
><P
>          Theres are still a lot of topics that have not been covered:
        </P
><P
></P
><UL
><LI
><P
> Format strings/MOPs</P
></LI
><LI
><P
> IRemoteActivation</P
></LI
><LI
><P
> Complex/simple pings, distributed garbage collection</P
></LI
><LI
><P
> Marshalling IDispatch</P
></LI
><LI
><P
> ICallFrame</P
></LI
><LI
><P
> Interface pointer swizzling</P
></LI
><LI
><P
> Runtime class object registration (CoRegisterClassObject), ROT</P
></LI
><LI
><P
> Exactly how InstallShield uses DCOM</P
></LI
></UL
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4149"
>Further Reading</A
></H3
><P
>          Most of these documents assume you have knowledge only contained in
          other documents. You may have to reread them a few times for it all to
          make sense. Don't feel you need to read these to understand DCOM, you
          don't, you only need to look at them if you're planning to help
          implement it.
        </P
><P
></P
><UL
><LI
><P
>              <A
HREF="http://www-csag.ucsd.edu/individual/achien/cs491-f97/projects/dcom-writeup.ps"
TARGET="_top"
>                http://www-csag.ucsd.edu/individual/achien/cs491-f97/projects/dcom-writeup.ps</A
>

            </P
></LI
><LI
><P
>              <A
HREF="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_n2p_459u.asp"
TARGET="_top"
>                http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_n2p_459u.asp</A
>

            </P
></LI
><LI
><P
>              <A
HREF="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_q2z_5ygi.asp"
TARGET="_top"
>                http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_q2z_5ygi.asp</A
>
            </P
></LI
><LI
><P
>                <A
HREF="http://www.microsoft.com/msj/0398/dcom.aspx"
TARGET="_top"
>                  http://www.microsoft.com/msj/0398/dcom.aspx</A
>
            </P
></LI
><LI
><P
>                <A
HREF="http://www.microsoft.com/ntserver/techresources/appserv/COM/DCOM/4_ConnectionMgmt.asp"
TARGET="_top"
>                  http://www.microsoft.com/ntserver/techresources/appserv/COM/DCOM/4_ConnectionMgmt.asp</A
>
            </P
></LI
><LI
><P
><A
HREF="http://www.idevresource.com/com/library/articles/comonlinux.asp"
TARGET="_top"
>                  http://www.idevresource.com/com/library/articles/comonlinux.asp</A
>

                (unfortunately part 2 of this article does not seem to exist anymore, if it was ever written)</P
></LI
></UL
></DIV
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="OPENGL"
></A
>Wine and OpenGL</H1
><DIV
CLASS="SECT1"
><H2
CLASS="SECT1"
><A
NAME="OPENGL-REQUIRED"
>What is needed to have OpenGL support in Wine</A
></H2
><P
>        Basically, if you have a Linux OpenGL ABI compliant libGL
        (<A
HREF="http://oss.sgi.com/projects/ogl-sample/ABI/"
TARGET="_top"
>          http://oss.sgi.com/projects/ogl-sample/ABI/</A
>)
        installed on your computer, you should have everything 
	that is needed.
      </P
><P
>        To be more clear, I will detail one step after another what
        the <B
CLASS="COMMAND"
>configure</B
> script checks.
      </P
><P
>        If, after Wine compiles, OpenGL support is not compiled in,
        you can always check <TT
CLASS="FILENAME"
>config.log</TT
> to see
        which of the following points failed.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4181"
>Header files</A
></H3
><P
>          The needed header files to build OpenGL support in Wine are :
        </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="FILENAME"
>gl.h:</TT
></DT
><DD
><P
>                the definition of all OpenGL core functions, types and enumerants
              </P
></DD
><DT
><TT
CLASS="FILENAME"
>glx.h:</TT
></DT
><DD
><P
>                how OpenGL integrates in the X Window environment
              </P
></DD
><DT
><TT
CLASS="FILENAME"
>glext.h:</TT
></DT
><DD
><P
>                the list of all registered OpenGL extensions
              </P
></DD
></DL
></DIV
><P
>          The latter file (<TT
CLASS="FILENAME"
>glext.h</TT
>) is, as of
          now, not necessary to build Wine. But as this file can be
          easily obtained from SGI 
          (<A
HREF="http://oss.sgi.com/projects/ogl-sample/ABI/glext.h"
TARGET="_top"
>            http://oss.sgi.com/projects/ogl-sample/ABI/glext.h</A
>),
          and that all OpenGL should provide one, I decided to keep it here.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4203"
>OpenGL library itself</A
></H3
><P
>          To check for the presence of 'libGL' on the system, the
          script checks if it defines the
          <TT
CLASS="FUNCTION"
>glXCreateContext</TT
> function.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4207"
>glXGetProcAddressARB function</A
></H3
><P
>          The core of Wine's OpenGL implementation (at least for all
          extensions) is the <TT
CLASS="FUNCTION"
>glXGetProcAddressARB</TT
>
          function. Your OpenGL library needs to have this function
          defined for Wine to be able to support OpenGL.
        </P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="OPENGL-WORKS"
>How it all works</A
></H2
><P
>        The core OpenGL function calls are the same between Windows
        and Linux. So what is the difficulty to support it in Wine ?
        Well, there are two different problems :
      </P
><P
></P
><OL
TYPE="1"
><LI
><P
>            the interface to the windowing system is different for
            each OS. It's called 'GLX' for Linux (well, for X Window)
            and 'wgl' for Windows. Thus, one need first to emulate one
            (wgl) with the other (GLX).
          </P
></LI
><LI
><P
>            the calling convention between Windows (the 'Pascal'
            convention or 'stdcall') is different from the one used on
            Linux (the 'C' convention or 'cdecl'). This means that
            each call to an OpenGL function must be 'translated' and
            cannot be used directly by the Windows program.
          </P
></LI
></OL
><P
>        Add to this some brain-dead programs (using GL calls without
        setting-up a context or deleting three time the same context)
        and you have still some work to do :-)
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4220"
>The Windowing system integration</A
></H3
><P
>          This integration is done at two levels : 
        </P
><P
></P
><OL
TYPE="1"
><LI
><P
>              At GDI level for all pixel format selection routines (ie
              choosing if one wants a depth / alpha buffer, the size
              of these buffers, ...) and to do the 'page flipping' in
              double buffer mode. This is implemented in
              <TT
CLASS="FILENAME"
>dlls/x11drv/opengl.c</TT
> (all these
              functions are part of Wine's graphic driver function
              pointer table and thus could be reimplemented if ever Wine
              works on another Windowing system than X).
            </P
></LI
><LI
><P
>              In the <TT
CLASS="FILENAME"
>OpenGL32.DLL</TT
> itself for all
              other functionalities (context creation / deletion,
              querying of extension functions, ...). This is done in
              <TT
CLASS="FILENAME"
>dlls/opengl32/wgl.c</TT
>.
            </P
></LI
></OL
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4231"
>The thunks</A
></H3
><P
>          The thunks are the Wine code that does the calling
          convention translation and they are auto-generated by a Perl
          script. In Wine's Git tree, these thunks are already
          generated for you. Now, if you want to do it yourself, there
          is how it all works....
        </P
><P
>          The script is located in <TT
CLASS="FILENAME"
>dlls/opengl32</TT
>
          and is called <B
CLASS="COMMAND"
>make_opengl</B
>. It requires
          Perl5 to work and takes two arguments :
        </P
><P
></P
><OL
TYPE="1"
><LI
><P
>              The first is the path to the OpenGL registry. Now, you
              will all ask 'but what is the OpenGL registry ?' :-)
              Well, it's part of the OpenGL sample implementation
              source tree from SGI (more information at this URL :
              <A
HREF="http://oss.sgi.com/projects/ogl-sample/"
TARGET="_top"
>                http://oss.sgi.com/projects/ogl-sample/</A
>. 
            </P
><P
>              To summarize, these files contain human-readable but
              easily parsed information on ALL OpenGL core functions
              and ALL registered extensions (for example the
              prototype, the OpenGL version, ...).
            </P
></LI
><LI
><P
>              the second is the OpenGL version to 'simulate'. This
              fixes the list of functions that the Windows application
              can link directly to without having to query them from
              the OpenGL driver. Windows is based, for now, on OpenGL
              1.1, but the thunks that are in the Git tree are
              generated for OpenGL 1.2.
            </P
><P
>              This option can have three values:
              <TT
CLASS="LITERAL"
>1.0</TT
>, <TT
CLASS="LITERAL"
>1.1</TT
> and
              <TT
CLASS="LITERAL"
>1.2</TT
>.
            </P
></LI
></OL
><P
>          This script generates three files :
        </P
><P
></P
><OL
TYPE="1"
><LI
><P
>              <TT
CLASS="FILENAME"
>opengl32.spec</TT
> gives Wine's linker
              the signature of all function in the
              <TT
CLASS="FILENAME"
>OpenGL32.DLL</TT
> library so that the
              application can link them. Only 'core' functions are
              listed here.
            </P
></LI
><LI
><P
>              <TT
CLASS="FILENAME"
>opengl_norm.c</TT
> contains all the
              thunks for the 'core' functions. Your OpenGL library
              must provide ALL the function used in this file as these
              are not queried at run time.
            </P
></LI
><LI
><P
>              <TT
CLASS="FILENAME"
>opengl_ext.c</TT
> contains all the
              functions that are not part of the 'core' functions.
              Contrary to the thunks in
              <TT
CLASS="FILENAME"
>opengl_norm.c</TT
>, these functions do
              not depend at all on what your libGL provides. 
            </P
><P
>              In fact, before using one of these thunks, the Windows
              program first needs to 'query' the function pointer. At
              this point, the corresponding thunk is useless. But as
              we first query the same function in libGL and store the
              returned function pointer in the thunk, the latter
              becomes functional.
            </P
></LI
></OL
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="OPENGL-PROBLEMS"
>Known problems</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN4264"
>When running an OpenGL application, the screen flickers</A
></H3
><P
>          Due to restrictions (that do not exist in Windows) on OpenGL
          contexts, if you want to prevent the screen to flicker when
          using OpenGL applications (all games are using double-buffered
          contexts), you need to set the following option in your
          <TT
CLASS="FILENAME"
>~/.wine/config</TT
> file
          in the <TT
CLASS="LITERAL"
>[x11drv]</TT
> section:
          <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>DesktopDoubleBuffered = Y
          </PRE
></TD
></TR
></TABLE
>
          and to run Wine in desktop mode.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4270"
>Unknown extension error message:</A
></H3
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>Extension defined in the OpenGL library but NOT in opengl_ext.c...
Please report (lionel.ulmer@free.fr) !
        </PRE
></TD
></TR
></TABLE
><P
>          This means that the extension requested by the application
          is found in the libGL used by Linux (ie the call to
          <TT
CLASS="FUNCTION"
>glXGetProcAddressARB</TT
> returns a
          non-<CODE
CLASS="CONSTANT"
>NULL</CODE
> pointer) but that this string
          was NOT found in Wine's extension registry.
        </P
><P
>          This can come from two causes:
        <P
></P
><OL
TYPE="1"
><LI
><P
>              The <TT
CLASS="FILENAME"
>opengl_ext.c</TT
> file is too old
              and needs to be generated again.
            </P
></LI
><LI
><P
>              Use of obsolete extensions that are not supported
              anymore by SGI or of 'private' extensions that are not
              registered. An example of the former are
              <TT
CLASS="FUNCTION"
>glMTexCoord2fSGIS</TT
> and
              <TT
CLASS="FUNCTION"
>glSelectTextureSGIS</TT
> as used by
              Quake 2 (and apparently also by old versions of Half
              Life). If documentation can be found on these functions,
              they can be added to Wine's extension set.
            </P
></LI
></OL
>
        </P
><P
>          If you have this, run with <TT
CLASS="PARAMETER"
><I
>WINEDEBUG=+opengl</I
></TT
>
          and send me <CODE
CLASS="EMAIL"
>&#60;<A
HREF="mailto:lionel.ulmer@free.fr"
>lionel.ulmer@free.fr</A
>&#62;</CODE
> the TRACE.
        </P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4288"
><TT
CLASS="FILENAME"
>libopengl32.so</TT
> is built but it is still not working</A
></H3
><P
>          This may be caused by some missing functions required by
          <TT
CLASS="FILENAME"
>opengl_norm.c</TT
> but that your Linux
          OpenGL library does not provide.
        </P
><P
>          To check for this, do the following steps :
        </P
><P
></P
><OL
TYPE="1"
><LI
><P
>              create a dummy <TT
CLASS="FILENAME"
>.c</TT
> file :
            </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>int main(void)
{
    return 0;
}
            </PRE
></TD
></TR
></TABLE
></LI
><LI
><P
>              try to compile it by linking both libwine and
              libopengl32 (this command line supposes that you
              installed the Wine libraries in
              <TT
CLASS="FILENAME"
>/usr/local/lib</TT
>, YMMV) :
            </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>gcc dummy.c -L/usr/local/lib -lwine -lopengl32
            </PRE
></TD
></TR
></TABLE
></LI
><LI
><P
>              if it works, the problem is somewhere else (and you can
              send me an email). If not, you could re-generate the
              thunk files for OpenGL 1.1 for example (and send me your
              OpenGL version so that this problem can be detected at
              configure time).
            </P
></LI
></OL
></DIV
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="DDRAW"
></A
>Outline of DirectDraw Architecture</H1
><P
>      This is an outline of the architecture. Many details are
      skipped, but hopefully this is useful.
    </P
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DDINHERITANCE"
>DirectDraw inheritance tree</A
></H2
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>        Main
         |
        User
         |-----------\
        XVidMode    DGA2
      </PRE
></TD
></TR
></TABLE
><P
>        Most of the DirectDraw functionality is implemented in a common base
        class. Derived classes are responsible for providing display
        mode functions (Enum, Set, Restore), GetCaps, GetDevice identifier
        and internal functions called to create primary and backbuffer
        surfaces.
      </P
><P
>        User provides for DirectDraw capabilities based on drawing to a
        Wine window. It uses the User DirectDrawSurface implementation
        for primary and backbuffer surfaces.
      </P
><P
>        XVidMode attempt to use the XFree86 VidMode extension to set the
        display resolution to match the parameters to SetDisplayMode.
      </P
><P
>        DGA2 attempt to use the XFree86 DGA 2.x extension to set the
        display resolution and direct access to the framebuffer, if the
        full-screen-exclusive cooperative level is used. If not, it just
        uses the User implementation.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="DDSURFACEINHERITANCE"
>DirectDrawSurface inheritance tree</A
></H2
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>             Main
              |--------------\
              |              |
             DIB        Fake Z-Buffer
              |
              |------\---------\
              |      |         |
             User   DGA2   DIBTexture
        </PRE
></TD
></TR
></TABLE
><P
>             Main provides a very simple base class that does not implement any of
             the image-related functions. Therefore it does not place any
             constraints on how the surface data is stored.
        </P
><P
>             DIB stores the surface data in a DIB section. It is used by the Main
             DirectDraw driver to create off-screen surfaces.
        </P
><P
>             User implements primary and backbuffer surfaces for the User DirectDraw
             driver. If it is a primary surface, it will attempt to keep itself
             synchronized to the window.
        </P
><P
>             DGA2 surfaces claims an appropriate section of framebuffer space and
             lets DIB build its DIB section on top of it.
        </P
><P
>             Fake Z-Buffer surfaces are used by Direct3D to indicate that a primary
             surface has an associated z-buffer. For a first implementation, it
             doesn't need to store any image data since it is just a placeholder.
        </P
><P
>             (Actually 3D programs will rarely use Lock or GetDC on primary
             surfaces, backbuffers or z-buffers so we may want to arrange for
             lazy allocation of the DIB sections.)
        </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="INTERFACETHUNKS"
>Interface Thunks</A
></H2
><P
>        Only the most recent version of an interface needs to be implemented.
        Other versions are handled by having thunks convert their parameters
        and call the root version.
      </P
><P
>        Not all interface versions have thunks. Some versions could be combined
        because their parameters were compatible. For example if a structure
        changes but the structure has a dwSize field, methods using that structure
        are compatible, as long as the implementation remembers to take the dwSize
        into account.
      </P
><P
>        Interface thunks for Direct3D are more complicated since the paradigm
        changed between versions.
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="LOGICALOBJECTLAYOUT"
>Logical Object Layout</A
></H2
><P
>        The objects are split into the generic part (essentially the fields for
        Main) and a private part. This is necessary because some objects
        can be created with CoCreateInstance, then Initialized later. Only
        at initialization time do we know which class to use. Each class
        except Main declares a Part structure and adds that to its Impl.
      </P
><P
>        For example, the DIBTexture DirectDrawSurface implementation looks
        like this:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>        struct DIBTexture_DirectDrawSurfaceImpl_Part
        {
                union DIBTexture_data data; /*declared in the real header*/
        };

        typedef struct
        {
                struct DIB_DirectDrawSurfaceImpl_Part dib;
                struct DIBTexture_DirectDrawSurfaceImpl_Part dibtexture;
        } DIBTexture_DirectDrawSurfaceImpl;
      </PRE
></TD
></TR
></TABLE
><P
>        So the DIBTexture surface class is derived from the DIB surface
        class and it adds one piece of data, a union.
      </P
><P
>        Main does not have a Part structure. Its fields are stored in
        IDirectDrawImpl/IDirectDrawSurfaceImpl.
      </P
><P
>        To access private data, one says
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>        DIBTexture_DirectDrawSurfaceImpl* priv = This-&#62;private;
        do_something_with(priv-&#62;dibtexture.data);
      </PRE
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="CREATINGOBJECT"
>Creating Objects</A
></H2
><P
>        Classes have two functions relevant to object creation, Create and
        Construct. To create a new object, the class' Create function is
        called. It allocates enough memory for IDirectDrawImpl or
        IDirectDrawSurfaceImpl as well as the private data for derived
        classes and then calls Construct.
      </P
><P
>        Each class's Construct function calls the base class's Construct,
        then does the necessary initialization.
      </P
><P
>        For example, creating a primary surface with the user ddraw driver
        calls User_DirectDrawSurface_Create which allocates memory for the
        object and calls User_DirectDrawSurface_Construct to initialize it.
        This calls DIB_DirectDrawSurface_Construct which calls
        Main_DirectDrawSurface_Construct.
      </P
></DIV
></DIV
><DIV
CLASS="CHAPTER"
><HR><H1
><A
NAME="MULTIMEDIA"
></A
>Wine and Multimedia</H1
><P
>      This file contains information about the implementation of the
      multimedia layer of Wine.
    </P
><P
>      The implementation can be found in the
      <TT
CLASS="FILENAME"
>dlls/winmm/</TT
> directory (and in many of its
      subdirectories), but also in <TT
CLASS="FILENAME"
>dlls/msacm/</TT
>
      (for the audio compression/decompression manager) and
      <TT
CLASS="FILENAME"
>dlls/msvideo/</TT
> (for the video
      compression/decompression manager).
    </P
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MM-OVERVIEW"
>Overview</A
></H2
><P
>	The multimedia stuff is split into 3 layers. The low level (device
	drivers), mid level (MCI commands) and high level abstraction layers.
        The low level layer has also some helper DLLs (like the MSACM/MSACM32
        and MSVIDEO/MSVFW32 pairs).
      </P
><P
>	All of those components are defined as DLLs (one by one).
      </P
><P
>	The low level layer may depend on current hardware and OS services
        (like OSS on Unix). It provides the core of playback/record
	using fine grain objects (audio/midi streams...).
      </P
><P
>	Mid level (MCI) and high level layers must be written independently from
	the hardware and OS services.
      </P
><P
>	MCI level provides some coarser grain operations (like playing
	a Midi file, or playing a video stream).
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MM-ARCH"
>Multimedia architecture</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN4359"
>Windows 95 multimedia architecture</A
></H3
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>             |
Kernel space |                    Client applications
             |
             |           | |         ^ ^       | |          | |
             |        16&#62;| |&#60;32   16&#62;| |&#60;32 16&#62;| |&#60;32    16&#62;| |&#60;32
             |           | v         | |       | v          | v
             |      +----|-----------|---------|------------|-------+
             |      |    |           |         |            |       |  WinMM.dll
             |      |    |           |         |            |       |   32 bit
             |      +----|-----------|---------|------------|-------+
             |           | |         | ^       | |          |
             |  +------+ | |&#60;16      | |       | |&#60;16       |
             |  |   16&#62;| | |         | |       | |          |
             |  |      v v v         | |       v v          v
             |  |   +---------------+---+-------------+-------------+
             |  |   | waveInXXX     |   | mciXXX      | *playSound* |
             |  |   | waveOutXXX    |   |             | mmioXXX     |
             |  |   | midiInXXX     |   |             | timeXXX     |
             |  |   | midiOutXXX    |   |             | driverXXX   |
             |  |   | midiStreamXXX |   |             |             |  MMSystem.dll
             |  |   | mixerXXX      |   |             |             |     16 bit
 +--------+  |  |   | auxXXX    +---+   +---+ mmThread|             |
 |MMDEVLDR|&#60;-------&#62;| joyXXX    | Call back | mmTask  |             |
 +--------+  |  |   +-----------+-----------+---------+-------------+
     ^       |  |          |      ^    ^       | ^
     |       |  |       16&#62;|      |&#60;16&#62;|    16&#62;| |&#60;16
     v       |  |          v      |    |       v |
 +--------+  |  |   +-------------+    +----------+
 |  VxD   |&#60;-------&#62;|    *.drv    |    | mci*.drv |
 +--------+  |  |   +--------------+   +-----------+
             |  |    |  msacm.drv  |    | mciwave  |
             |  |    +--------------+   +-----------+
             |  |     | midimap.drv |    | mcimidi  |
             |  |     +-------------+    +-----------+
             |  |    Low-level drivers    |    ...   | MCI drivers
             |  |                         +----------+
             |  |                               |
             |  |                               |&#60;16
             |  +-------------------------------+
             |
	</PRE
></TD
></TR
></TABLE
><P
>	  The important points to notice are:
	  <P
></P
><UL
><LI
><P
>		all drivers (and most of the core code) is 16 bit
	      </P
></LI
><LI
><P
>		all hardware (or most of it) dependent code reside in the kernel
		space (which is not surprising)
	      </P
></LI
></UL
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4368"
>Windows NT multimedia architecture</A
></H3
><P
>	  Note that Win 98 has mixed 95/NT architecture, so when
	  speaking about Windows 95 (resp. NT) architecture, it refers
	  to the type of architecture, not what's actually
	  implemented. For example, Windows 98 implements both types
	  of architectures.
	</P
><P
>	  The important points to notice (compared to the Windows 95
	  architecture) are:
	  <P
></P
><UL
><LI
><P
>		drivers (low level, MCIs...) are 32 bit and Unicode
	      </P
></LI
><LI
><P
>		the interfaces between kernel and user drivers has
		changed, but it doesn't impact much Wine. Those
		changes allow some good things (like kernel mixing,
		where different apps share the audio hardware) and of
		course bad things (like kernel mixing, which adds
		latency).
	      </P
></LI
></UL
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4377"
>Wine multimedia architecture</A
></H3
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>             |
Kernel space |                    Client applications
             |
             |           | |         ^ ^       | |          | |
             |        16&#62;| |&#60;32   16&#62;| |&#60;32 16&#62;| |&#60;32    16&#62;| |&#60;32
             |           | |         | |       | |          | |
             |  +------+ | |         | |       | |          | |
             |  |32/16&#62;| | |         | |       | |          | |
             |  |      v v v         | |       v v          v v
             |  |   +---------------+---+-------------+-------------+
             |  |   | waveInXXX     |   | mciXXX      | *playSound* |
             |  |   | waveOutXXX    |   |             | mmioXXX     | WinMM.dll
             |  |   | midiInXXX     |   |             | timeXXX     |   32 bit
             |  |   | midiOutXXX    |   |             | driverXXX   |
             |  |   | midiStreamXXX |   |             |             | MMSystem.dll
             |  |   | mixerXXX      |   |             |             |   16 bit
             |  |   | auxXXX    +---+   +---+ mmThread|             |
             |  |   | joyXXX    | Call back | mmTask  |             |
             |  |   +-----------+-----------+---------+-------------+
             |  |         ||      ^    ^       | ^
             |  |      16&#62;||&#60;32   |&#60;16&#62;|    16&#62;| |&#60;16
             |  |         vv      |&#60;32&#62;|    32&#62;v |&#60;32
+---------+  |  |   +-------------+    +----------+
|HW driver|&#60;-------&#62;|    *.drv    |    | mci*.drv |
+---------+  |  |   +--------------+   +-----------+
             |  |    |  msacm.drv  |    | mciwave  |
             |  |    +--------------+   +-----------+
             |  |     | midimap.drv |    | mcimidi  |
             |  |     +-------------+    +-----------+
             |  |    Low-level drivers    |    ...   | MCI drivers
             |  |                         +----------+
             |  |                               |
             |  |                               |&#60;32/16
             |  +-------------------------------+
             |
	</PRE
></TD
></TR
></TABLE
><P
>	  From the previous drawings, the most noticeable differences are:
	  <P
></P
><UL
><LI
><P
>		low-level drivers can either be 16 or 32 bit (in fact,
		Wine supports only native wave and audio mappers).
	      </P
></LI
><LI
><P
>		MCI drivers can either be 16 or 32 bit
	      </P
></LI
><LI
><P
>		all built-in drivers (low-level and MCI) will be written as 32 bit
		drivers
	      </P
></LI
></UL
>
	</P
><P
>	  Wine's WinMM automatically adapts the messages to be sent to
	  a driver so that it can convert it to 16 or 32 bit
	  interfaces.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MM-LOW"
>Low level layers</A
></H2
><P
>	The low level drivers abstract the hardware specific features
	from the rest of the multimedia code. Those are implemented with a
	well defined set of APIs, as windows do.
      </P
><P
>	Please note that native low level drivers are not currently supported
	in Wine, because they either access hardware components or require
	VxDs to be loaded; Wine does not correctly supports those two so far.
      </P
><P
>	There are two specific low level drivers (msacm.drv for wave input/output,
        midimap.drv for MIDI output only). These drivers (also present
        in Windows) allow:
	<P
></P
><UL
><LI
><P
>	      choosing one low level driver between many (we'll
              discuss how the choice is made later on)
	    </P
></LI
><LI
><P
>              add the possibility to convert stream's format (ie ADPCM =&#62;
              PCM) (this is useful if the format required by the
              application for playback isn't supported by the soundcard). 
            </P
></LI
><LI
><P
>	      add the possibility to filter a stream (adding echo, equalizer...
              to a wave stream, or modify the instruments that have to be
              played for a MIDI stream).
	    </P
></LI
></UL
>
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4401"
>Hardware-bound low level drivers</A
></H3
><P
>	  Each low lever driver has to implement at least one of the
	  following functionality, through the named function: 
	  <P
></P
><UL
><LI
><P
>		Waveform audio: out for playback, and in for
		recording. MMSYSTEM and WINMM call the real low level
		audio driver using the driver's
		<TT
CLASS="FUNCTION"
>wodMessage</TT
> and
		<TT
CLASS="FUNCTION"
>widMessage</TT
> functions which handle
		the different requests.
	      </P
></LI
><LI
><P
>		MIDI (Musical Instrument Digital Interface): out for
		playback, and in for recording. MMSYSTEM and WINMM
		call the low level driver functions using the driver's
		<TT
CLASS="FUNCTION"
>midMessage</TT
> and the
		<TT
CLASS="FUNCTION"
>modMessage</TT
> functions.
	      </P
></LI
><LI
><P
>		Mixer: this allows setting the volume for each one of
		the other functionnality (and also some specific
		attributes, like left/right balance for stereo
		streams...). MMSYSTEM and WINMM call the low level
		driver functions using the
		<TT
CLASS="FUNCTION"
>mxdMessage</TT
> function. 
	      </P
></LI
><LI
><P
>		Aux: this is the predecessor of the mixer
		functionnality (introduced in Win 95). Its usage has
		been deprecated in favor of mixer interfaces.
	      </P
></LI
></UL
>
	</P
><P
>	  Wine currently supports the following (kernel) multimedia
	  interfaces. 
	  <P
></P
><UL
><LI
><P
>		Open Sound System (OSS) as supplied in the Linux and
		FreeBSD kernels by <A
HREF="http://www.4front-tech.com/"
TARGET="_top"
>4Front
		  Technologies</A
>. The presence of this driver is
		checked by configure (depends on the
		&#60;sys/soundcard.h&#62; file). Source code resides in
		<TT
CLASS="FILENAME"
>dlls/winmm/wineoss</TT
>.
	      </P
></LI
><LI
><P
>		Advanced Linux Sound Architecture (<A
HREF="http://www.alsa-project.org/"
TARGET="_top"
>ALSA</A
>) as
		supplied in the Linux kernel. Source code resides in
		<TT
CLASS="FILENAME"
>dlls/winmm/winealsa</TT
>.
	      </P
></LI
><LI
><P
>		Analog RealTime Synthetizer (<A
HREF="http://www.arts-project.org/"
TARGET="_top"
>aRts</A
>): a
		network server (and virtual mixer) used in the KDE project.
	      </P
></LI
><LI
><P
>		Enlightenment Sound Daemon (<A
HREF="http://www.tux.org/~ricdude/EsounD.html"
TARGET="_top"
>EsounD</A
>):
                  a network server used in the GNOME project.
	      </P
></LI
><LI
><P
>		Network Audio Server (<A
HREF="http://radscan.com/nas.html"
TARGET="_top"
>NAS</A
>): an
		audio server.
	      </P
></LI
><LI
><P
>		<A
HREF="http://jackit.sourceforge.net/"
TARGET="_top"
>Jack</A
>: a
		low latency audio server.
	      </P
></LI
><LI
><P
>		AudioIO: the native Solaris audio interface.
	      </P
></LI
></UL
>
	</P
><P
>	  The supported functionnalities per driver is as follows
	  (this table lists the available features of the products,
	  not exactly what's actually implemented on Wine):
	  <DIV
CLASS="TABLE"
><A
NAME="AEN4443"
></A
><P
><B
>Table 1. Wine multimedia drivers' functionalities</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Driver</TH
><TH
>Wave Out</TH
><TH
>Wave In</TH
><TH
>Midi Out</TH
><TH
>Midi In</TH
><TH
>Mixer (and Aux)</TH
></TR
></THEAD
><TBODY
><TR
><TD
>OSS</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>Yes</TD
></TR
><TR
><TD
>ALSA</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>Yes</TD
></TR
><TR
><TD
>aRts</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>No</TD
><TD
>No</TD
><TD
>Yes</TD
></TR
><TR
><TD
>ESD</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>No</TD
><TD
>No</TD
><TD
>No</TD
></TR
><TR
><TD
>NAS</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>No</TD
><TD
>No</TD
><TD
>Yes</TD
></TR
><TR
><TD
>AudioIO</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>No</TD
><TD
>No</TD
><TD
>Yes</TD
></TR
><TR
><TD
>Jack</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>No</TD
><TD
>No</TD
><TD
>Yes</TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
><P
>	  Lots of listed drivers won't support Midi (in a short time)
	  because the exposed "Un*x" native interfaces don't. This
	  would require using some kind as software synthesis (as
	  Timidity), but we cannot incorporate as it's GPL'ed.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4505"
>Wave mapper (msacm.drv)</A
></H3
><P
>	  The Wave mapper device allows to load on-demand audio codecs
	  in order to perform software conversion for the types the
	  actual low level driver (hardware). Those codecs are
	  provided through the standard ACM drivers in MSACM32.DLL.
	</P
><P
>	  Wave mapper driver implementation can be found in
	  <TT
CLASS="FILENAME"
>dlls/winmm/wavemap/</TT
> directory. This
	  driver heavily relies on MSACM and MSACM32 DLLs which can be
	  found in <TT
CLASS="FILENAME"
>dlls/msacm</TT
> and
          <TT
CLASS="FILENAME"
>dlls/msacm32</TT
>. Those DLLs load ACM
          drivers which provide the conversion to PCM format (which is
          normally supported by low level drivers). A Law, uLaw,
          ADPCM, MP3... fit into the category of non PCM formats. 
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4512"
>MIDI mapper (midimap.drv)</A
></H3
><P
>	  Midi mapper allows to map each one of 16 MIDI channels to a
	  specific instrument on an installed sound card. This allows
	  for example to support different MIDI instrument definitions
	  (XM, GM...). It also permits to output on a per channel
	  basis to different MIDI renderers.
	</P
><P
>	  A built-in MIDI mapper can be found in
	  <TT
CLASS="FILENAME"
>dlls/winmm/midimap/</TT
>. It partly provides
	  the same functionality as the Windows' one. It allows to
	  pick up destination channels: you can map a given channel to
	  a specific playback device channel (see the configuration
	  bits for more details).
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MM-MCI"
>Mid level drivers (MCI)</A
></H2
><P
>	The mid level drivers are represented by some common API
	functions, mostly <TT
CLASS="FUNCTION"
>mciSendCommand</TT
> and
	<TT
CLASS="FUNCTION"
>mciSendString</TT
>. Wine implements several
	MCI mid level drivers.
      </P
><P
>	<DIV
CLASS="TABLE"
><A
NAME="AEN4523"
></A
><P
><B
>Table 2. Wine MCI drivers</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>MCI Name</TH
><TH
>DLL Name</TH
><TH
>Role</TH
><TH
>Location</TH
><TH
>Comments</TH
></TR
></THEAD
><TBODY
><TR
><TD
>CdAudio</TD
><TD
>MciCDA.drv</TD
><TD
>MCI interface to a CD audio player</TD
><TD
><TT
CLASS="FILENAME"
>dlls/winmm/mcicda/</TT
></TD
><TD
>		  Relies on NTDLL CdRom raw interface (through
		  <TT
CLASS="FUNCTION"
>DeviceIoControl</TT
>). 
		</TD
></TR
><TR
><TD
>WaveAudio</TD
><TD
>MciWave.drv</TD
><TD
>		  MCI interface for wave playback and record
		</TD
><TD
><TT
CLASS="FILENAME"
>dlls/winmm/mciwave/</TT
></TD
><TD
>It uses the low level audio API.</TD
></TR
><TR
><TD
>Sequencer</TD
><TD
>MciSeq.drv</TD
><TD
>Midi Sequencer (playback)</TD
><TD
><TT
CLASS="FILENAME"
>dlls/winmm/mciseq/</TT
></TD
><TD
>It uses the low level midi APIs</TD
></TR
><TR
><TD
>AviVideo</TD
><TD
>MciAvi.drv</TD
><TD
>AVI playback and record</TD
><TD
><TT
CLASS="FILENAME"
>dlls/winmm/mciavi/</TT
></TD
><TD
>		  It rather heavily relies on MSVIDEO/MSVFW32 DLLs
		  pair to work.
		</TD
></TR
></TBODY
></TABLE
></DIV
>
	The MCI Name column is the name of the MCI driver, as it is
	searched in configuration. The DLL Name column is the name of
	the DLL the configuration provides as a value. The name listed
	here is the default one (see the configuration section for the
	details).
      </P
><P
>	Adding a new MCI driver is just a matter of writing the
	corresponding DLL with the correct interface (see existing MCI
	drivers for the details), and to provide the relevant setup
	information for <TT
CLASS="FILENAME"
>wine.inf</TT
>
      </P
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MM-HIGH"
>High level layers</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN4567"
>WINMM (and MMSYSTEM)</A
></H3
><P
>	  The high level layers encompass basically the MMSYSTEM and
	  WINMM DLLs exported APIs. It also provides the skeleton for
	  the core functionality for multimedia playback and
	  recording. Note that native MMSYSTEM and WINMM do not
	  currently work under Wine and there is no plan to support
	  them (it would require to also fully support VxD, which is
	  not done yet).
	</P
><P
>	  WINMM and MMSYSTEM in Wine can handle both 16 bit and 32 bit
	  drivers (for low level and MCI drivers). It will handle all
	  the conversions transparently for the all the calls to WINMM
	  and MMSYSTEM, as it knows what the driver interface is (16
	  bit or 32 bit) and it manages the information sent
	  accordingly.
	</P
><P
>	  MCI drivers are seen as regular Wine modules, and can be
	  loaded (with a correct load order between builtin, native),
	  as any other DLL. Please note, that MCI drivers module names
	  must bear the <TT
CLASS="FILENAME"
>.drv</TT
> extension to be
	  correctly understood.
	</P
><P
>	  Multimedia timers are implemented with a dedicated thread,
	  run in the context of the calling process, which should
	  correctly mimic Windows behavior. The only drawback is that
	  the thread will appear the calling process if it enumerates
	  the running processes.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4574"
>DSOUND</A
></H3
><P
>	  Wine also provide a DSound (DirectX) DLL with the proper
	  COM implementation.
	</P
><P
>	  Note that a Wine specific flag has been added to the
	  <TT
CLASS="FUNCTION"
>wodOpen</TT
> function, so that the DSound
	  DLL can get a reference to a COM sound object from a given
	  WINMM wave output device. This should be changed in the
	  future.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MSACM"
>MS ACM Dlls</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN4581"
>Contents</A
></H3
><P
>	  The MSACM32 (and its 16 bit sibbling MSACM) provide a way to
	  map a given wave format to another format. It also provides
	  filtering capabilities. Those DLLs only implement the proper
	  switch between a caller and a driver providing the
	  implementation of the requested format change or filter
	  operation.
	</P
><P
>	  There's nothing specific in Wine's implementation compared
	  to Windows' one. Here's however a list of the builtin format
	  change drivers (there's no filter driver yet):
	  <DIV
CLASS="TABLE"
><A
NAME="AEN4585"
></A
><P
><B
>Table 3. Wine ACM drivers</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Name</TH
><TH
>Provides</TH
></TR
></THEAD
><TBODY
><TR
><TD
>imaadp32</TD
><TD
>IMA ADPCM (adaptative PCM)</TD
></TR
><TR
><TD
>msadp32</TD
><TD
>Microsoft's ADPCM (adaptative PCM)</TD
></TR
><TR
><TD
>msg711</TD
><TD
>Microsoft's G.711 (A-Law and ?-Law)</TD
></TR
><TR
><TD
>winemp3</TD
><TD
>		    Wine's MP3 (MPEG Layer 3), based on mpglib library
		  </TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
><P
>	  Note that Wine also supports native audio codecs as
	  well.
	</P
><P
>	  All builtin ACM drivers are 32 bit Unicode DLLs
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4607"
>Caching</A
></H3
><P
>	  The MSACM/MSACM32 keeps some data cached for all known ACM
	  drivers. Under the key
          <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	  Software\Microsoft\AudioCompressionManager\DriverCache\&#60;driver name&#62;
	  </PRE
></TD
></TR
></TABLE
>
	  , are kept for values:
	  <P
></P
><UL
><LI
><P
>		<TT
CLASS="LITERAL"
>aFormatTagCache</TT
> which
		contains an array of <TT
CLASS="LITERAL"
>DWORD</TT
>. There
		are two <TT
CLASS="LITERAL"
>DWORD</TT
>s per <TT
CLASS="LITERAL"
>cFormatTags</TT
> 
		entry. The first <TT
CLASS="LITERAL"
>DWORD</TT
> contains a
		format tag value, and the second the associated
		maximum size for a <TT
CLASS="LITERAL"
>WAVEFORMATEX</TT
> structure.
		(Fields <TT
CLASS="LITERAL"
>dwFormatTag</TT
> and <TT
CLASS="LITERAL"
>cbFormatSize</TT
> from
		ACMFORMATDETAILS)
	      </P
></LI
><LI
><P
>		<TT
CLASS="LITERAL"
>cFilterTags</TT
> contains the number of tags supported by the driver
		for filtering.
	      </P
></LI
><LI
><P
>		<TT
CLASS="LITERAL"
>cFormatTags</TT
> contains the number of tags support
		by the driver for conversions.
	      </P
></LI
><LI
><P
>		<TT
CLASS="LITERAL"
>fdwSupport</TT
> (the same as the one returned from
		<TT
CLASS="FUNCTION"
>acmDriverDetails</TT
>).
	      </P
></LI
></UL
>
	</P
><P
>	  The <TT
CLASS="LITERAL"
>cFilterTags</TT
>,
	  <TT
CLASS="LITERAL"
>cFormatTags</TT
>,
	  <TT
CLASS="LITERAL"
>fdwSupport</TT
> are the same values as the
	  ones returned from <TT
CLASS="FUNCTION"
>acmDriverDetails</TT
> 
	  function.
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MSVFW32"
>MS Video Dlls</A
></H2
><DIV
CLASS="SECT2"
><H3
CLASS="SECT2"
><A
NAME="AEN4639"
>Contents</A
></H3
><P
>	  The MSVFW32 (and its 16 bit sibbling MSVIDEO) provide
	  encode/decode video streams. Those DLLs only implement the
	  proper switch between a caller and a driver providing the
	  implementation of the requested format coding/decoding
	  operation.
	</P
><P
>	  There's nothing specific in Wine's implementation compared
	  to Windows' one. Here's however a list of the builtin
	  decoding drivers:
	  <DIV
CLASS="TABLE"
><A
NAME="AEN4643"
></A
><P
><B
>Table 4. Wine VIDC drivers</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Name</TH
><TH
>Provides</TH
></TR
></THEAD
><TBODY
><TR
><TD
>msrle32</TD
><TD
>Microsoft's RLE (Run-Length encoded)</TD
></TR
><TR
><TD
>msvidc32</TD
><TD
>Microsoft's Video-1</TD
></TR
><TR
><TD
>iccvid</TD
><TD
>Radius Cinepak Video Decoder</TD
></TR
></TBODY
></TABLE
></DIV
>
	</P
><P
>	  Note that Wine also supports native video codecs as well.
	</P
><P
>	  All builtin VIDC drivers are 32 bit Unicode DLLs
	</P
></DIV
></DIV
><DIV
CLASS="SECT1"
><HR><H2
CLASS="SECT1"
><A
NAME="MM-CONF"
>Multimedia configuration</A
></H2
><P
>	Unfortunately, multimedia configuration evolved over time:
	<P
></P
><UL
><LI
><P
>	      In the early days on Windows 3.x, configuration was
	      stored in <TT
CLASS="FILENAME"
>system.in</TT
> file, under
	      various sections (<TT
CLASS="LITERAL"
>[drivers]</TT
> for low
	      level drivers, <TT
CLASS="LITERAL"
>[mci]</TT
>
	      (resp. <TT
CLASS="LITERAL"
>[mci32]</TT
>) for 16 bit (resp. 32
	      bit) MCI drivers...).
	    </P
></LI
><LI
><P
>	      With the apparition of the registry, in Windows 95,
	      configuration as been duplicated there, under the key
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>HKLM\System\CurrentControlSet\Control\MediaResources</PRE
></TD
></TR
></TABLE
>
	    </P
></LI
><LI
><P
>	      Windows NT also adopted the registry, but decided to
	      store the configuration information under another key
	      than Windows 9x did.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
>HKLM\Software\Microsoft\Windows NT\CurrentVersion</PRE
></TD
></TR
></TABLE
>
	      And with a different layout of keys and values beneath
	      this key.
	    </P
></LI
></UL
>
      </P
><P
>	Currently, Wine tries to load first a driver (low-level or
	MCI) from the NT registry settings. If it fails, it will try
	the <TT
CLASS="FILENAME"
>system.ini</TT
> configuration.
      </P
><P
>	An out-of-the-box configuration is provided in
	<TT
CLASS="FILENAME"
>wine.inf</TT
>, and shall be stored in registry
	and <TT
CLASS="FILENAME"
>system.ini</TT
> at Wine installation
        time. It will setup correctly the MCI drivers' configuration
	(as well as the wave and MIDI mappers). As the low-level
	drivers depend on hardware, their setup will be handled by
	<TT
CLASS="FILENAME"
>winecfg</TT
>.
      </P
><DIV
CLASS="TABLE"
><A
NAME="AEN4684"
></A
><P
><B
>Table 5. Wine multimedia configuration scheme</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
CELLSPACING="0"
CELLPADDING="4"
CLASS="CALSTABLE"
><THEAD
><TR
><TH
>Driver</TH
><TH
>Read from NT registry</TH
><TH
>Read from <TT
CLASS="FILENAME"
>system.ini</TT
></TH
><TH
>Setup by <TT
CLASS="FILENAME"
>wine.inf</TT
></TH
><TH
>Setup by <TT
CLASS="FILENAME"
>winecfg</TT
></TH
></TR
></THEAD
><TBODY
><TR
><TD
>MCI drivers</TD
><TD
>Yes (1)</TD
><TD
>Yes (2)</TD
><TD
>Yes</TD
><TD
>No</TD
></TR
><TR
><TD
>Wave and MIDI mappers</TD
><TD
>Yes</TD
><TD
>No</TD
><TD
>Yes</TD
><TD
>No</TD
></TR
><TR
><TD
>Hardware-bound low level drivers</TD
><TD
>Yes</TD
><TD
>No</TD
><TD
>No</TD
><TD
>Yes</TD
></TR
><TR
><TD
>ACM and VIDC drivers (audio &#38; video codecs)</TD
><TD
>No</TD
><TD
>Yes</TD
><TD
>Yes</TD
><TD
>No</TD
></TR
></TBODY
></TABLE
></DIV
><P
>	This will allow most settings to be correctly loaded and
	handled. However, it won't if an app tries to search directly
	the registry for the actual configuration, as the three
	potential configuration places may not be in sync.
      </P
><P
>	It still lacks a correct installation scheme (as any
	multimedia device under Windows), so that all the correct
	keys are created in the registry. This requires an advanced
	model since, for example, the number of wave out devices can
	only be known on the destination system (depends on the
	sound card driven by the OSS interface).
      </P
><P
>	The following sections describe which type of information
	(depending on the location) Wine's multimedia DLLs understand.
      </P
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4725"
>NT configuration</A
></H3
><P
>	  Under the 
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>HKLM\Software\Microsoft\Windows NT\CurrentVersion
	  </PRE
></TD
></TR
></TABLE
>
	  key, are stored the names of the DLLs to be loaded for each
	  MCI driver name:
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	    "cdaudio"="mcicda.drv"
	    "sequencer"="mciseq.drv"
	    "waveaudio"="mciwave.drv"
	    "avivideo"="mciavi.drv"
	    "videodisc"="mcipionr.drv"
	    "vcr"="mcivisca.drv"
	    "MPEGVideo"="mciqtz.drv"
	  </PRE
></TD
></TR
></TABLE
>
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4730"
><TT
CLASS="FILENAME"
>system.ini</TT
></A
></H3
><P
>	  Wine will read the MCI drivers from the
	  <TT
CLASS="LITERAL"
>[mci]</TT
> or <TT
CLASS="LITERAL"
>[mci32]</TT
>
	  section. Wine won't make any difference between the two.
	</P
><P
>	  Here's a sample configuration:
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>   [mci]
   cdaudio=mcicda.drv
   sequencer=mciseq.drv
   waveaudio=mciwave.drv
   avivideo=mciavi.drv
   videodisc=mcipionr.drv
   vcr=mcivisca.drv
   MPEGVideo=mciqtz.drv
	  </PRE
></TD
></TR
></TABLE
>
	</P
><P
>	  ACM drivers' configuration is read (only so far) from the
	  <TT
CLASS="FILENAME"
>system.ini</TT
> (and setup at Wine
	  installation from the <TT
CLASS="FILENAME"
>wine.inf</TT
> file).
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    [drivers32]
    MSACM.imaadpcm=imaadp32.acm
    MSACM.msadpcm=msadp32.acm
    MSACM.msg711=msg711.acm
    MSACM.winemp3=winemp3.acm
	  </PRE
></TD
></TR
></TABLE
>
	</P
><P
>	  Video (aka vidc) drivers' configuration is read (only so
	  far) from the <TT
CLASS="FILENAME"
>system.ini</TT
> (and setup at
	  Wine installation from the <TT
CLASS="FILENAME"
>wine.inf</TT
>
	  file).
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>    [drivers32]
    VIDC.MRLE=msrle32.dll
    VIDC.MSVC=msvidc32.dll
    VIDC.CVID=iccvid.dll
	  </PRE
></TD
></TR
></TABLE
>
	</P
><P
>	  See also the configuration part of the User's Guide for
	  other information on low level drivers.
	</P
></DIV
><DIV
CLASS="SECT2"
><HR><H3
CLASS="SECT2"
><A
NAME="AEN4747"
>Per driver/DLL configuration</A
></H3
><DIV
CLASS="SECT3"
><H4
CLASS="SECT3"
><A
NAME="AEN4749"
>Midi mapper</A
></H4
><P
>	    The Midi mapper configuration is the same as on Windows
	    9x. Under the key:
	  <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Multimedia\MIDIMap
	  </PRE
></TD
></TR
></TABLE
>
	    if the <TT
CLASS="LITERAL"
>UseScheme</TT
> value is not set, or
	    is set to a null value, the MIDI mapper will always use
	    the driver identified by the
	    <TT
CLASS="LITERAL"
>CurrentInstrument</TT
> value. Note: Wine
	    (for simplicity while installing) allows to define
	    <TT
CLASS="LITERAL"
>CurrentInstrument</TT
> as
	    <TT
CLASS="LITERAL"
>#n</TT
> (where n is a number), whereas
	    Windows only allows the real device name here. If
	    <TT
CLASS="LITERAL"
>UseScheme</TT
> is set to a non null value,
	    <TT
CLASS="LITERAL"
>CurrentScheme</TT
> defines the name of the
	    scheme to map the different channels.  All the schemes are
	    available with keys like 
	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\MediaProperties\PrivateProperties\Midi\Schemes\%name_of_scheme%
	    </PRE
></TD
></TR
></TABLE
>
	    For every scheme, under this key, will be a sub-key (which
	    name is usually a two digit index, starting at 00). Its
	    default value is the name of the output driver, and the
	    value <TT
CLASS="LITERAL"
>Channels</TT
> lists all channels (of
	    the 16 standard MIDI ones) which have to be copied to this
	    driver.
	  </P
><P
>	    To provide enhanced configuration and mapping
	    capabilities, each driver can define under the key
	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\MediaProperties\PrivateProperties\Midi\Ports\%driver_name%
	    </PRE
></TD
></TR
></TABLE
>
	    a link to and <TT
CLASS="FILENAME"
>.IDF</TT
> file which allows
	    to remap channels internally (for  example 9 -&#62; 16), to
	    change instruments identification, event controllers
	    values. See the source file
	    <TT
CLASS="FILENAME"
>dlls/winmm/midimap/midimap.c</TT
> for the
	    details (this isn't implemented yet).
	  </P
></DIV
></DIV
></DIV
></DIV
></DIV
></DIV
></BODY
></HTML
>
