2 <title id="bindlls.title">Using Linux libraries as DLLs</title>
3 <sect1 id="bindlls-intro">
4 <title id="binary-dlls-intro.title">Introduction</title>
6 For one reason or another you may find yourself with a Linux shared
7 library that you want to use as if it were a Windows Dll. There are
8 various reasons for this including the following:
12 You are porting a large application that uses several third-party
13 libraries. One is available on Linux but you are not yet ready
14 to link to it directly as a Linux shared library.
19 There is a well-defined interface available and there are several
20 Linux solutions that are available for it.
21 (The ODBC interface in Wine)
26 You wish to do something that you can do in Linux but Wine does
33 The process for dealing with these situations is actually quite simple.
34 You need to write a spec file that will describe the library's
35 interface in the same format as a Dll (primarily what functions it
36 exports). Also you will want to write a small wrapper around the
37 library. You combine these to form a Wine built-in Dll that links to the
38 Linux library. Then you modify the DllOverrides in the wine config
39 file to ensure that this new built-in DLL is called rather than any
43 In this section we will look at two examples. The first example is
44 extremely simple and leads into the subject in "baby steps". The
45 second example is the ODBC interface proxy in Wine. The files to which
46 we will refer for the ODBC example are currently in the
47 <filename class="Directory">dlls/odbc32</filename> directory of the
51 The first example is based very closely on a real case (the names
52 of the functions etc. have been changed to protect the innocent).
53 A large Windows application includes a DLL that links to a third-party
54 DLL. For various reasons the third-party DLL does not work too well
55 under Wine. However the third-party DLL is also available for the
56 Linux environment. Conveniently the DLL and Linux shared library
57 export only a small number of functions and the application only uses
61 Specifically, the application calls a function:
63 signed short WINAPI MyWinFunc (unsigned short a, void *b, void *c,
64 unsigned long *d, void *e, unsigned char f, char g,
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 (depending on changes to the way that the
86 specfile is formatted since this was written).
89 # File: MyWin.dll.spec
91 # some sort of copyright
93 # Wine spec file for the MyWin.dll built-in library (a minimal wrapper around the
94 # linux library libMyLinux)
96 # For further details of wine spec files see the Winelib documentation at
99 2 stdcall MyWinFunc (long ptr ptr ptr ptr long long ptr) MyProxyWinFunc
103 Notice that the arguments are flagged as long even though they are
105 Notice also that we do not specify an initial function. With this
106 example we will link directly to the Linux shared library whereas
107 with the ODBC example we will load the Linux shared library dynamically.
110 In the case of the ODBC example you can see this in the file
111 <filename>odbc32.spec</filename>.
115 <sect1 id="bindlls-cxx-apis">
116 <title id="bindlls-cxx-apis.title">How to deal with C++ APIs</title>
118 names are mangled, how to demangle them, how to call them
122 <sect1 id="bindlls-wrapper">
123 <title id="bindlls-wrapper.title">Writing the wrapper</title>
125 Firstly we will look at the simple example. The main complication of
126 this case is the slightly different argument lists. The f parameter
127 does not have to be passed to the Linux function and the d parameter
128 (theoretically) has to be converted between unsigned long * and
129 unsigned short *. Doing this ensures that the "high" bits of the
130 returned value are set correctly. Also unlike with the ODBC example we
131 will link directly to the Linux Shared Library.
136 * Copyright (c) The copyright holder.
138 * Basic Wine wrapper for the Linux <3rd party library> so that it can be
139 * used by <the application>
141 * Currently this file makes no attempt to be a full wrapper for the <3rd
142 * party library>; it only exports enough for our own use.
144 * Note that this is a Unix file; please don't go converting it to DOS format
145 * (e.g. converting line feeds to Carriage return/Line feed).
147 * This file should be built in a Wine environment as a WineLib library,
148 * linked to the Linux <3rd party> libraries (currently libxxxx.so and
152 #include < <3rd party linux header> >
153 #include <windef.h> /* Part of the Wine header files */
155 signed short WINAPI MyProxyWinFunc (unsigned short a, void *b, void *c,
156 unsigned long *d, void *e, unsigned char f, char g,
158 /* This declaration is as defined in the spec file. It is deliberately not
159 * specified in terms of <3rd party> types since we are messing about here
160 * between two operating systems (making it look like a Windows thing when
161 * actually it is a Linux thing). In this way the compiler will point out any
163 * For example the fourth argument needs care
169 d1 = (unsigned short) *d;
170 ret = <3rd party linux function> (a, b, c, &d1, e, g, h);
180 For a more extensive case we can use the ODBC example. This is
181 implemented as a header file
182 (<filename class="HeaderFile">proxyodbc.h</filename>) and the actual
183 C source file (<filename>proxyodbc.c</filename>). Although the file
184 is quite long it is extremely simple in structure.
187 The MAIN_OdbcInit function is the function that was named in the
188 <link linkend="bindlls-spec">spec file</link> as the init function.
189 On the process attach event the function dynamically links to the
190 desired Linux ODBC library (since there are several available) and
191 builds a list of function pointers. It unlinks on the process
195 Then each of the functions simply calls the appropriate Linux function
196 through the function pointer that was set up during initialisation.
200 <sect1 id="bindlls-building">
201 <title id="binary-dlls-building.title">Building</title>
203 So how do we actually build the Wine built-in Dll? The easiest way is
204 to get Winemaker to do the hard work for us. For the simple example we
205 have two source files (the wrapper and the spec file). We also have
206 the 3rd party header and library files of course.
209 Put the two source files in a suitable directory and then use
210 winemaker to create the build framework, including configure script,
211 makefile etc. You will want to use the following options of
216 --nosource-fix and --nogenerate-specs (requires winemaker version
217 0.5.8 or later) to ensure that the two files are not modified.
218 (If using an older version of winemaker then make the two files
219 readonly and ignore the complaints about being unable to modify
225 --dll --single-target MyWin --nomfc to specify the target
230 -DMightNeedSomething -I3rd_party_include -L3rd_party_lib -lxxxx
231 -lyyyy where these are the locations of the header files etc.
237 After running winemaker I like to edit the Makefile.in to add the line
238 CEXTRA = -Wall just before the DEFINES =.
241 Then simply run the configure and make as normal (described elsewhere).
245 <sect1 id="bindlls-installing">
246 <title id="binary-dlls-installing.title">Installing</title>
248 So how do you install the proxy and ensure that everything connects up
249 correctly? You have quite a bit of flexibility in this area so what
250 follows are not the only options available.
253 Ensure that the actual Linux Shared Object is placed somewhere where
254 the Linux system will be able to find it. Typically this means it
255 should be in one of the directories mentioned in the /etc/ld.so.conf
256 file or somewhere in the path specified by LD_LIBRARY_PATH. If you
257 can link to it from a Linux program it should be OK.
260 Put the proxy shared object (MyWin.dll.so) in the same place as the
261 rest of the built-in DLLs. (If you used winemaker to set up your build
262 environment then running "make install" as root should do that for you)
263 Alternatively ensure that WINEDLLPATH includes the directory containing
264 the proxy shared object.
267 If you have both a Windows DLL and a Linux DLL/proxy pair then you will
268 have to ensure that the correct one gets called. The easiest way is
269 probably simply to rename the windows version so that it doesn't get
270 detected. Alternatively you could specify in the DllOverrides section
271 (or the AppDefaults\\myprog.exe\\DllOverrides section) of the config
272 file (in your .wine directory) that the built-in version be used. Note
273 that if the Windows version Dll is present and is in the same
274 directory as the executable (as opposed to being in the Windows
275 directory) then you will currently need to specify the whole path to
276 the dll, not merely its name.
279 Once you have done this you should be using the Linux Shared Object
280 successfully. If you have problems then use the --debugmsg +module
281 options to wine to see what is actually happening.
285 <sect1 id="bindlls-advanced">
286 <title id="binary-dlls-advanced.title">Advanced options</title>
288 Here are a few more advanced options.
290 <sect2 id="bindlls-adv-filenames">
291 <title id="binary-dlls-adv-filenames.title">Converting filenames</title>
293 Suppose you want to convert incoming DOS format filenames to their
294 Unix equivalent. Of course there is no suitable function in the true
295 Microsoft Windows API, but wine provides a function for just this
296 task and exports it from its copy of the kernel32 DLL. The function
297 is wine_get_unix_file_name (defined in winbase.h). Use the -ikernel32
298 option to winemaker to link to it.
304 <!-- Keep this comment at the end of the file
307 sgml-parent-document:("wine-doc.sgml" "book" "chapter" "")