Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to access an external DLL function from F#. This one has me really sweating.

The C header is:

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

I imported that in F# accordingly:

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

The problem is the array part. I tried PinnedArray from F# Powerpack, but the call still fails. The char array is probably ok, even though I can't check since the call fails.

So far it 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" not matching 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 this point (the console window disappears and in debugging, it just jumps back to the first line.

I'm aware that I could use "use" instead of "let" with "let argx = PinnedArray.of_array ar", but the compiler won't let me have it because of a module declaration at the top.

There is an implementation in C# like this:

    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;
    }

Maybe the whole problem goes back to the FileLoad exception (even though that is not displayed in the dll call) - possibly because of the FSharp Powerpack?

Thank you very much for your help indeed.

like image 418
markwest Avatar asked Oct 07 '22 12:10

markwest


1 Answers

Here's my guess, based on the C type signature and the function documentation I found. It compiles, but you'll have to tell me if it actually works since I don't have the swedll32.dll assembly. Also, my code doesn't require the F# Powerpack assembly.

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

EDIT 2: Removed the ExactSpelling = true from the MarshalAs attribute, as per @wolfgang's 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
like image 95
Jack P. Avatar answered Oct 10 '22 03:10

Jack P.