473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2165
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
0
by: Daimy | last post by:
I meet the same problem below, please help me! Thanks! //written by some one I have developed a windows forms user control, which I am going to host in Internet Explorer.. I am familiar...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
11
by: Marc Pelletier | last post by:
Hello, I am having trouble implementing the following callback: CNCSError CECWCompressor::WriteReadLine(UINT32 nNextLine, void **ppInputArray) where ppInputArray is a 3 by x array. The...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
5
by: Richard Lewis Haggard | last post by:
I am trying to create multi-dimensioned arrays in conventional ASP pages and pass these arrays as arguments to functions that are in a C# interop assembly. ASP complains because it doesn't...
3
by: borewill | last post by:
I have a dll writtin in native C++ which provide such interface: typedef struct _PARAM { LPWSTR swName; DWORD dwOptions; } PARAM,*PPARAM,*LPPARAM; DLLEXPORT BOOL CALLBACK Function(LPPARAM...
6
by: Jeff | last post by:
Dear experts! ..NET 2.0 I'm trying to make an array containg multiple datatypes. This array will consist of 3 items (string, string, integer): my first try was this, (of course it fails)...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.