Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8590685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:21:04+00:00 2026-06-11T23:21:04+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:21:05+00:00Added an answer on June 11, 2026 at 11:21 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.