Passing an long array from C++ to C#

M

markburnside

I'm trying to pass an array of longs from my C++ DLL to C# but I'm not
having much success any help would be great.

Here's what I'm doing and when I compile the C++ I get the error:
error C2664: 'API::Maintenance::Maint::SetArrayElements' : cannot
convert parameter 2 from 'long *' to 'int __gc[]'


extern "C" __declspec(dllexport) void PP_ShowMaint( char *szStr)
{
DialogResult dialogResult;


long *ar = new long[8192];

for (int i=0; i<8192; i++)
ar = i;

API::Maintenance::Maint *MaintScreen = new
API::Maintenance::Maint(szStrl);

MaintScreen->SetArrayElements(8192, ar);

dialogResult = MaintScreen->ShowDialog();
}

C#
public void SetArrayElements(int len,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]int[] ar)
{
int[] bbb = new int[10240];
Array.Copy(ar, 0, bbb, 0, len);
}
 
G

Guest

It would probably be easier to have your C# method accept a long[] (as in
System::Array of long) and in your c++ function allocate a managed array:

cli::array<Int64> ar = gcnew cli::array<Int64>(8192);

May not work as you may need a native array in the C++ code because you're
doing something more than just filling it with an int in a loop, but just an
idea ;)

- KH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top