Calling haskell DLL from MSVC++

Mauricio Carvalho m.decarvalho@telia.com
Sun, 1 Dec 2002 14:12:39 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0021_01C29943.B50298A0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hey!
I' having problems to call functions from a DLL made with ghc with a C++ =
program, using Microsofts Visual C++
To learn how to do it, I tried the example found at the user's guide =
(11.3, 11.4), but things aren't working out..=20
Here is as far as I've done:
I have a haskell module, MyModule.hs:module MyModule where
foreign export stdcall fac :: Int -> Int
fac :: Int -> Int
fac n
 | n <=3D 0  =3D 1
 | otherwise  =3D n * fac(n-1)
I compile it with:  ghc -c MyModule.hs -fffi

The .c file, containing the DLL initialization is:
#include <windows.h>
#include <Rts.h>
extern void __stginit_MyModule(void);
static char *args[] =3D {"MyDll", NULL};
BOOL STDCALL DllMain(HANDLE hModule, DWORD reason, void *reserved){
    int i;
    if(reason =3D=3D DLL_PROCESS_ATTACH){
        printf("\nStarting Haskell");
        startupHaskell(1, args, __stginit_MyModule);
        printf("Haskell Started!!");
        return TRUE;
    }
return TRUE;
}

Which I compile with:  ghc -c MyDll.c
I build the DLL with:  ghc --mk-dll -o MyDll.dll MyModule.o =
MyModule_stub.o MyDll.o
The DLL is build, and following the example, I try to call it from VB, =
but the program hangs. I don't know if I'm missing something in the =
building process, I can't see what it can be.
Moreover, for calling it from my C++ programs, I need a library file =
(generally called .lib in MSVC linker, I think it's equivalent to the =
libMyLib.a of gcc) to resolve the function (fac) when linking. I tried =
to use=20
dlltool -b MyDll.dll -l MyDll.lib
to contruct the .lib file, it's contructed, but yet, when I use to link =
my C++ program with the DLL, i got unresolved external symbol for the =
function.
Thus, I can't even link the C++ program.
Note that I used the stdcall (for fac)to follow the example; I tried =
even with ccall, but without success either.
Can someone see if I'm missing something at the creation process of the =
dll? And to create the library file, is it the right thing to do?
Mauricio




------=_NextPart_000_0021_01C29943.B50298A0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2716.2200" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>Hey!</FONT></DIV>
<DIV>I' having problems to&nbsp;call functions from&nbsp;a DLL made with =
ghc=20
with a C++ program, using Microsofts Visual C++</DIV>
<DIV><FONT face=3DArial size=3D2>To learn how to do it, I tried the =
example found at=20
the user's guide (11.3, 11.4), but&nbsp;things aren't working out..=20
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Here is as far as I've =
done:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I have a haskell module, =
MyModule.hs:module=20
MyModule where<BR><FONT size=3D1>foreign export stdcall fac :: Int -&gt; =

Int<BR>fac :: Int -&gt; Int<BR>fac n<BR>&nbsp;| n &lt;=3D =
0&nbsp;&nbsp;=3D=20
1<BR>&nbsp;| otherwise&nbsp;&nbsp;=3D n * fac(n-1)</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I compile it with:&nbsp; <FONT =
size=3D2>ghc -c=20
MyModule.hs -fffi</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>The&nbsp;.c file, containing the DLL =
initialization=20
is:</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>#include &lt;windows.h&gt;</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>#include &lt;Rts.h&gt;</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>extern void =
__stginit_MyModule(void);</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>static char *args[] =3D {"MyDll", =
NULL};</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>BOOL STDCALL DllMain(HANDLE hModule, =
DWORD reason,=20
void *reserved){</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>&nbsp;&nbsp;&nbsp; int i;</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>&nbsp;&nbsp;&nbsp; if(reason =3D=3D=20
DLL_PROCESS_ATTACH){</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
printf("\nStarting Haskell");</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
startupHaskell(1, args, __stginit_MyModule);</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
printf("Haskell Started!!");</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
return=20
TRUE;</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV><FONT face=3DArial size=3D1>return TRUE;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT size=3D2><FONT=20
size=3D1>}</FONT></FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D1></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><FONT size=3D2>Which I compile =
with:&nbsp; <FONT=20
size=3D2>ghc -c MyDll.c</FONT></DIV></FONT></FONT>
<DIV><FONT face=3DArial size=3D2>I build the DLL with: &nbsp;<FONT =
size=3D2>ghc=20
--mk-dll -o MyDll.dll MyModule.o MyModule_stub.o =
MyDll.o</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>The DLL is build, and following the =
example, I try=20
to call it from VB, but the program hangs. I don't know if I'm missing =
something=20
in the building process, I can't see what it can be.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Moreover, for calling it from my C++ =
programs, I=20
need a library file (generally called .lib in MSVC linker, I think it's=20
equivalent to the lib<EM>MyLib</EM>.a of gcc) to resolve the function =
(fac) when=20
linking. I tried to use </FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT size=3D1>dlltool -b MyDll.dll -l=20
MyDll.lib</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>to contruct the .lib file, it's =
contructed, but=20
yet, when I use to link my C++ program with the DLL, i got unresolved =
external=20
symbol for the function.</FONT></DIV>
<DIV>Thus, I can't even link the C++ program.</DIV>
<DIV>Note that I used the stdcall (for fac)to follow the example; I =
tried even=20
with ccall, but without success either.</DIV>
<DIV><FONT face=3DArial size=3D2>Can someone&nbsp;see if I'm missing =
something at=20
the creation process of the dll? And to create the library file, is it =
the right=20
thing to do?</FONT></DIV>
<DIV>
<DIV><FONT face=3DArial size=3D2></FONT></DIV><FONT face=3DArial=20
size=3D2>Mauricio<BR></DIV></FONT>
<DIV><FONT face=3DArial><FONT =
size=3D2></FONT></FONT>&nbsp;</DIV></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_0021_01C29943.B50298A0--