native dll calling with structs containing arrays of nested structs 
Author Message
 native dll calling with structs containing arrays of nested structs

Ok guys,
    I posted a message a few days ago about my inability to pass a structure
that contained a nested structure to a native dll, and the response that I
received was helpful, but I was wrong with my first question. I cannot pass
a structure that contains a nested array of other structures to a native
dll; I get a run-time exception during the dll call stating that the field
<whatever> cannot be marshaled as a structure member.

Here's some sample code (using redefined points and rects in this example):

[StructLayour(bla bla)]
public class TPoint{
    public int x=0;
    public int y=0;

Quote:
}

[StructLayour(bla bla)]
public class TRect{
    [MarshalAs( ?????? )]
    public TPoint[] boundaries = new TPoint[2];

Quote:
}

Anyway, when I attempt to pass a TRect, I get the exception. I have tried to
specify Struct as the MarshalAs type, ByValArray with a subtype of Struct,
asAny, almost everything, and I can't get it to work..

any ideas?
Trey Hutcheson



Mon, 22 Mar 2004 21:45:41 GMT  
 native dll calling with structs containing arrays of nested structs
Current (Beta2) Marshaler can't take this arrays!
Even with [MarshalAs( ..., SizeConst=n)] .

If you have fixed sized array of 2 TPoints, try:

==================================================
[StructLayout(LayoutKind.Sequential,Pack=1)]
public class TPoint
 {
 public int x = 0;
 public int y = 0;
 }

[StructLayout(LayoutKind.Sequential,Pack=1)]
public class TRect
 {
 public TPoint bound0 = new TPoint();
 public TPoint bound1 = new TPoint();
 }

TRect t = new TRect();
t.bound0.x = 1;
t.bound0.y = 2;
t.bound1.x = 11;
t.bound1.y = 12;

Quote:

> received was helpful, but I was wrong with my first question. I cannot pass
> a structure that contains a nested array of other structures to a native
> dll; I get a run-time exception during the dll call stating that the field
> <whatever> cannot be marshaled as a structure member.



Tue, 23 Mar 2004 00:50:38 GMT  
 native dll calling with structs containing arrays of nested structs
You have a couple of options here.

If the array of structures is a fixed length, there's a workaround where you
put one element of the struct where the first element goes, and then set the
size of the structure explicitly using [StructLayout] so that it's big
enough to hold multiple elements of the struct. You then use unsafe to get a
pointer to the first structure, and pointer arithmetic to walk through
"array".

The other option is to call write a small wrapper using the Managed
Extensions to C++, which I think is a better choice.


Quote:
> Ok guys,
>     I posted a message a few days ago about my inability to pass a
structure
> that contained a nested structure to a native dll, and the response that I
> received was helpful, but I was wrong with my first question. I cannot
pass
> a structure that contains a nested array of other structures to a native
> dll; I get a run-time exception during the dll call stating that the field
> <whatever> cannot be marshaled as a structure member.

> Here's some sample code (using redefined points and rects in this
example):

> [StructLayour(bla bla)]
> public class TPoint{
>     public int x=0;
>     public int y=0;
> }

> [StructLayour(bla bla)]
> public class TRect{
>     [MarshalAs( ?????? )]
>     public TPoint[] boundaries = new TPoint[2];
> }

> Anyway, when I attempt to pass a TRect, I get the exception. I have tried
to
> specify Struct as the MarshalAs type, ByValArray with a subtype of Struct,
> asAny, almost everything, and I can't get it to work..

> any ideas?
> Trey Hutcheson



Tue, 23 Mar 2004 02:13:45 GMT  
 native dll calling with structs containing arrays of nested structs
just try it by replacing class with struct and it will
work fine

public struct TPoint{
    public int x=0;
    public int y=0;

Quote:
}

have fun

Quote:
>-----Original Message-----
>Ok guys,
>    I posted a message a few days ago about my inability
to pass a structure
>that contained a nested structure to a native dll, and
the response that I
>received was helpful, but I was wrong with my first

question. I cannot pass
Quote:
>a structure that contains a nested array of other

structures to a native
Quote:
>dll; I get a run-time exception during the dll call

stating that the field
Quote:
><whatever> cannot be marshaled as a structure member.

>Here's some sample code (using redefined points and rects
in this example):

>[StructLayour(bla bla)]
>public class TPoint{
>    public int x=0;
>    public int y=0;
>}

>[StructLayour(bla bla)]
>public class TRect{
>    [MarshalAs( ?????? )]
>    public TPoint[] boundaries = new TPoint[2];
>}

>Anyway, when I attempt to pass a TRect, I get the

exception. I have tried to

- Show quoted text -

Quote:
>specify Struct as the MarshalAs type, ByValArray with a
subtype of Struct,
>asAny, almost everything, and I can't get it to work..

>any ideas?
>Trey Hutcheson

>.



Tue, 23 Mar 2004 18:43:44 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Initialisation of structs containing arrays of structs...?

2. Malloc & structs contain struct*

3. SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct

4. array that contains structs

5. Struct containing an array of unknown length

6. structs containing arrays

7. Defining MIDL structs that contain more than one conformant arrays

8. Mallocing data structs containing growing arrays of data

9. Passing structs containing arrays from COM

10. Nested struct array?

11. Problem with struct/array inside a struct using fread/fwrite

12. struct.array works - struct->pointer not

 

 
Powered by phpBB® Forum Software