안녕하세요.
C에서 구조체를 콜백으로 받아, 데이터를 사용하려고 합니다.
C
typedef Structure item
{
char *item;
}
typedef Structure itemList
{
int count;
item* itemList;
}
cb(itemList);
C#
public structure item
{
string data;
}
public Structure ItemList
{
int count
item[] item;
}
public void cb(IntPtr p)
{
ItemList list = Marshal.PtrToStructure<ItemList>(p);
print(list.count) // 정상
print(list.item[i].data); // 쓰레기 값
item[] item = new Item[count.
}
각 구조체 및 콜백은 위와 같은 형태인데요.
IntPtr로 데이터를 받아 다음과 같이 풀어내면 ItemList 데이터에는 접근이 가능한데, ItemList 값을 찍어보면 정크 값이 출력됩니다.
item 배열에 list.count 만큼 메모리 할당 후 값 출력해 봐도 동일한 문제가 발생하네요.
제 접근 방식이 잘못되었을까요?
조언 부탁드립니다. 감사합니다.