2012-02-21 3 views
1

제 C# 프로젝트에서 사용하고자하는 제 3 자 c dll이 있습니다. 필자는 파일의 헤더를 읽는 한 가지 방법을 가져올 수있었습니다. 이제 데이터를 읽는 메소드에 액세스하려고합니다. 문제는 String 배열을 포함하는 구조체이므로 StringBuilder 목록과 같은 다양한 시도를했습니다. 문자열 목록을 만들고 배열로 전달하여 문자열 배열을 직접 만드는 것입니다 (아래 그림 참조). 하루 종일이 일을 이미 마친 후에 나는 더 이상 무엇을해야 할지를 모릅니다. 나는 단지 내가 십진수 배열을 내가 지금하는 방식으로 전달할 수 있는지도 모른다. (http://msdn.microsoft.com/en-us/library/ac7ay120(v=vs.100).aspx에 열거되지 않았기 때문에). 의 DLLC# 문자열 배열을 포함하는 구조체의 dllimport

C 헤더 :

public enum id_retrieve_enum 
{ 
    GET_ID = 1, 
    DO_NOT_GET_ID, // id is in file  
    NO_ID_IN_FILE, 
    CREATE_ID  // id is not in file 
}; 

[StructLayout(LayoutKind.Sequential)] 
public struct id_struct 
{ 
    [MarshalAs(UnmanagedType.SafeArray)] 
    public String[] values; 
    public int size; 
    public id_retrieve_enum retrieve; 
}; 

같이 DllImport :

[DllImport("libpandconv.dll", CallingConvention = CallingConvention.Cdecl)] 
public static extern int importdata(
    String fullfilename, 
    int numrows, 
    int numcols, 
    int startrow, 
    Decimal[] dataset, 
    [In, Out, MarshalAs(UnmanagedType.Struct)] ref id_struct ids); 

초기화 데이터 및 통화 방법

Decimal[] data = new Decimal[numpoints * highdim]; 

id_struct ids = new id_struct(); 
    ids.retrieve = (hasid.Equals(1)) ? id_retrieve_enum.GET_ID : id_retrieve_enum.CREATE_ID; 
    ids.values = new String[numpoints]; 

importdata(inputfilename, numpoints, highdim, startrow, data, ref ids); 
ENUM 및 구조체

enum id_retrieve_enum { 
    GET_ID = 1, 
    DO_NOT_GET_ID, // id is in file 
    NO_ID_IN_FILE, 
    CREATE_ID  // id is not in file 
}; 

struct id_struct { 
    char **values;      // allocated 
    int size;       // optional: DEFAULT_VALUE = NULL_INT 
    enum id_retrieve_enum retrieve; // optional 
}; 

int importdata(char *fullfilename, int numrows, int numcols, int startrow, 
       decimal *dataset, struct id_struct *ids); 

C# 코드

여기서 inputfilename는 이미 마샬링되었으며 이미 가져온 방법 importheader에 의해 numpoints, highdimstartrow이 이미 반환되었습니다.

+0

문자열 배열 대신 2 차원 문자 배열을 전달하려고 시도한 적이 있습니까? –

+0

'values'에는'IntPtr'을, 손으로 마샬링 할 필요가 있습니다. C++/CLI 계층도 가능합니다. 이 문자열 배열을 누가 할당하고 어떻게 해제 될 것으로 예상됩니까? –

+0

나는 이것을 시도했다 [this] (http://stackoverflow.com/questions/1498931/marshalling-array-of-strings-to-char-in-c-must-be-quite-easy-if-you-know-h)'IntPtr'를 사용했지만 그때는 작동하지 않았습니다. 나는 그것을 다시 지금 시험 할 예정이다, 고마워! –

답변

2

배열 대신 IntPtr을 사용하고 수동으로 해결해 보았습니까?

관련 문제