Exporting a String

Sven Eric Panitz sep@softwareag.com
Fri, 28 Sep 2001 08:20:08 +0200

I am no expert in these things, but
as a matter of fact I just did some quite similar and found
the help in the mailing list archive. (at least I did not find
a cookbook for this in the documentation)

My Haskell module:

module Mini where
import CString

foreign export stdcall cAppend :: CString -> CString -> IO CString

cAppend :: CString -> CString -> IO CString
cAppend cxs cys
 = do
    xs <- peekCString cxs
    ys <- peekCString cys
    erg <- return (xs++ys)
    newCString erg

And my C program Main.c calling this function:

#include <stdio.h>
#include "Mini_stub.h"
#include "RtsAPI.h" 

extern void __stginit_Mini ( void ); 

int main(void)
   char* bogusFlags[1] = { "\0" }; 
   char* test;   

   //starting up Haskell runtime
   startupHaskell(0, bogusFlags, __stginit_Mini);

   //calling Haskell function
   test=cAppend("the world"," is my oyster"); 

   //printing its result
   printf("%s\n", test);

   //say bye bye to Haskell

   return 0;

and my way to build this thing:
ghc -fglasgow-exts -package text -c Mini.hs
gcc -I. -I'e:/Program Files/ghc/ghc-5.02/include' -I'E:\Program Files\ghc\ghc-5.02\include\mingw' -c Main.c
ghc -fglasgow-exts -no-hs-main -package text -static Main.o Mini.o Mini_stub.o -o Test.exe

Please some expert correct me, if this is not the right way to
do this.

Sven Eric

Sven Eric Panitz         Uhlandstr. 12       D-64297 Darmstadt
Software AG           sep@softwareag.com     (+49)6151-92-1426 
---  when I cannot sing my heart, I can only speak my mind ---