c# - P/Invoke Marshaling float*& -


I am trying to use some C + + dll function by using P / Invoke from c # application. Their headers are:

  zero F1 (float * a, float * b, long n, float * r); Zero F2 (Float * A, Float * B, Long N, Float & amp; R); The difference in use is that F1 is obtained as the fourth parameter, which is an array created outside the function while F2 preserves memory and fills the array in it. I can declare F1. [DllImport ("myLib.dll", Calling Convention = Calling Convention. CDEcl)] Private Static Exxon Zero F1 (Float [] A, Float [] B, UIT N, Float [] R);  

and calling it with the program:

  float [] A = new float [] {1,2,3}; Float [] b = new float [] (4,5,6}; UIT n = 3; float [] r = new float [n]; f1 ​​(a, b, n, r);   

This works well.

But I do not know how to use F2 I declare it:

  [DllImport ( "MyLib.dll", Calling Convention = Calling Convention. CDEcl) Personal Static Extern Zero F2 (Float [] A, Float [] B, UIT N, Float [] R);  

When I call f2 with an unused array:

  float [] r = null; F2 (A, B, N, Out R);  

A system. AccessViolationException is thrown when I try to reach R.

Any sugestions? Thanks in advance.

P / Awaz Marshler can not control the last parameter as an array. There should be a size, whereas in C they are just memory blocks, because the margarar is not aware of the size of the array, so it is not controlled by it. May be not. Although you can manually handle it by using IntPtr

  [DllImport ("myLib.dll", Calling Convention = Calling Convention. CDEcl)] Personal Static extension zero f2 (float [] a, float [] b, uit n, out intpetrate rptr);  

You can then drag the array values ​​using the pointer:

  float [] r = new float [size]; Martial Copy (RPTR, R, 0, Size);  

In addition to this, you will need to call the function in your DLL to free the memory allocated for the array, because there is no way to do the free storage The C was not allocated in DLL.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -