471,331 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,331 software developers and data experts.

float array param in interop

I've got a C method I'm trying to call and I seem to be having problems with
a reference parameter.

The C method is:
void CalculateValues(int nInputs, float* pInputs, int nOutputs, float*
pOutputs)
My method import looks like this:

[DllImport("MyLib")]
extern public static void CalculateValues(int nInputs, float[] inputs, int
nOutputs, ref float[] pOutputs);

My call looks like this:

float[] outputs = new float[nOutputs];
CalculateValues(nInputs, inputs, nOutputs, ref outputs);

The problem is, in the C code, when it's setting values in the outputs
array, it crashes setting the 25th value even though nOutputs is 512 and the
managed array has a length of 512.

For the inputs array, I seem to be able to access all the elements, but then
it's not a ref, and I assume the ref has something to do with it.

I tried changing the C parameter to float**, thinking maybe it needed to be
a pointer to an array for the ref to work, but the same thing happens...

What am I missing?

Thanks.
Aug 3 '08 #1
6 2102
Fredo <fr***@hotmail.comwrote:
I've got a C method I'm trying to call and I seem to be having problems with
a reference parameter.

The C method is:
void CalculateValues(int nInputs, float* pInputs, int nOutputs, float*
pOutputs)
My method import looks like this:

[DllImport("MyLib")]
extern public static void CalculateValues(int nInputs, float[] inputs, int
nOutputs, ref float[] pOutputs);
Why have you put a "ref" on pOutputs, but not on inputs? Note that the
declaration for both is the same in the C method.

I suspect that just not passing it by reference will do the trick. You
would want ref (and float**) if you wanted the C code to do the
allocation. (I don't know the details of how the GC works out in that
case though.)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 3 '08 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Fredo <fr***@hotmail.comwrote:
>I've got a C method I'm trying to call and I seem to be having problems
with
a reference parameter.

The C method is:
void CalculateValues(int nInputs, float* pInputs, int nOutputs, float*
pOutputs)
My method import looks like this:

[DllImport("MyLib")]
extern public static void CalculateValues(int nInputs, float[] inputs,
int
nOutputs, ref float[] pOutputs);

Why have you put a "ref" on pOutputs, but not on inputs? Note that the
declaration for both is the same in the C method.
Because CalculateValues sets the values in the output array. I could just do
it as an "out", but I thought it'd be easier to just let it be marhsalled
both ways.
I suspect that just not passing it by reference will do the trick. You
would want ref (and float**) if you wanted the C code to do the
allocation. (I don't know the details of how the GC works out in that
case though.)
I managed to get it working, but now I can't remember exactly what I did to
get it working, and now I can't figure out what I changed, which is a little
disappointing. But it works.
Aug 3 '08 #3
Fredo <fr***@hotmail.comwrote:
Why have you put a "ref" on pOutputs, but not on inputs? Note that the
declaration for both is the same in the C method.

Because CalculateValues sets the values in the output array. I could just do
it as an "out", but I thought it'd be easier to just let it be marhsalled
both ways.
I'll admit I don't know much about marshalling, but I would still have
expected it to work without out/ref.
I suspect that just not passing it by reference will do the trick. You
would want ref (and float**) if you wanted the C code to do the
allocation. (I don't know the details of how the GC works out in that
case though.)

I managed to get it working, but now I can't remember exactly what I did to
get it working, and now I can't figure out what I changed, which is a little
disappointing. But it works.
With what signatures?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 3 '08 #4
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
Because CalculateValues sets the values in the output array. I could just do
it as an "out", but I thought it'd be easier to just let it be marhsalled
both ways.

I'll admit I don't know much about marshalling, but I would still have
expected it to work without out/ref.
I've just read a bit more about array marshalling, and indeed I can see
why "ref" would be appropriate. I should really look into marshalling
some time...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 3 '08 #5
It's a nightmare. As for what the signatures were, they are unchanged. I
somehow fixed it without changing the signature. Originally I tried a
MarshalAs attribute with a SizeParamIndex argument, but I removed that
because something else I did fixed it, but again, no recollection of what it
was.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
Because CalculateValues sets the values in the output array. I could
just do
it as an "out", but I thought it'd be easier to just let it be
marhsalled
both ways.

I'll admit I don't know much about marshalling, but I would still have
expected it to work without out/ref.

I've just read a bit more about array marshalling, and indeed I can see
why "ref" would be appropriate. I should really look into marshalling
some time...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Aug 4 '08 #6
Fredo wrote:
It's a nightmare. As for what the signatures were, they are unchanged. I
somehow fixed it without changing the signature. Originally I tried a
MarshalAs attribute with a SizeParamIndex argument, but I removed that
because something else I did fixed it, but again, no recollection of what it
was.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
>Jon Skeet [C# MVP] <sk***@pobox.comwrote:
>>>Because CalculateValues sets the values in the output array. I could
just do
it as an "out", but I thought it'd be easier to just let it be
marhsalled
both ways.
I'll admit I don't know much about marshalling, but I would still have
expected it to work without out/ref.
I've just read a bit more about array marshalling, and indeed I can see
why "ref" would be appropriate. I should really look into marshalling
some time...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Hi there, can you please post your amended code ?
Aug 5 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Robert Oschler | last post: by
8 posts views Thread by intrepid_dw | last post: by
11 posts views Thread by Marc Pelletier | last post: by
3 posts views Thread by borewill | last post: by
6 posts views Thread by Jeff | last post: by
2 posts views Thread by yeshello54 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.