我正在尝试从 F# 访问外部 DLL 函数。这个真的让我汗流浃背。
C头文件是:
ext_def(int32) swe_calc_ut(double tjd_ut, int32 ipl, int32 iflag,
double *xx, char *serr);
我相应地在 F# 中导入了它:
extern int32 ext_swe_calc_ut(double tjd_ut, int32 ipl, int32 iflag, double *xx, StringBuilder serr);
问题是数组部分。我尝试了 F# Powerpack 中的 PinnedArray,但调用仍然失败。char 数组可能没问题,即使由于调用失败我无法检查。
到目前为止是:
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,版本=2.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”不匹配程序集(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)
程序此时崩溃(控制台窗口消失,在调试中,它只是跳回第一行。
我知道我可以使用“use”而不是“let”和“let argx = PinnedArray.of_array ar”,但编译器不会让我拥有它,因为顶部有一个模块声明。
在 C# 中有这样的实现:
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;
}
也许整个问题可以追溯到 FileLoad 异常(即使它没有显示在 dll 调用中)——可能是因为 FSharp Powerpack?
非常感谢您的帮助。