mark.warren
2019-2-15 10:45:07

I’m having my first attempt at ffi and have the following for the Windows GetSystemTime function. (define-cstruct _SYSTEMTIME ([wYear _int16] [wMonth _int16] [wDayOfWeek _int16] [wDay _int16] [wHour _int16] [wMinute _int16] [wSecond _int16] [wMilliseconds _int16])) (define-ffi-definer define-kernel32 (ffi-lib "Kernel32")) (define sys-time (make-SYSTEMTIME 0 0 0 0 0 0 0 0)) (define-kernel32 GetSystemTime (_fun _SYSTEMTIME-pointer -> _void)) My question is do I need to free the sys-time struct at the end?


andreiformiga
2019-2-15 13:31:16

@mark.warren I don’t think so, if you’re creating the struct using make-SYSTEMTIME then I believe it will be managed by the racket runtime


andreiformiga
2019-2-15 13:31:36

you have to use free explicitly when you use malloc


andreiformiga
2019-2-15 13:32:29

you could use _ptr to have the FFI allocate and return the struct itself, so the user wouldn’t have to create the struct before calling the function


mark.warren
2019-2-15 13:32:29

@andreiformiga That is what I had sort of understood from the documentation, but I wasn’t 100% sure.


mark.warren
2019-2-15 13:33:51

Ah ok, I hadn’t quite got the idea of _ptr. I was just quite happy to have got it working.


andreiformiga
2019-2-15 13:34:03

I’m wrapping functions from SDL this way: ;extern DECLSPEC int SDLCALL SDL_GetCurrentDisplayMode(int displayIndex, SDL_DisplayMode * mode); (define-sdl SDL_GetCurrentDisplayMode (_fun _int [dm : (_ptr o _sdl-display-mode)] -> (err : _int) -> (if (zero? err) dm #f)))


andreiformiga
2019-2-15 13:34:52

so the user doesn’t have to create a SDL_DisplayMode and then call the function, you can just call the function and it will return the struct (or #f in case of failure)


mark.warren
2019-2-15 13:35:13

Cool. Thanks.


daniel
2019-2-15 13:41:17

@daniel has joined the channel


mark.warren
2019-2-15 13:46:54

@andreiformiga Sorry, where does _sdl-display-mode come from. When I try this with my GetSystemTime I get an unbound identifier error.


andreiformiga
2019-2-15 13:47:47
(define-cstruct _sdl-display-mode
  ((format _uint32)
   (w _int)
   (h _int)
   (refresh-rate _int)
   (driver-data _pointer)))

andreiformiga
2019-2-15 13:48:27

you use _ptr o and then the type of the struct that is parameter to the function


mark.warren
2019-2-15 13:49:05

Ah, cool, I was just being daft. I thought you meant you could get rid of the struct definition. My brain is winding down for the weekend.


andreiformiga
2019-2-15 13:50:03

:slightly_smiling_face:


andreiformiga
2019-2-15 13:50:18

this way you don’t need to create the struct manually before calling the function


mark.warren
2019-2-15 13:51:00

As in the make-SYSTEMTIME


andreiformiga
2019-2-15 13:51:04

the FFI will allocate a struct and pass a pointer to the function


andreiformiga
2019-2-15 13:51:05

yes


andreiformiga
2019-2-15 13:51:24

you don’t need to call it, just GetSystemTime


mark.warren
2019-2-15 13:51:37

Sweet


andreiformiga
2019-2-15 13:51:38

unless you want to have a single struct that gets changed in multiple calls, of course


mark.warren
2019-2-15 13:51:51

Thanks


mark.warren
2019-2-15 14:06:16

@andreiformiga I think I’ve got it now (define-kernel32 GetSystemTime (_fun [st : (_ptr o _SYSTEMTIME)] -> _void -> st))


xarxziux
2019-2-15 15:33:38

NVM, found a solution!