Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I am newbie in C++ programming. I work on small app which does high frequecy calculations. Application is programmed in C# however I want to amend C++ native library that would be cosumed by managed code. And would be implementable in C#. I am trying to pin pointers to managed to unmanaged, I am however unsure how to proceed with pinning

C++
System::Double^ FSStat::Average(System::Double data[], int x1, int x2 )
{
	UnmanagedArrayPointer* DataArray = GetUnmanagedArrayPointer(data);
	double* unRes = st->Average(DataArray->Data, x1, x2);
	System::Double^ res = gcnew Double(*unRes);
	delete unRes;
	delete DataArray;
	return res;
}

UnmanagedArrayPointer* FSStat::GetUnmanagedArrayPointer( System::Double data[] )
{
    double* arr = new double[sizeof(data)];
    for (int i = 0; i<sizeof(data); i++)
    {
        pin_ptr<double> pinned = &arr[i];
        data[i]->CopyToBuffer(pinned);

    }
	UnmanagedArrayPointer* res = new UnmanagedArrayPointer(arr* , data.Length);
	return res;
}



Error 1 error C2227: left of '->CopyToBuffer' must point to class/struct/union/generic type E:\C#\Aerion Workstation\FinStatsLib\FSStat.cpp 35 1 FinStatsLib


Could somebody advice me how may I proceed with this. Your help would be much appreciated.

With best regards,
Jakub
Posted
Comments
Philippe Mori 2-Mar-12 19:41pm    
I don't understand what you are trying to do. Code samples does not make much sense (too complicate and improper use of managed/unmanaged types). See my solution for some hints.

Firstly the following is wrong:
C++
double* arr = new double[sizeof(data)];

This will not get the size of the array passed in to this method, but the size of a pointer. You will need an extra parameter that gives the array size.
Secondly, the following line is wrong:
C++
data[i]->CopyToBuffer(pinned);

because System::Double[^] does not contain a CopyToBuffer() method.
 
Share this answer
 
Comments
KubikKE 2-Mar-12 12:11pm    
Ah ok, so System::Double data[] does not actually behave as array<system::double>? so if I use this instead? would i be able to use data->Length ?
I have seen some componenet stuff where CopyToBuffer() function was used.

What I actually want is to avoid copying of the value what is being done by marshalizing if I understood well. I would like to pin pointer only. Any thoughts about how can I proceed?

Managed C++ -> if I have library with function with parameters array<double>, would I be entering System.Double[] variable or do I need to use System::Double[^] parameter
Richard MacCutchan 2-Mar-12 15:19pm    
I'm no expert in managed/unmanaged code, so would refer you to the P/Invoke tutorial for more information on marshalling. Although it has a slant towards C# it is still relevant to managed C++.
Philippe Mori 2-Mar-12 19:38pm    
In C++/CLI, [] is used to declare unmanaged arrays (as in standard C++). You have to uses cli::array for managed arrays.
Richard MacCutchan 3-Mar-12 5:04am    
Don't tell me, tell the questioner.
As far as I know, you can pin a pointer (How to: Pin Pointers and Arrays[^]) from a cli::array[^], call unmanaged code and then get back the result.

And for such scenario, it is probably not even necessary to use C++/CLI as you can use P/Invoke from C#.

Calling Win32 DLLs in C# with P/Invoke[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900