从Haskell调用DLL [英] Call a DLL from Haskell
问题描述
我有一个名为 Foo.dll的Windows DLL / / code>。它导出(其他东西)以下内容:
I have a Windows DLL named Foo.dll
. It exports (amoung other things) the following:
extern "C" __declspec(dllexport) unsigned int Version();
如何编写一个调用此函数并打印出答案的Haskell程序?
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
这个编译很好,但是完全无法链接。这并不奇怪在这一点上,GHC没有想到在哪里找到这个功能。但是我无法确定我需要推动什么魔术按钮来实现这一点。有没有人可以告诉我如何建立成功?
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?
(我也不能100%确定调用约定应该是 ccall
或 stdcall
;有什么区别?)
(I'm also not 100% sure whether the calling convention should be ccall
or stdcall
; what's the difference?)
推荐答案
p>使用以下选项进行编译出现:
ghc -O2 -L. -lFoo --make Wrapper
似乎添加 -lFoo
告诉GHC寻找一个 Foo.dll
文件,并添加 -L。
告诉它包含DLL搜索路径中的当前目录。
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.
我不是100%确定这是否在运行时加载DLL,或者实际上将DLL的代码静态链接到二进制文件。 (!!)
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. (!!)
将 ccall
更改为 stdcall
一些警告将被发出(但编译代码仍然正常工作)。因此,它出现, ccall
是正确的。
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...
这篇关于从Haskell调用DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!