|
Is it possible to pass parameters from unmanaged code to managed code by reference?
I am programming agents for the Metta Trader 4 (MT4) platform using MetaQuotes Language 4 (MQL4); a C like language.
Normally one would use C++ to generate DLLs but I would like to use C# if I could because when you release something in C++ it is frozen in time as opposed to when you release something in C#. With C# if problems are detected in the underlieing classes they are corrected by normal updates of the .NET platform. This is an advantage I would like to take advantage of to simplify maintenance of products I hope to sell.
I will need to pass large arrays of market data to these DLLs for analysis and would like to do this by reference.
Here is an example of how to pass an array by value:
[DllExport(EntryPoint = "SumIntegerArray")]
public static int Sum([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]int[] values, int count)
{
int result = 0;
for (int i = 0; i < count; i++)
result += values[i];
return result;
}
How would I pass the array by reference to the DLL?
|
|
|
|
|
You could pass a pointer and use unsafe on the C# end instead of arrays. AFAIK you can't (easily) pretend that a native array is a managed array.
|
|
|
|
|
Thanks for the information, I will try it.
I am just unsure how to accomplish it in C#. I didn't know you could use a pointer in managed code.
Off to the articles to see if I can find out how.
|
|
|
|
|
It mostly works as you'd expect, same syntax and all, but you can't take the address of managed objects and you need "unsafe" keywords (and enable unsafe code in the build options).
Also, "int *x, y" makes both of them pointers, not just x.
|
|
|
|
|
Thanks for the information.
|
|
|
|
|
As an alternative, you could put it in a memory mapped file[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I don't think it would be applicable in my case but I will look into them.
Thanks for the information.
|
|
|
|