convert libpointer to .NET System.IntPtr

조회 수: 3 (최근 30일)
Dan Nussinson
Dan Nussinson 2017년 5월 29일
답변: Philip Borghesani 2017년 5월 31일
I am working with 2 dlls. One is a C++ dll file with which I interact using calllib and libpointers. From the dll I am getting a pointer to a large array of data (~60Mb); The second dll I am working with is a C# assembly so I have to in interact with it using NET.addAssembly. I am trying to send the libpointer I get from the C++ dll, to the C# dll that excepts System.IntPtr type. But I couldn't find a way to convert between the two. I have tried:
C_sharp_ptr=System.IntPter(cpp_libpointer);
but that gives an error. I have also tried:
setdatatype(cpp_libpointer,'uint64Ptr',1,1);
C_sharp_ptr=System.IntPter(double(cpp_libpointer.value));
which does not give an error but C_sharp_ptr does not seem to point the the correct address. Please help. thanks, Dan
  댓글 수: 2
Guillaume
Guillaume 2017년 5월 29일
What is the type of the C++ pointer? What is the prototype of the C# function you want to call?
Dan Nussinson
Dan Nussinson 2017년 5월 29일
편집: Dan Nussinson 2017년 5월 30일
from the c++ dll I get two void pointers. i can Import the data to matlab using:
ptr1.setdatatype('uint16Ptr',number_of_bytes); DAT=get(ptr1,'Value');
But since the nubmer of bytes is large it takes a long time to import to workspace. The c# assembly expects 2 inputs of type System.IntPtr this pointers actually point to where the data of the libpointers point to. A perfect solution would be if I could somehow get the libpointer address. Then I could simply create the system intpointers using:
csharp_ptr1=System.IntPtr(address(Ptr1));
but I couldn't find a way to know the address of the libpointer.
thanks, Dan

댓글을 달려면 로그인하십시오.

답변 (1개)

Philip Borghesani
Philip Borghesani 2017년 5월 31일
One way to cheat and get the address is to edit the prototype file (create if needed) and change the prototype for the function returning the pointer to return a uint64 value. For instance if the function has an input argument of uint16PtrPtr then change to uint64Ptr and call csharp_ptr1=System.IntPtr(Ptr1.value); This can be done with an alias to the function if you still want access using the original argument types.
The other and more flexible way to work around this is to create a mex file or additional shared library and write some glue code or helper functions in c.

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by