I는 C DLL에서 내 보낸 다음과 같은 기능이 있습니다 :출력 바이트 배열을 마샬링하는 올바른 방법입니까?
int result = -1;
byte[] buffer = new byte[2048];
int length = buffer.Length;
result = Simulator.GetAttributeValue(
tag.NativeId,
element_index,
attribute_index,
buffer,
ref length
);
int[] output = new int[length];
for (int i = 0; i < length; i++)
{
output[i] = buffer[i];
}
return output;
해봤 또 한가지 :
// C
BOOL WINAPI GetAttributeValue(
IN TAG * psTag,
IN DWORD dwEltIdx,
IN DWORD dwAttrIdx,
OUT BYTE * pbBuffer,
IN OUT DWORD * pdwLen)
// C#
[DllImport(Simulator.ASSEMBLY, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
public extern static int GetAttributeValue(
IntPtr tag_id,
int element_index,
int attribute_index,
[In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=4)]
byte[] data_buffer,
[In, Out]
ref int data_length
);
이 내가 SO에 여기에 몇 가지 응답에 따라, 그것을 사용하기 위해 노력하고있어 어떻게 이것은 또한 SO에서 발견 대답을 기반으로합니다
[DllImport(Simulator.ASSEMBLY, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
public extern static int GetAttributeValue(
IntPtr tag_id,
int element_index,
int attribute_index,
IntPtr data_buffer, // changed this
[In, Out]
ref int data_length
);
// snip
GCHandle pinned_array = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr pointer = pinned_array.AddrOfPinnedObject();
result = Simulator.GetAttributeValue(
tag.NativeId,
element_index,
attribute_index,
pointer,
ref length
);
// snip, copying stuff to output
pinned_array.Free();
return output;
자, 두 경우 모두, 내 length
제대로 채워 것 같다,하지만 buffer
은 항상 비어 있습니다. 나는 P/Invoke와 marshalling에 정통하지 않으므로, 이것이 올바른지 확실하지 않습니다. 이 작업을 수행하는 더 좋은 방법이 있습니까?
다른 모든 것이 실패하면 C++/cli에 래퍼 작성을 고려하십시오. 이렇게 많은 호출을하면 작성하고 디버그하는 것이 더 쉬울 수도 있습니다. –
@ jdv-JandeVaan : 운좋게도 버퍼가있는 API 함수가 두 개뿐입니다. 그렇지 않으면 큰 어려움이 있습니다. 아무도 코드까지는 아무 것도 발견하지 못해서 다행입니다.> _> –