passing a Handle to a C function

I have a C function that, for simplicity, has its definition as something like:
void myCFun(FILE *fd);
I have a Handle I've opened in Haskell using openFileEx and would like to pass this to the function. I've tried a bunch of things, the most recent being:
foreign import ccall "myHFile.h myCFun" c__myCFun :: Ptr CInt -> IO () myCFun :: Handle -> IO () myCFun (FileHandle handleMV) = do h__ <- readMVar handleMV ptr <- malloc poke ptr (toCInt $ haFD h__) c__initVars ptr
i've also tried it with just CInt -> IO (), without the ptr, but that doesn't work either. Surely someone has done this at some point...or is it even possible (please say it is)... - Hal -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume

Hal Daume wrote:
I have a C function that, for simplicity, has its definition as something like:
void myCFun(FILE *fd);
I have a Handle I've opened in Haskell using openFileEx and would like to pass this to the function.
GHC's Handle type is based upon a descriptor rather than a FILE*, so you will have to manufacture the FILE*, e.g. with fdopen().
I've tried a bunch of things, the most recent being:
foreign import ccall "myHFile.h myCFun" c__myCFun :: Ptr CInt -> IO () myCFun :: Handle -> IO () myCFun (FileHandle handleMV) = do h__ <- readMVar handleMV ptr <- malloc poke ptr (toCInt $ haFD h__) c__initVars ptr
i've also tried it with just CInt -> IO (), without the ptr, but that doesn't work either.
Surely someone has done this at some point...or is it even possible (please say it is)...
For Unix, use Posix.handleToFd to get a descriptor, then fdopen() (in
C, or write a foreign import) to get a FILE*.
Also, don't forget about synchronisation issues between the C and
Haskell interfaces to the descriptor (e.g. buffering).
--
Glynn Clements

In local.glasgow-haskell-users, you wrote:
For Unix, use Posix.handleToFd to get a descriptor, then fdopen() (in C, or write a foreign import) to get a FILE*. Also, don't forget about synchronisation issues between the C and Haskell interfaces to the descriptor (e.g. buffering).
After the 'handleToFd', there shouldn't be any issues of interference because the handle becomes unusable in Haskell-land. Volker -- http://www-i2.informatik.rwth-aachen.de/stolz/ *** PGP *** S/MIME rage against the finite state machine

By the way, this questions is some bit close on that with Subject: "Posix library, Concurrent Haskell and ..." I have just send ... Let's read both... By Using Posix library I did not need any more to pass file descriptors into C code (may be your case is different) On Thu, 10 Jul 2003, Volker Stolz wrote:
In local.glasgow-haskell-users, you wrote:
For Unix, use Posix.handleToFd to get a descriptor, then fdopen() (in C, or write a foreign import) to get a FILE*. Also, don't forget about synchronisation issues between the C and Haskell interfaces to the descriptor (e.g. buffering).
this is true.... o "flush" for writing At GHC-5.02 Posix does not implement it.. o and nonBlocking for reading. (see given message on this)
After the 'handleToFd', there shouldn't be any issues of interference because the handle becomes unusable in Haskell-land.
Volker -- http://www-i2.informatik.rwth-aachen.de/stolz/ *** PGP *** S/MIME rage against the finite state machine _______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
participants (4)
-
Glynn Clements
-
Hal Daume
-
Rafael Martinez Torres
-
Volker Stolz