473,242 Members | 1,350 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,242 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 2162
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.