marshalling cascading C structures 
Author Message
 marshalling cascading C structures

Folks,

consider these cascading C?structures:

  typedef struct A_
  {
    int a1;

  } A;

  typedef struct B_
  {
    int numA;
    A *As;

  } B;

  typedef struct C_
  {
    int numB;
    B *Bs;

  } C;

which have been allocated dynamically.  How can I marshal this in VB.net
2008?

I tried this:

  <StructLayout(LayoutKind.Sequential)> _
    Public Structure A
      Public a1 As Integer
    End Structure

  <StructLayout(LayoutKind.Sequential)> _
    Public Structure B
      Public numA As Integer
      <MarshalAs(UnmanagedType.LPArray, SizeParamIndex := 0)> _
        Public As() As Point
    End Structure

  <StructLayout(LayoutKind.Sequential)> _
    Public Structure C
      Public numB As Integer
      <MarshalAs(UnmanagedType.LPArray, SizeParamIndex := 0)> _
        Public Bs() As Point
    End Structure

using `IntPtr' instead of the `C' structure everywhere if I don't have to
access the contents of the cascading arrays.  However, at one place I
actually have to access it, and converting `IntPtr' to `C' with

  Marshal.PtrToStructure(foo, GetType(C)), C)

fails with a TypeLoadException.

After hours of reading web resources I've finally come to this article

  http://www.*-*-*.com/

which mentions that using Marshal.Copy might work.  However, I consider this
quite inelegant.  Since I have control over the C?code (using the C?compiler
which comes with MS Visual Studio 9.0) I wonder whether I can `help' the
marshalling process on the C?side to avoid this additional copying in case
there is no other possibility.

Note, however, that I'm not a Windows user, and I use the compilers on the
command line only, so please be patient with me :-)

   Werner



Thu, 12 Jul 2012 15:10:40 GMT  
 marshalling cascading C structures

Quote:

>   typedef struct B_
>   {
>     int numA;
>     A *As;

Oops!  For clarity I've manually changed the structure names to something
logical, introducing this error (using `As' as a variable name).  This
doesn't affect the underlying problem, however.

    Werner



Thu, 12 Jul 2012 15:15:39 GMT  
 marshalling cascading C structures

Quote:


> >   typedef struct B_
> >   {
> >     int numA;
> >     A *As;
> Oops!  For clarity I've manually changed the structure names to something
> logical, introducing this error (using `As' as a variable name).  This
> doesn't affect the underlying problem, however.

Uff.  The error is of course not here but in the corresponding VB structure
(which I've given the same name).

    Werner



Thu, 12 Jul 2012 15:19:13 GMT  
 marshalling cascading C structures

Quote:

>How can I marshal this in VB.net 2008?

This newsgroup is for VB6 support.  I'd suggest asking in a VB.Net
newsgroup.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
  updated see http://www.autofeupdater.com/
Granite Fleet Manager http://www.granitefleet.com/



Thu, 12 Jul 2012 15:31:55 GMT  
 marshalling cascading C structures

Quote:
> >How can I marshal this in VB.net 2008?
> This newsgroup is for VB6 support.  I'd suggest asking in a VB.Net
> newsgroup.

Ok.  However, is there a difference in marshalling?  Or is this really
something specific to VB.net?  I could imagine that the interaction with
DLLs is quite similar...

    Werner



Thu, 12 Jul 2012 15:56:24 GMT  
 marshalling cascading C structures

Quote:
>> >How can I marshal this in VB.net 2008?

>> This newsgroup is for VB6 support.  I'd suggest asking in a VB.Net
>> newsgroup.

> Ok.  However, is there a difference in marshalling?  Or is this really
> something specific to VB.net?  I could imagine that the interaction with
> DLLs is quite similar...

VB.Net is a different language with different constructs that is not
backward compatible with VB6 and earlier, and converting code often require
a rewrite. That's how different they are. This group is for VB6 and
earlier(VB Classic). VB.Net and all dotnet groups have either "dotnet" or
"vsnet" in the group name. Please use the following
group instead:




Thu, 12 Jul 2012 17:49:18 GMT  
 marshalling cascading C structures

Quote:

> VB.Net is a different language with different constructs that is not
> backward compatible with VB6 and earlier, and converting code often
> require a rewrite.  That's how different they are.  This group is for VB6
> and earlier(VB Classic).  VB.Net and all dotnet groups have either
> "dotnet" or "vsnet" in the group name.  Please use the following group
> instead:


Thanks!

    Werner



Thu, 12 Jul 2012 23:42:28 GMT  
 marshalling cascading C structures

Folks,

consider these cascading C structures:

  typedef struct AA_
  {
    int aa1;

  } AA;

  typedef struct BB_
  {
    int numAA;
    AA *AAs;

  } BB;

  typedef struct CC_
  {
    int numBB;
    BB *BBs;

  } CC;

which have been allocated dynamically.  How can I marshal this in VB.net
2008?

I tried this:

  <StructLayout(LayoutKind.Sequential)> _
    Public Structure AA
      Public aa1 As Integer
    End Structure

  <StructLayout(LayoutKind.Sequential)> _
    Public Structure BB
      Public numAA As Integer
      <MarshalAs(UnmanagedType.LPArray, SizeParamIndex := 0)> _
        Public AAs() As Point
    End Structure

  <StructLayout(LayoutKind.Sequential)> _
    Public Structure CC
      Public numBB As Integer
      <MarshalAs(UnmanagedType.LPArray, SizeParamIndex := 0)> _
        Public BBs() As Point
    End Structure

using `IntPtr' instead of the `CC' structure everywhere if I don't have to
access the contents of the cascading arrays.  However, at one place I
actually have to access it, and converting `IntPtr' to `CC' with

  Marshal.PtrToStructure(foo, GetType(CC)), CC)

fails with a TypeLoadException.

After hours of reading web resources I've finally come to this article

  http://www.itags.org/dotnet/1288074/

which mentions that using Marshal.Copy might work.  However, I consider this
quite inelegant.  Since I have control over the C code (using the C compiler
which comes with MS Visual Studio 9.0) I wonder whether I can `help' the
marshalling process on the C side to avoid this additional copying in case
there is no other possibility.  The SAFEARRAY macro comes to my mind,
however, I haven't yet found the right document which covers such cascading
structures and gives a decent introduction.

Note, however, that I'm not a Windows user (and used to gcc only), and I use
the Visual Studio compilers on the command line only, so please be patient
with me :-)

   Werner



Thu, 12 Jul 2012 23:54:17 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Problem with Marshalling and Structures

2. Another CS-Realizer question

3. good VB website(s) for high school CS teacher??

4. CS-RCS with VB6

5. ANNOUNCE: CS-RCS Version 1.2

6. Announce: CS-RCS Version 1.1

7. System.TypeLoadException : Marshalling types to LPStruct is not supported on structure fields?

8. Marshalling for platform invoke

9. .NET Remoting - Marshalling Collections

10. Method Parameter Marshalling

11. API Calls and Marshalling

12. Structure within Structure

 

 
Powered by phpBB® Forum Software