Wrapping DLL imports in a module in F #
I am trying to create a module "wrapper" for some windows api functions from user32.dll
. I'm still learning F #, so I am rather unclear about how inheritance and polymorphism work in F # and how to apply it 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" a function mouse_event
from other code that uses this module, and instead show that function as MouseEvent
. With this code it is now available mouse_event
and MouseEvent
for the code that calls this module. How to hide mouse_event
where it is private for a module?
source to share
In your example code, you've already marked the function mouse_event
as internal
- so basically you just need to mark it as private
. However, the F # compiler seems to ignore visibility annotations on elements extern
, so the simplest option is to place them in a nested module and hide the entire 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 is Imported
now displayed only inside the module MouseControl
. From the outside, you cannot access anything inside MouseControl.Imported
.
source to share