Return an array from the unmanaged

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
 
W

Willy Denoyette [MVP]

| 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.
 

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