Marshaling structures with arrays 
Author Message
 Marshaling structures with arrays

[StructLayout(LayoutKind.Sequential,
CharSet=CharSet.Ansi, Pack=1)]

public struct UNIT
{
    public short        Number ;
    [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=5 ) ]
    public string       UnitID;
    public short        Type ;
    public string       CurrencyID;
    public int          Values ;
    public int          InitialCount ;
    public int          Count ;
    public int          Minimum ;
    public int          Maximum ;
    public int          AppLock ;
    public int          DevLock ;
    public short        Status ;

Quote:
}

[ StructLayout( LayoutKind.Sequential, Pack=1 ) ]
public struct INFO
{
    public short  Count;
    public IntPtr List; // UNIT lppList

Quote:
}

I have to call a function defined in a dll written in C.
This function  
is toInf( INFO * lpInfo ) that receive a pointer to a
INFO struct.
There isn't any problem yet.

But the List field of the INFO struct is a Pointer to an
array of
pointers to  UNIT structures. This is the problem, i
don't know how to
set this field.

Im tryng

INFO    info;
UNIT[]  units = new UNIT[ 5 ];
IntPtr  ptrInfoUnits = IntPtr.Zero;
IntPtr  ptrInfo = IntPtr.Zero

//...

// Here i fill the units array

//...

ptrInfoUnits = Marshal.AllocHGlobal( Marshal.SizeOf(
typeof( UNIT ) ) );
Marshal.StructureToPtr( units[ 0 ], ptrInfoUnits ,
false );

info.List = ptrInfoUnits;      
info.Count = 5;

ptrInfo = Marshal.AllocHGlobal( Marshal.SizeOf( typeof(
INFO) ) );
Marshal.StructureToPtr( info, ptrInfo , false );

toInf( ptrInfo );

This doesn't give me any good results

My problem is how to set the List field of the INFO
struct.
How to set a pointer to an array of pointers to UNIT
structures ??  

May be a IntPtr pointing to an array of IntPtr each one
pointing to an UNIT info ???

Thanks !!!



Sun, 22 May 2005 04:57:41 GMT  
 Marshaling structures with arrays
Unfortunately, you didn't post the real C++ code,
thus I can only assume you have to use code like:

// ======================================= USE AT YOUR OWN RISK ==============================

  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)]
 public struct UNIT
 {
  public short Number ;
  [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=5 ) ]
  public string UnitID;
  public short Type ;
  public string CurrencyID;   // are you realy sure this is not ByValTStr?  <<<<<<<<<<<<<<<<<
  public int Values ;
  public int InitialCount ;
  public int Count ;
  public int Minimum ;
  public int Maximum ;
  public int AppLock ;
  public int DevLock ;
  public short Status ;
 }

 [ StructLayout( LayoutKind.Sequential, Pack=1 ) ]
 public struct INFO
 {
  public short  Count;
  public IntPtr List; // UNIT lppList
 }

 .....................
  // wild guess:
  [DllImport("YourDll.dll", CharSet=CharSet.Ansi)]
  public static extern int toInf( ref INFO inf );

 .....................
   UNIT[] uni = new UNIT[5];
    uni[0].UnitID = "Test";   // just for testing
    uni[0].CurrencyID = "$$$";
    uni[4].UnitID = "Last";
    uni[4].CurrencyID = "###";

   INFO inf = new INFO();
   inf.Count = (short) uni.Length;

   Type typ = typeof( UNIT );
   int size = Marshal.SizeOf( typ );
   inf.List = Marshal.AllocHGlobal( size * uni.Length );

   int run = (int) inf.List;
   for( int i = 0; i < uni.Length; i++ )
   {
    Marshal.StructureToPtr( uni[i], (IntPtr) run, false );
    run += size;
   }

   toInf( ref inf );

   // DestroyStructure-loop is only needed if 'UNIT.CurrencyID' realy is a plain string  (no ByValTStr)
   run = (int) inf.List;
   for( int i = 0; i < uni.Length; i++ )
   {
    Marshal.DestroyStructure( (IntPtr) run, typ );
    run += size;
   }

   Marshal.FreeHGlobal( inf.List ); inf.List = IntPtr.Zero;

// =========================================================================================

--
 Thomas Scheidegger - MVP .NET - 'NETMaster'
 http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/



Sun, 22 May 2005 06:29:56 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Marshaling a structure containing a aray of different structures

2. An array in a structure Or an Array Of Structures

3. Converting byte[] to structure without Marshal?

4. Marshal problem with pointers to structures

5. Marshal..marshaling..

6. IJW Marshaling arrays and matrices

7. marshal an array from managed C++ to unmanaged C++

8. Zero-length strings in marshaled arrays

9. Passing structs and arrays --- marshaling and IDL issues...

10. Marshaling Arrays of Strings

11. how to marshal C style array in a struct

12. Array of Structures in a Structure.

 

 
Powered by phpBB® Forum Software