2013-07-18 2 views
3

관리되지 않는 배열을 가리키는 다른 IntPtr을 가리키는 IntPtr이 있습니다. 이 관리되지 않는 배열을 관리 대상 배열에 어떻게 복사 할 수 있습니까? 나는 마샬 (Marshal)을 사용해야 할 것임을 압니다. 그러나 포인터에 포인터가있을 때 포인터를 사용할 때 포인터를 사용할 방법이 확실하지 않습니다. 여기 이 배열의 정보를 다시 얻으려면 어떻게해야합니까?

내 예제 코드

관리되지 않는 C++입니다 :

void foo(Unsigned_16_Type** Buffer_Pointer); 

관리 C 번호 : ppUnmanagedBuffer 내부 배열에 내가 IntPtr입니다에 IntPtr입니다이이 시점에서 이제

[DllImport("example.dll")] 
     public static extern void foo(IntPtr Buffer_Pointer); 
//... 
//... 

int[] bufferArray = new int[32]; 


IntPtr p_Buffer = (IntPtr)Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(int)) * bufferArray.Length); 
Marshal.Copy(bufferArray, 0, p_Buffer, bufferArray.Length); 

GCHandle handle = GCHandle.Alloc(p_Buffer, GCHandleType.Pinned); 
IntPtr ppUnmanagedBuffer = (IntPtr)handle.AddrOfPinnedObject(); 

//Call to foo 
foo(ppUnmanagedBuffer); 

하지만 마샬 (Marshal)을 사용하여 배열을 새로운 관리 대상으로 복사하는 방법을 잘 모르겠습니다. 복사

나는

int[] arrayRes = new int[word_count]; 
Marshal.Copy(ppUnmanagedBuffer, arrayRes, 0, word_count); 

같은 시도하지만 그것은 남아있는 유일한 것은 당신이 기대하는 데이터의 종류를 가리 키도록 ppUnmanagedBuffer을 얻기 위해 다음 호출 "취소"하는 것입니다

답변

0

작동하지 않습니다

C 번호는 GIV 관리 한 경우

GCHandle handle = GCHandle.Alloc(p_Buffer, GCHandleType.Pinned);

IntPtr ppUnmanagedBuffer = (IntPtr)handle.AddrOfPinnedObject();

Marshal.Copy((IntPtr)(GCHandle.FromIntPtr(ppUnmanagedBuffer).target), arrayRes, 0, word_count);

(구문은 약간 떨어져있을 수 있습니다,하지만 일반적인 생각 : 당신이 메커니즘에 의해 int**의 동등한 전자, 당신은과 같이, 한번 int[]에 상응하는 얻는 역 참조 할 필요가 ...)

+0

세 번째 인수는 시작 인덱스입니다. Marshal.Copy (IntPtr source, int [] destination, int startIndex, int length); derefence는 어떻게 적용합니까? – user2134127

+0

내 첫 번째 복사본은 int 배열을 IntPtr에 복사하고, 두 번째는 IntPtr을 int 배열에 복사합니다. 그것이 논쟁이 다른 이유입니다. 또한 C#에서 포인터와 참조를 사용할 수 없습니다. 사용할 수있는 equivelent가 있습니까? – user2134127

+0

나는 현재 나의 대답이 정확하다고 믿는다. 이전에 완전히 이해할 수있는 시간을 들이지 않고 게시하는 것에 대해 사과드립니다. – abiessu

관련 문제