C # to F #: Pinned Array and Stringbuilder in an external function called from F #

I am trying to access an external DLL function from F #. I really have sweating.

Header C:

ext_def(int32) swe_calc_ut(double tjd_ut, int32 ipl, int32 iflag, double *xx, char *serr); 

I imported this into F # respectively:

 extern int32 ext_swe_calc_ut(double tjd_ut, int32 ipl, int32 iflag, double *xx, StringBuilder serr); 

The problem is part of the array. I tried PinnedArray from F # Powerpack, but the call still failed. The char array is probably good, although I cannot verify since the call failed.

So far this is:

 open System open System.Runtime.InteropServices open System.Text open Microsoft.FSharp.NativeInterop #r "FSharp.PowerPack.dll" #nowarn "51" module Sweph = [<DllImport(@"swedll32.dll", CharSet = CharSet.Ansi, EntryPoint = "swe_calc_ut")>] extern int32 ext_swe_calc_ut(double tjd_ut, int32 ipl, int32 iflag, double *xx, StringBuilder serr); /// <param name="jdnr">Julian day</param> /// <returns>Array with 6 doubles: 0:longitude, 1:latitude, 2:distance,3:speel in longitude, /// 4: speed in latitude, 5: speed in distance </returns> let ar: double array = Array.zeroCreate 6 let argx = PinnedArray.of_array ar printfn " ar: %A" ar // This fails with a "FileLoadException" printfn "ar: %A" argx // Details of FileLoadException: (* 

"FSharp.Core, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a" does not match the assembly (Ausnahme von HRESULT: 0x80131040) *)

 // However, if I leave the printfn for argx out, code continues and does not display this error again. let sb = new StringBuilder(50) // Vgl. Expert F#, S. 515 Console.ReadKey(true) // Application crashes here: let ret_calc = Sweph.ext_swe_calc_ut(4700.0,1,1,argx.Ptr,sb) 

The program crashes at the moment (the console window disappears and when debugging it simply returns to the first line.

I know that I could use "use" instead of "let" with "let argx = PinnedArray.of_array ar", but the compiler will not let me get it because of the module declaration at the top.

In C #, the following implementation is implemented:

  public static double[] getPlanet(int ipl, double jdnr) { // String ephePath = "Q:\\sweph\\"; // Sweph.setEphePath(ephePath); double[] xx2 = new double[8]; double[] xx = new double[6]; String serr = ""; int iflag = Constants.SEFLG_SPEED; long iflgret = ext_swe_calc_ut(jdnr, ipl, iflag, xx, serr); for (int i = 0; i < 6; i++) { xx2[i] = xx[i]; } iflag = Constants.SEFLG_SWIEPH | Constants.SEFLG_SPEED | Constants.SEFLG_EQUATORIAL; iflgret = ext_swe_calc_ut(jdnr, ipl, iflag, xx, serr); xx2[6] = xx[0]; xx2[7] = xx[1]; return xx2; } 

Perhaps the whole problem goes back to the FileLoad exception (although this does not appear in the dll call) - perhaps due to FSharp Powerpack?

Many thanks for your help.

+4
source share
2 answers

Here's my hunch based on a type C signature and function documentation I found. It compiles, but you will have to tell me if it really works, since I do not have the swedll32.dll assembly. In addition, my code does not require building F # Powerpack.

EDIT: just read the comments in @kvb's answer - he suggested the same thing that I included in my code, which is an attribute [<MarshalAs(...)>] with a set of SizeConst properties. I also added [<Out>] because the last two parameters are used as return values; IIRC, this also affects the behavior of the marshalling.

EDIT 2: Removed ExactSpelling = true from the MarshalAs attribute, according to the @wolfgang report.

 [<DllImport(@"swedll32.dll", CharSet = CharSet.Ansi, EntryPoint = "swe_calc_ut")>] extern int32 swe_calc_ut ( float tjd_ut, int32 ipl, int32 flag, [<Out; MarshalAs(UnmanagedType.LPArray, SizeConst = 6)>] float[] xx, [<Out; MarshalAs(UnmanagedType.LPStr, SizeConst = 256)>] System.Text.StringBuilder serr) // Wrapper function for swe_calc_ut let swe_calc_ut_wrapper (tjd_ut, ipl, flag) = let positions = Array.zeroCreate 6 let errorSB = System.Text.StringBuilder (256) let result = swe_calc_ut (tjd_ut, ipl, flag, positions, errorSB) if result < 0 then // Error Choice2Of2 (result, errorSB.ToString ()) else Choice1Of2 positions 
+3
source

You can try to use the functionality from the System.Runtime.Interop , rather than relying on F # PowerPack. I think you need to try something like:

 let hndl = GCHandle.Alloc(ar, GCHandleType.Pinned) let ptr : double nativeptr = hndl.AddrOfPinnedObject() |> NativePtr.ofNativeInt let ret_calc = Sweph.ext_swe_calc_ut(4700.0,1,1,ptr,sb) hndl.Free() 

However, it seems that the C # code uses a different interop signature - it marchs xx as double[] and serr as string (and it looks like it probably returns int64 ). If this code works, you can just do the same in F # ...

0
source

Source: https://habr.com/ru/post/1436609/


All Articles