passing Strings with FFI
Julian Seward (Intl Vendor)
Wed, 20 Jun 2001 04:38:35 -0700
| I want to pass a String to a function which is imported from C code:
| foreign import "pass" prim_pass :: String -> String
| The declaration above gives me an error from ghc like "String=20
| type not=20
| supported for imported functions".
| I thought that String being [Char] should be supported=20
| (somehow like a=20
| definition with "newtype" keyword).
| Can you tell me how can I pass strings to and from C code, as=20
| simple as=20
| possible (I know about HDirect, but I couldn't make it work=20
| for me: red-hat=20
| 7.1, ghc 5.00)?
First of all, upgrade to 5.00.2, since it is significantly less
buggy than 5.00.
int fooble ( char* str, int n )
fprintf(stderr, "fooble called: %s %d\n", str, n );
import PrelPack (packString)
foreign import "fooble" fooble
fooble :: PackedString -> Int -> IO Int
type PackedString =3D ByteArray Int
main =3D do n <- fooble (packString "hello, C world") 99
putStrLn ("Returned value is " ++ show n)
For more examples read the compiler sources, at=20