Return an array from the unmanaged

  • Thread starter Thread starter serj
  • Start date Start date
S

serj

Hello,

I try to edit an array of double (or whatever type) allocated in the
managed code from the unmanaged code.
Is it possible ?
I have an unsupported exception with the below code.

void ExpTest(double* tab)
{
for(int i=0;i<3;i++)
{
tab=i;
}
}

[DllImport("***.dll")]
static extern void ExpTest(ref double[] ouput);

public void Test(ref double[] output)
{
try
{
ExpTest(ref output);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Thx
 
| Hello,
|
| I try to edit an array of double (or whatever type) allocated in the
| managed code from the unmanaged code.
| Is it possible ?
| I have an unsupported exception with the below code.
|
| void ExpTest(double* tab)
| {
| for(int i=0;i<3;i++)
| {
| tab=i;
| }
| }
|
| [DllImport("***.dll")]
| static extern void ExpTest(ref double[] ouput);
|
| public void Test(ref double[] output)
| {
| try
| {
| ExpTest(ref output);
| }
| catch (Exception ex)
| {
| MessageBox.Show(ex.ToString());
| }
| }
|
| Thx
|

Remove the ref keyword, the interop marshaler will pass a (pinned) pointer
to the array's first element.
But beware, what you are doing is dangerous, you must make sure you don't
write more elements than the the passed array can hold. There is no way for
the unmanaged side to know the length of the array, so you need to pass this
size as well.

Willy.
 
Back
Top