Real World Haskell: From Portable to System.Win32

Hello Haskellers. I'm having a bit of trouble solving the challenge in chapter 9 of the book Real World Haskell [1]. I understand the portable code and have no difficulty with it. More specifically, I want to know how to get a file's last modified date. Perhaps in understanding this bit I may also extrapolate to similar parts of System.Win32 more easily. A simple example using portable code, and also formats the date: getFileModifiedDate :: FilePath -> IO String getFileModifiedDate f = do m <- getModificationTime f c <- toCalendarTime m return $ formatCalendarTime undefined "%Y-%m-%d %T" c However, the exercise is to "port the code from this chapter to your platform's native API", which in my case is System.Win32. Using the simple example, I know that I can use getFileTime from System.Win32 [2]: getFileTime :: HANDLE -> IO (FILETIME, FILETIME, FILETIME) I suppose that means getFileTime takes a file handle (with type HANDLE) and returns an IO type with CreationTime LastAccessTime LastWriteTime [3]. The thing is I don't know how to get a HANDLE type. I understand I can get a file handle by opening it. I've been trying the following: openFile :: FilePath -> IOMode -> IO Handle fh <- openFile "myfile.hs" ReadMode Right now I cannot use getFileTime on it because this has type Handle: Couldn't match expected type `HANDLE' with actual type `Handle' In the first argument of `getFileTime', namely `fh' In a stmt of an interactive GHCi command: nf <- getFileTime fh I think I got lost at inspecting the type HANDLE = Ptr () If anyone could make a useful example I would appreciate it very much. Perhaps it's just me going around in circles. Regards, P. M. Pocinho -- [1] http://book.realworldhaskell.org/read/io-case-study-a-library-for-searching-... [2] http://hackage.haskell.org/packages/archive/Win32/2.2.1.0/doc/html/System-Wi... [3] http://msdn.microsoft.com/en-us/library/windows/desktop/ms724320%28v=VS.85%2...

On Sat, Oct 8, 2011 at 7:47 AM, Paulo Pocinho
getFileTime :: HANDLE -> IO (FILETIME, FILETIME, FILETIME)
I suppose that means getFileTime takes a file handle (with type HANDLE) and returns an IO type with CreationTime LastAccessTime LastWriteTime [3].
The thing is I don't know how to get a HANDLE type. I understand I can get a file handle by opening it. I've been trying the following:
openFile :: FilePath -> IOMode -> IO Handle fh <- openFile "myfile.hs" ReadMode
Yes, the case is significant and that means that Handle and HANDLE aren't the same, apparently HANDLE is specific to the win32 api (which I don't know well), a rapid inspection of System.Win32.File seems to reveal that the only function to get an HANDLE is createFile which has a ton of parameters you'll need to set carefully to avoid overwriting your existing file (you should probably define another function with most of these already fixed for convenience) but should do the trick. Good luck. -- Jedaï

On Sat, 08 Oct 2011 18:45:43 +0200, Chaddaï Fouché
On Sat, Oct 8, 2011 at 7:47 AM, Paulo Pocinho
wrote: getFileTime :: HANDLE -> IO (FILETIME, FILETIME, FILETIME)
I suppose that means getFileTime takes a file handle (with type HANDLE) and returns an IO type with CreationTime LastAccessTime LastWriteTime [3].
The thing is I don't know how to get a HANDLE type. I understand I can get a file handle by opening it. I've been trying the following:
openFile :: FilePath -> IOMode -> IO Handle fh <- openFile "myfile.hs" ReadMode
Yes, the case is significant and that means that Handle and HANDLE aren't the same, apparently HANDLE is specific to the win32 api (which I don't know well), a rapid inspection of System.Win32.File seems to reveal that the only function to get an HANDLE is createFile which has a ton of parameters you'll need to set carefully to avoid overwriting your existing file (you should probably define another function with most of these already fixed for convenience) but should do the trick.
You can use the information about createFile at: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).as... Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --
participants (3)
-
Chaddaï Fouché
-
Henk-Jan van Tuyl
-
Paulo Pocinho