Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HANDLE to Handle

Tags:

io

haskell

winapi

I want to create a annonymous pipe on windows using the CreatePipe of the WinAPI via the FFI. This will give me a HANDLE (type from the Win32 haskell package), but I'd like to get an ordinary haskell Handle such that I can use the standard haskell IO functions on it. So I need a function of type:

win32handleToStandardHandle :: HANDLE -> IO Handle

How can I implement this?

On linux, I can use System.Posix.IO's fdToHandle function to convert between the FD type used by the linux system calls and the standard haskell type. But there seems to be no such function for windows.

like image 982
bennofs Avatar asked Jul 02 '14 19:07

bennofs


1 Answers

Even on Windows the standard implementation of files in System.IO uses a file descriptor, not a win32 HANDLE. These file descriptors are provided by the C runtime, not Windows. To convert a HANDLE to a file descriptor the runtime provides the _open_osfhandle function. GHC uses MinGW on Windows but MinGW doesn't provide its own C runtime, so Haskell EXEs use msvcrt.dll.

Use the FFI to import _open_osfhandle. Use this to convert your HANDLE to a file descriptor and then call fdToHandle, which seems to live in GHC.IO.Handle.FD.

I haven't actually tried this.

like image 135
arx Avatar answered Oct 02 '22 07:10

arx