Skip to content

Converting ConsoleHandles with GHC.IO.Handle.Windows.handleToHANDLE

Motivation

On linux it is possible to do things like:

handleToFd stdin
handleToFd <$> hDuplicate stdin

But on windows the equivalent doesn't work:

handleToHANDLE stdin
handleToHANDLE <$> hDuplicate stdin

Proposal

The handleToHANDLE function should be able to convert IoHandle ConsoleHandle values to HANDLE.

I think it should be possible to change it like this:

-- | Turn an existing Handle into a Win32 HANDLE. This function throws an
-- IOError if the Handle does not reference a HANDLE
handleToHANDLE :: Handle -> IO Win.HANDLE
handleToHANDLE h = case h of
  FileHandle _ mv -> do
    Handle__{haDevice = dev} <- readMVar mv
    case (cast dev :: Maybe (Win.Io Win.NativeHandle)) of
      Just hwnd -> return $ Win.toHANDLE hwnd
      Nothing   ->
        case (cast dev :: Maybe (Win.Io Win.ConsoleHandle)) of
          Just console -> return $ Win.toHANDLE console
          Nothing   -> throwErr "not a file HANDLE"
  DuplexHandle{} -> throwErr "not a file handle"
  where
    throwErr msg = ioException $ IOError (Just h)
      InappropriateType "handleToHANDLE" msg Nothing Nothing
Edited by Jaro Reinders
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information