Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

P/Invoke Function Overloading

Tags:

pinvoke

f#

I'm attempting to P/Invoke a C library from F#, and have encountered a peculiar issue. I have a module containing all my extern functions. The underlying C library has two functions with the same name, but different arguments. This, of course, is not allowed in an F# module.

module C =

  open System.Runtime.InteropServices

  [<DllImport("libc", CallingConvention = CallingConvention.Cdecl)>]
  extern int setValue(nativeint source, int value)

  [<DllImport("libc", CallingConvention = CallingConvention.Cdecl)>]
  extern int setValue(nativeint source, string value)

  // the previous function declaration cause the following compile-time error:
  // Duplicate definition of value 'setValue'

Is there some special way to work around this? I can not alter the C library.

like image 873
pblasucci Avatar asked Sep 11 '26 12:09

pblasucci


1 Answers

The EntryPoint attribute should work (e.g. with an ordinal), if MSDN can be trusted (haven't tested in F#). Name your imported functions e.g. setValueInt() and setValueString().

like image 187
Pontus Gagge Avatar answered Sep 20 '26 00:09

Pontus Gagge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!