MathematicalOrchid
MathematicalOrchid

Reputation: 62858

Call a DLL from Haskell

I have a Windows DLL named Foo.dll. It exports (amoung other things) the following:

extern "C" __declspec(dllexport) unsigned int Version();

How do I write a Haskell program which calls this function and prints out the answer?

I managed to figure out that I can write this:

foreign import ccall "Version" cpp_Version :: CUInt

This compiles just fine, but utterly fails to link. This is not surprising; at this point GHC has no idea where the hell to look for this function. But I can't figure out what magic button I need to push to make this happen. Can anybody tell me how to get this to build sucessfully?

(I'm also not 100% sure whether the calling convention should be ccall or stdcall; what's the difference?)

Upvotes: 3

Views: 845

Answers (1)

MathematicalOrchid
MathematicalOrchid

Reputation: 62858

Compiling with the following options appears to work:

ghc -O2 -L. -lFoo --make Wrapper

It appears that adding -lFoo tells GHC to look for a Foo.dll file, and adding -L. tells it to include the current directory in the DLL search path.

I am not 100% sure whether this is loading the DLL at runtime, or actually statically linking the DLL's code into the binary somehow. (!!)

Changing ccall to stdcall causes a bunch of warnings to be emitted (but the compiled code still works correctly). Thus, it appears that ccall is the correct thing.

I would still appreciate it if somebody could double-check that what I've written isn't complete nonsense...

Upvotes: 1

Related Questions