2011-04-26 51 views
2

我想在C#中編寫一個類來使用dll的導出,但我遇到了 一堆函數,它有一個數組作爲函數參數,我不確定 如何處理。下面是一個例子。數組作爲函數參數c到c#混淆

我該如何在C#中的函數中使用它?具體如何處理iymdf []?

函數調用時發生了什麼?

這是否正確?

[DllImport("libsofa.dll")] 
public static extern int iauJdcalf(int ndp, double dj1, double dj2, ref int[] iymdf =  new int[4]); 

例如:

int __stdcall iauJdcalf(int ndp, double dj1, double dj2, int iymdf[4]) 
{ 
    int j, js; 
    double denom, d1, d2, f1, f2, f; 

    /* Denominator of fraction (e.g. 100 for 2 decimal places). */ 
    if ((ndp >= 0) && (ndp <= 9)) { 
     j = 0; 
     denom = pow(10.0, ndp); 
    } else { 
     j = 1; 
     denom = 1.0; 
    } 

    /* Copy the date, big then small, and realign to midnight. */ 
    if (dj1 >= dj2) { 
     d1 = dj1; 
     d2 = dj2; 
    } else { 
     d1 = dj2; 
     d2 = dj1; 
    } 
    d2 -= 0.5; 

    /* Separate days and fractions. */ 
    f1 = fmod(d1, 1.0); 
    f2 = fmod(d2, 1.0); 
    d1 = floor(d1 - f1); 
    d2 = floor(d2 - f2); 

    /* Round the total fraction to the specified number of places. */ 
    f = floor((f1+f2)*denom + 0.5)/denom; 

    /* Re-assemble the rounded date and re-align to noon. */ 
    d2 += f + 0.5; 

    /* Convert to Gregorian calendar. */ 
    js = iauJd2cal(d1, d2, &iymdf[0], &iymdf[1], &iymdf[2], &f); 
    if (js == 0) { 
     iymdf[3] = (int) (f * denom); 
    } else { 
     j = js; 
    } 

    /* Return the status. */ 
    return j; 
} 

函數文檔:

/* 
** - - - - - - - - - - 
** i a u J d c a l f 
** - - - - - - - - - - 
** 
** Julian Date to Gregorian Calendar, expressed in a form convenient 
** for formatting messages: rounded to a specified precision. 
** 
** This function is part of the International Astronomical Union's 
** SOFA (Standards Of Fundamental Astronomy) software collection. 
** 
** Status: support function. 
** 
** Given: 
**  ndp  int  number of decimal places of days in fraction 
**  dj1,dj2 double dj1+dj2 = Julian Date (Note 1) 
** 
** Returned: 
**  iymdf  int[4] year, month, day, fraction in Gregorian 
**      calendar 
** 
** Returned (function value): 
**    int  status: 
**       -1 = date out of range 
**       0 = OK 
**       +1 = NDP not 0-9 (interpreted as 0) 
** 
** Notes: 
** 
** 1) The Julian Date is apportioned in any convenient way between 
**  the arguments dj1 and dj2. For example, JD=2450123.7 could 
**  be expressed in any of these ways, among others: 
** 
**    dj1   dj2 
** 
**   2450123.7   0.0  (JD method) 
**   2451545.0  -1421.3  (J2000 method) 
**   2400000.5  50123.2  (MJD method) 
**   2450123.5   0.2  (date & time method) 
** 
** 2) In early eras the conversion is from the "Proleptic Gregorian 
**  Calendar"; no account is taken of the date(s) of adoption of 
**  the Gregorian Calendar, nor is the AD/BC numbering convention 
**  observed. 
** 
** 3) Refer to the function iauJd2cal. 
** 
** 4) NDP should be 4 or less if internal overflows are to be 
**  avoided on machines which use 16-bit integers. 
** 
** Called: 
**  iauJd2cal JD to Gregorian calendar 
** 
** Reference: 
** 
**  Explanatory Supplement to the Astronomical Almanac, 
**  P. Kenneth Seidelmann (ed), University Science Books (1992), 
**  Section 12.92 (p604). 
** 
** This revision: 2010 July 27 
** 
** SOFA release 2010-12-01 
** 
** Copyright (C) 2010 IAU SOFA Board. See notes at end. 
*/ 
+0

您應該再次閱讀許可證並更改您的代碼。你不符合你的要求。我正在嘗試使用Ruby來完成與所有C SOFA代碼類似的構建。 – 2014-04-01 23:28:07

回答

1

你會想要一個標準的函數調用,這取決於你在做什麼。函數定義只是聲明「這些是我的輸入」,然後當您調用它時,必須提供實際值。你不重新創建函數def。

[DllImport("libsofa.dll")] 
myReturn = iauJdcalf(1, 2.0, 2.0, [1,2,3,4]); 

你的函數定義爲返回一個int,所以我們用myReturn變量捕獲它,你需要首先聲明它。然後我們提供一個int,兩個double和一個數組。你也可以傳遞這些類型的變量,注意確保你的數組的大小與需要的相同。

另外,看你的指針!如果你將一個變量傳遞給c,它將複製到函數中定義的局部變量。如果你給它一個指針,你需要在函數定義中聲明它,並且意識到我們現在正在玩指針遊戲。

0

爲了回答我自己的問題,我認爲會有更多涉及Marshaling的東西,但下面的代碼 提供了合理的輸出。

[DllImport("libsofa.dll")] 
    public static extern int iauJdcalf(int ndp, double dj1, double dj2, [MarshalAs(UnmanagedType.LPArray, SizeConst = 4)] int[] iymdf); 

以及本:

[DllImport("libsofa.dll")] 
    public static extern int iauJdcalf(int ndp, double dj1, double dj2, int[] iymdf); 
+1

這不是一個答案。而不是你的用戶帳戶,看它的外觀。 – 2011-04-27 00:46:37

4

刪除從C#聲明裁判關鍵字,這是不正確的。您還需要失去聲明中的初始化,這不是受支持的語法。在進行呼叫之前初始化數組。

[DllImport("libsofa.dll")] 
public static extern int iauJdcalf(int ndp, double dj1, double dj2, int[] iymdf); 
... 
    int[] arr = new int[] { 1, 2, 3, 4 }; 
    int retval = iauJdcalf(1, 2, 3, arr);