Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping a DLL import in a module in F#

Tags:

f#

dllimport

I'm attempting to create a "wrapper" module for some windows api functions from user32.dll. I'm still learning F# so I'm rather fuzzy on how inheritance and polymorphism works in F# and how to apply that to this situation.

I have this module:

module MouseControl =         
    [<DllImport( "user32.dll", CallingConvention = CallingConvention.Cdecl )>]
    extern void ShowCursor(bool show)

    [<DllImport( "user32.dll", CallingConvention = CallingConvention.Cdecl )>]
    extern void internal mouse_event(int flags, int dX, int dY, int buttons, int extraInfo)
    let MouseEvent(flags, dX, dY, buttons, extraInfo) = mouse_event(flags, dX, dY, buttons, extraInfo)

My goal is to be able to "hide" the mouse_event function from other code that uses this module and instead expose that function as MouseEvent. With this code both mouse_event and MouseEvent are currently available to code that calls this module. How do I hide mouse_event where it's private to the module?

like image 654
Ken Avatar asked Aug 07 '14 20:08

Ken


1 Answers

In your code sample, you already marked the mouse_event function as internal - so in principle, you should just need to mark it as private. However, it looks that F# compiler is ignoring visibility annotations on extern members, so the easiest option is to put them in a nested module and hide the whole nested module:

module MouseControl =         
  module private Imported = 
    [<DllImport( "user32.dll", CallingConvention = CallingConvention.Cdecl )>]
    extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo)

  let MouseEvent(flags, dX, dY, buttons, extraInfo) = 
    Imported.mouse_event(flags, dX, dY, buttons, extraInfo)

The module Imported is now visible only inside the MouseControl module. From the outside, you cannot access anything inside MouseControl.Imported.

like image 151
Tomas Petricek Avatar answered Nov 18 '22 03:11

Tomas Petricek