How to access hs_free_fun_ptr from a Visual Studio DLL

Brian Hulley brianh at
Sat Mar 25 14:00:35 EST 2006

Hi -
I've found a workaround to the problem below: instead of trying to use 
hs_free_fun_ptr, I instead pass a FunPtr to the Haskell function 
freeHaskellFunPtr into my DLL, and use this to free everything, finally 
using it to free itself (!) which I assume should be safe.

Thus there is no need to try to link against hs_free_fun_ptr (or 
hs_free_stable_ptr which can be obtained by wrapping 

Regards, Brian.

PS: as an aside, it is interesting to note the strange naming for 
Foreign.Ptr.freeHaskellFunPtr (why the "Haskell"?) as opposed to 
freeStablePtr... :-)

Brian Hulley wrote:
> Hi -
> I have the following declaration and code in part of a DLL I'm
> writing:
>   extern "C" {
>        typedef void (*HsFunPtr)();
>       extern __declspec(dllimport) void hs_free_fun_ptr(HsFunPtr fp);
>   }
>   enum ECallback { ECallback_Render, ECallback_COUNT};
>   HsFunPtr callback[ECallback_COUNT];
>   void SetCallback(ECallback c, HsFunPtr fp){
>       if (callback[c]){
>          hs_free_fun_ptr(callback[c]);
>      }
>      callback[c] = fp;
>   }
> However when I try to build the DLL, I get the following error from
> Visual Studio:
>   Duma.obj : error LNK2019: unresolved external symbol
> __imp__hs_free_fun_ptr
> So the problem is that the VS is adding __imp__ onto the function
> name even though I've specified it as extern "C" and is moreover just
> ignoring the dllimport directive and complaining that it can't find
> the function.
> I've also tried deleting the "extern" before the "__declspec" but this
> doesn't make any difference. Also, I've tried putting hs_free_fun_ptr
> into the IMPORTS section of the def file but this doesn't make any
> difference either (I don't even think def files have an IMPORTS
> section so it's probably just being ignored).
> I've got a horrible feeling that VS will need a .lib file for ghc to
> allow it to link against hs_free_fun_ptr but I don't have a clue how
> to generate one...
> Does anyone know how I can get everything to link?
> Thanks, Brian.
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at

More information about the Glasgow-haskell-users mailing list