2 <!-- FIXME: note that you can link PE DLLs to Winelib apps -->
3 <title id=
"bindlls.title">Building WineLib DLLs
</title>
4 <sect1 id=
"bindlls-intro">
5 <title id=
"binary-dlls-intro.title">Introduction
</title>
7 For one reason or another you may find yourself with a Linux
8 library that you want to use as if it were a Windows Dll. There are
9 various reasons for this including the following:
13 You are porting a large application that uses several third-party
14 libraries. One is available on Linux but you are not yet ready
15 to link to it directly as a Linux shared library.
20 There is a well-defined interface available and there are several
21 Linux solutions that are available for it
22 (e.g. the ODBC interface in Wine).
27 You have a binary only Windows application that can be extended
28 through plugins, such as a text editor or IDE.
34 The process for dealing with these situations is actually quite simple.
35 You need to write a spec file that will describe the library's
36 interface in the same format as a Dll (primarily what functions it
37 exports). Also you will want to write a small wrapper around the
38 library. You combine these to form a Wine built-in Dll that links to the
39 Linux library. Then you modify the DllOverrides in the wine config
40 file to ensure that this new built-in DLL is called rather than any
44 In this section we will look at two examples. The first example is
45 extremely simple and leads into the subject in
"baby steps". The
46 second example is the ODBC interface proxy in Wine. The files to which
47 we will refer for the ODBC example are currently in the
48 <filename class=
"Directory">dlls/odbc32
</filename> directory of the
52 The first example is based very closely on a real case (the names
53 of the functions etc. have been changed to protect the innocent).
54 A large Windows application includes a DLL that links to a third-party
55 DLL. For various reasons the third-party DLL does not work too well
56 under Wine. However the third-party library is also available for the
57 Linux environment. Conveniently the DLL and Linux shared library
58 export only a small number of functions and the application only uses
62 Specifically, the application calls a function:
64 signed short WINAPI MyWinFunc (unsigned short a, void *b, void *c,
65 unsigned long *d, void *e, int f, char g, unsigned char *h);
67 and the linux library exports a corresponding function:
69 signed short MyLinuxFunc (unsigned short a, void *b, void *c,
70 unsigned short *d, void *e, char g, unsigned char *h);
75 <sect1 id=
"bindlls-spec">
76 <title id=
"bindlls-spec.title">Writing the spec file
</title>
78 Start by writing the spec file. This file will describe the interface
79 as if it were a DLL. See elsewhere for the details of the format of
80 a spec file (e.g. man winebuild).
83 In the simple example we want a Wine built-in Dll that corresponds to
84 the MyWin Dll. The spec file is
<filename>MyWin.dll.spec
</filename> and
85 looks something like this:
88 # File: MyWin.dll.spec
90 # some sort of copyright
92 # Wine spec file for the MyWin.dll built-in library (a minimal wrapper around the
93 # linux library libMyLinux)
95 # For further details of wine spec files see the Winelib documentation at
98 2 stdcall MyWinFunc (long ptr ptr ptr ptr long long ptr) MyProxyWinFunc
102 Notice that the arguments are flagged as long even though they are
103 smaller than that. With this example we will link directly to the
104 Linux shared library whereas with the ODBC example we will load the
105 Linux shared library dynamically.
108 In the case of the ODBC example you can see this in the file
109 <filename>odbc32.spec
</filename>.
113 <sect1 id=
"bindlls-wrapper">
114 <title id=
"bindlls-wrapper.title">Writing the wrapper
</title>
116 Firstly we will look at the simple example. The main complication of
117 this case is the slightly different argument lists. The f parameter
118 does not have to be passed to the Linux function and the d parameter
119 (theoretically) has to be converted between
120 <literal>unsigned long *i
</literal> and
<literal>unsigned short *
</literal>.
121 Doing this ensures that the
"high" bits of the returned value are set
122 correctly. Also unlike with the ODBC example we will link directly to
123 the Linux Shared Library.
128 * Copyright (c) The copyright holder.
130 * Basic Wine wrapper for the Linux
<3rd party library
> so that it can be
131 * used by
<the application
>
133 * Currently this file makes no attempt to be a full wrapper for the
<3rd
134 * party library
>; it only exports enough for our own use.
136 * Note that this is a Unix file; please don't go converting it to DOS format
137 * (e.g. converting line feeds to Carriage return/Line feed).
139 * This file should be built in a Wine environment as a WineLib library,
140 * linked to the Linux
<3rd party
> libraries (currently libxxxx.so and
144 #include
< <3rd party linux header
> >
145 #include
<windef.h
> /* Part of the Wine header files */
147 /* This declaration is as defined in the spec file. It is deliberately not
148 * specified in terms of
<3rd party
> types since we are messing about here
149 * between two operating systems (making it look like a Windows thing when
150 * actually it is a Linux thing). In this way the compiler will point out any
152 * For example the fourth argument needs care
154 signed short WINAPI MyProxyWinFunc (unsigned short a, void *b, void *c,
155 unsigned long *d, void *e, int f, char g, unsigned char *h)
160 d1 = (unsigned short) *d;
161 ret =
<3rd party linux function
> (a, b, c,
&d1, e, g, h);
171 For a more extensive case we can use the ODBC example. This is
172 implemented as a header file
173 (
<filename class=
"HeaderFile">proxyodbc.h
</filename>) and the actual
174 C source file (
<filename>proxyodbc.c
</filename>). Although the file
175 is quite long it is extremely simple in structure.
178 <function>DllMain
</function> the function is used to initialize the DLL.
179 On the process attach event the function dynamically links to the
180 desired Linux ODBC library (since there are several available) and
181 builds a list of function pointers. It unlinks on the process
185 Then each of the functions simply calls the appropriate Linux function
186 through the function pointer that was set up during initialization.
190 <sect1 id=
"bindlls-building">
191 <title id=
"binary-dlls-building.title">Building
</title>
193 So how do we actually build the Wine built-in Dll? The easiest way is
194 to get Winemaker to do the hard work for us. For the simple example we
195 have two source files (the wrapper and the spec file). We also have
196 the
3rd party header and library files of course.
199 Put the two source files in a suitable directory and then use
200 winemaker to create the build framework, including configure script,
201 makefile etc. You will want to use the following options of
206 --nosource-fix and --nogenerate-specs (requires winemaker version
207 0.5.8 or later) to ensure that the two files are not modified.
208 (If using an older version of winemaker then make the two files
209 readonly and ignore the complaints about being unable to modify
215 --dll --single-target MyWin --nomfc to specify the target
220 -DMightNeedSomething -I3rd_party_include -L3rd_party_lib -lxxxx
221 -lyyyy where these are the locations of the header files etc.
227 After running winemaker I like to edit the Makefile.in to add the line
228 CEXTRA = -Wall just before the DEFINES =.
231 Then simply run the configure and make as normal (described elsewhere).
235 <sect1 id=
"bindlls-installing">
236 <title id=
"binary-dlls-installing.title">Installing
</title>
238 So how do you install the proxy and ensure that everything connects up
239 correctly? You have quite a bit of flexibility in this area so what
240 follows are not the only options available.
243 Ensure that the actual Linux Shared Object is placed somewhere where
244 the Linux system will be able to find it. Typically this means it
245 should be in one of the directories mentioned in the /etc/ld.so.conf
246 file or somewhere in the path specified by LD_LIBRARY_PATH. If you
247 can link to it from a Linux program it should be OK.
250 Put the proxy shared object (MyWin.dll.so) in the same place as the
251 rest of the built-in DLLs. (If you used winemaker to set up your build
252 environment then running
"make install" as root should do that for you)
253 Alternatively ensure that WINEDLLPATH includes the directory containing
254 the proxy shared object.
257 If you have both a Windows DLL and a Linux DLL/proxy pair then you will
258 have to ensure that the correct one gets called. The easiest way is
259 probably simply to rename the windows version so that it doesn't get
260 detected. Alternatively you could specify in the DllOverrides section
261 (or the AppDefaults\\myprog.exe\\DllOverrides section) of the config
262 file (in your .wine directory) that the built-in version be used. Note
263 that if the Windows version Dll is present and is in the same
264 directory as the executable (as opposed to being in the Windows
265 directory) then you will currently need to specify the whole path to
266 the dll, not merely its name.
269 Once you have done this you should be using the Linux Shared Object
270 successfully. If you have problems then set the WINEDEBUG=+module
271 environment variable before running wine to see what is actually happening.
275 <sect1 id=
"bindlls-filenames">
276 <title id=
"binary-dlls-filenames.title">Converting filenames
</title>
278 Suppose you want to convert incoming DOS format filenames to their
279 Unix equivalent. Of course there is no suitable function in the true
280 Microsoft Windows API, but wine provides a function for just this
281 task and exports it from its copy of the kernel32 DLL. The function
282 is
<function>wine_get_unix_file_name
</function> (defined in winbase.h).
287 <!-- Keep this comment at the end of the file
290 sgml-parent-document:("winelib-user.sgml" "book" "chapter" "")