GHC compile DLL for use in VBA

一笑奈何 提交于 2019-12-11 15:45:26

问题


This is an alternative approach to this question here: Export Haskell lib as DLL

I am working with GHC version 8.6.1 [latest] and am following the documentation for compiling a DLL from a haskell library for the subsequent use in VBA.

My files look like this:

Adder.hs:

{-# LANGUAGE ForeignFunctionInterface #-}
module Adder where

adder :: Int -> Int -> IO Int
adder x y = return (x+y)

foreign export ccall adder :: Int -> Int -> IO Int

StartEnd.c:

#include <Rts.h>

void HsStart() {
    int argc = 1;
    char* argv[] = {"ghcDll", NULL};
    char** args = argv;
    hs_init(&argc, &args);
}

void HsEnd() {
    hs_exit();
}

I copied the folder ghc-8.6.1\lib\include to the build location and copied the two files into that folder (because I cannot figure out how to pass the -I[PATH] parameter correctly).

Running these compilation steps, I get:

ghc -c Adder.hs
    --> no error
ghc -c StartEnd.c
    --> no error
ghc -shared -no-hs-main -o Adder.dll Adder.o Adder_stub.h StartEnd.o
    --> no error

and the files

Adder.dll
Adder.dll.a
Adder.hi
Adder.o
Adder_stub.h
startEnd.o

I tried using the third compilation command like it is in the documentation ghc -shared -o Adder.dll Adder.o Adder_stub.h StartEnd.o and with the parameter -no-hs-main just to be sure.

My VBA script looks like this:

Private Declare Function Adder Lib "PATH\TO\Adder.dll" Alias "adder" _
        (ByVal x As Long, ByVal y As Long) As Long

Private Declare Sub HsStart Lib "PATH\TO\Adder.dll" ()
Private Declare Sub HsEnd Lib "PATH\TO\Adder.dll" ()

Public Sub test()
    HsStart
    MsgBox "12 + 5 = " & Adder(12, 5)
    HsEnd
End Sub

However, whenever I try to run the Sub test, I get the error message:

Run-Time error '48'
File not found:
PATH\TO\Adder.dll

Can you please tell me where I made a mistake and how I can fix it?

If it helps, the output of dumpbin /EXPORTS Adder.dll is this:

Microsoft (R) COFF/PE Dumper Version 14.00.24234.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file Adder.dll

File Type: DLL

  Section contains the following exports for Adder.dll

    00000000 characteristics
    5BBD9533 time date stamp Wed Oct 10 07:59:15 2018
        0.00 version
           1 ordinal base
       29737 number of functions
       29737 number of names

    ordinal hint RVA      name

          1    0 004E0F18 ALLOC_BH_adm
          ...
          75   4A 00001681 HsEnd
          76   4B 00001640 HsStart
          ...
          472  1D7 00001540 adder
          ...

My Path:

C:\Users\scfa\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin;C:\Users\scfa\Documents\myProgramms\dev_msys2\mingw64\bin;C:\Users\scfa\Documents\myProgramms\dev_msys2\mingw32\bin;C:\Users\scfa\Documents\myProgramms\ghc-8.6.1\bin;C:\Users\scfa\Documents\myProgramms\ghc-8.6.1\mingw\bin

来源:https://stackoverflow.com/questions/52734002/ghc-compile-dll-for-use-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!