Dealing with unmanaged memory in c#

147 views Asked by At

I am using native C++ dll in c#, like this:

C++ functions:

poly* f1(/*some input data*/);
double* f2(poly* p);

In c#, I am doing:

(This is done using unsafe and fixed blocks appropriately, So don't worry about that)

void* ptr  = f1(/*input data*/);
double[] result = f2(ptr);

Now If I print data from f2, I get some data corrupted, garbage values. And some values are correct. I want to know, Is it due to saving that in void pointer ptr? Or is that fine and reason is something else?

1

There are 1 answers

0
Luaan On

I'd guess that the marshaller can't automatically convert double* to double[], because it has no idea how long the array is, and there could be a lot of other issues. You have to give us ideally at least the header file in C++, the declaration of your DllImported extern method in C# and then we can dig deeper into this :)