Call a DLL from Haskell

MathematicalOrchid

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?)

MathematicalOrchid

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...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Call Delphi dll from go

From Dev

python call function from DLL

From Dev

Call C dll from Fortran

From Dev

Call DLL written in Delphi from Java

From Dev

Not able to call the function from C DLL

From Java

Call C# dll from a Java Application

From Dev

Call fortran dll from java using JNI

From Dev

Call .dll function from C# Ironpython

From Dev

call c dll from delphi with strings

From Dev

Troubleshooting DLL function call from Excel Vba

From Dev

How call dll from unpaked extension directory

From Dev

Call a member function with offset address from Dll

From Dev

Can not call a function from a C# DLL

From Dev

Call a method in program from dll in C#

From Dev

Reference parent method call from drived DLL

From Dev

Call C dll function from C#

From Dev

How to call function in my dll from jquery

From Dev

How to call Haskell from Javascript with GHCJS

From Dev

How to call constructor from Template Haskell

From Dev

Creating a .NET List<List<double>> to call a .NET DLL from MATLAB?

From Dev

How to call a C DLL from a VBA application and return an array?

From Dev

Unable to make http call from c# DLL

From Dev

Call C# dll function from TypeScript | Overwolf

From Dev

Call a function of VB DLL file from java using JNA

From Dev

P/Invoke - call unmanaged DLL from C#

From Dev

Call an unmanaged dll always from same thread in C#

From Dev

Call C++ function from inside dynamically loaded dll

From Dev

pythonnet cannot call method from vb .net dll - TypeError

From Dev

How to call .dll coded in vb6 from AutoHotkey Script?