[Haskell-cafe] Re: FFI and DLLs

Simon Marlow simonmarhaskell at gmail.com
Tue Sep 4 05:47:37 EDT 2007


Lewis-Sandy, Darrell wrote:
> An early proposal for the FFI supported importing functions directly 
> from dynamic link libraries:
> 
> www.*haskell*.org/hdirect/ffi-a4.ps.gz 
> <http://www.haskell.org/hdirect/ffi-a4.ps.gz>
> 
> This looks like it was dropped from the final version of the addendum in 
> favor of C header files as the sole form of import entities.  Not being 
> a C programmer, how would one go about importing a foreign function 
> (e.g. void Foo(void) ) from a dynamic link library (e.g. foo.dll)??   
> Would someone be willing to provide an explicit example?

You don't have to do anything special to import a function from a DLL. 
e.g. here's one from the Win32 library:

foreign import stdcall unsafe "windows.h CloseHandle"
   c_CloseHandle :: HANDLE -> IO Bool

The windows.h header file isn't essential unless you're compiling via C 
with GHC, when compiling direct to native code with -fasm the header file 
is ignored.  Note that by convention most functions in DLLs are called 
using the stdcall convention, so you have to specify this on the foreign 
import.

To link against the DLL, the easiest way is to name the DLL directly on the 
command line.

Cheers,
	Simon


More information about the Haskell-Cafe mailing list