2011-04-26 4 views
2

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를 사용하여 모든 SOAP 코드와 비슷한 빌드를 작성하려고합니다. –

답변

1

당신은 당신이 무엇을하고 있는지에 따라, 표준 함수 호출을 원하는됩니다. 함수 정의는 단순히 "this is my inputs"라고 말하면, 호출 할 때 실제 값을 제공해야합니다. 함수 def를 다시 작성하지 마십시오.

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

함수는 int를 반환하므로 myReturn 변수로 캡처하므로 함수를 먼저 선언해야합니다. 그런 다음 int, 2 개의 double 및 배열을 제공합니다. 또한 배열이 필요한만큼 동일한 지 확인하기 위해 이러한 유형의 변수를 전달할 수 있습니다.

또한 포인터를보세요! 변수를 c에 전달하면 함수에 정의 된 지역 변수로 복사됩니다. 포인터를 주면 함수 정의에서 포인터를 선언해야합니다. 이제 포인터 게임을한다는 것을 알아야합니다.

0

내 자신의 질문에 너무 답이 너무 많아서 마샬링 관련 항목이 더 많이 포함될 것이라고 생각했지만 코드 아래 코드는 적절한 결과를 제공합니다.

[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

이것은 대답이 아닙니다. 그리고 귀하의 사용자 계정이 아니라 그것을 보입니다. –

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); 
관련 문제