Pass C # link to C ++ pointer and back

I would like to pass a C # link to C / C ++ code (pointer) and then return it from a pointer to a C # link. I have a lot of pairs (uint, Object). I created a sorting mechanism in C code because it is much faster than C #. There is only a key (uint) and a value (object reference). C code uses the key to sort and does not change the value (pointer). Is there an easy way to do this? Do I need to use sorting? Function C will be called many times (maybe even a million times), so I'm afraid that it will be too slow, and I don’t even know how to do this with sorting. I believe that when the address of an object changes by GC, the address of the C # link does not change. Thus, it will not be necessary to place the object in invented memory. I'm right? Now I can call the C function using DllImport, I can save the C # link to the C pointer, but I can not get the address stored in the C pointer to the C # link.

Any ideas on how to do this?

+4
source share
2 answers

It is not possible to pass any variables directly from managed C # code to native C ++ code. The solution is pinvoke, where you can marshal data.

This works great, but you should be aware that this is not a decision every time to fast. With each call, the memory should be copied and possibly converted depending on the data types.

+1
source

My solution to a similar problem, I had old code in Fortan. I convert this code to c and then created a C ++ managed project. In C #, I called this managed project C ++.

C # code is as follows:

unsafe
{
double * input = (double *) utils.Memory.Alloc (sizeof (double) * n);
double * output = (double *) utils.Memory.Alloc (sizeof (double) * n);
// call C ++ code
c_plus_plus.code (input, output); // now the output contains the output file // (you can use the same array as the input and output
}

Hope this helps.

0
source

All Articles