struct在p/invoke中的转化

struct在p/invoke中的转化


在msdn上发现很好的实例,与大家共享!

enum调用实例:
//  Unions.cs

using System;
using System.Runtime.InteropServices;

/*
union MYUNION
{
    
int  i;
    
double  d;
};
*/

[ StructLayout( LayoutKind.Explicit )]
public  struct MyUnion 
{
    [ FieldOffset( 
0  )]
    
public   int  i;
    [ FieldOffset( 
0  )]
    
public   double  d;
}

/*
union MYUNION2
{
    
int  i;
    char str[
128 ];
};
*/

[ StructLayout( LayoutKind.Explicit, Size
= 128  )]
public  struct MyUnion2_1 
{    
    [ FieldOffset( 
0  )]
    
public   int  i;
}

[ StructLayout( LayoutKind.Sequential )]
public  struct MyUnion2_2 
{    
    [ MarshalAs( UnmanagedType.ByValTStr, SizeConst
= 128  )] 
    
public   String  str;
}

public  class LibWrap
{
    
//  void TestUnion(MYUNION u,  int  type)
    
    [ DllImport( 
" ..\\LIB\\PinvokeLib.dll "  )]
    
public  static extern void TestUnion( MyUnion u,  int  type );
    
    
//  void TestUnion( MYUNION u,  int  type )
    
    [ DllImport( 
" ..\\LIB\\PinvokeLib.dll "  )]
    
public  static extern void TestUnion2( MyUnion2_1 u,  int  type );
    
    
//  void TestUnion(MYUNION u,  int  type)
    
    [ DllImport( 
" ..\\LIB\\PinvokeLib.dll "  )]
    
public  static extern void TestUnion2( MyUnion2_2 u,  int  type );        
}

public  class App
{
    
public  static void Main()
    {
        MyUnion mu 
=   new  MyUnion();
        mu.i 
=   99 ;
        LibWrap.TestUnion( mu, 
1  );
        
        mu.d 
=   99.99 ;
        LibWrap.TestUnion( mu, 
2  );
        
        MyUnion2_1 mu2_1 
=   new  MyUnion2_1();
        mu2_1.i 
=   99 ;
        LibWrap.TestUnion2( mu2_1, 
1  );
        
        MyUnion2_2 mu2_2 
=   new  MyUnion2_2();
        mu2_2.str 
=   " *** string *** " ;
        LibWrap.TestUnion2( mu2_2, 
2  );        
    }
}

struct调用实例:
//  Structs.cs

using System;
using System.Runtime.InteropServices;

/*
typedef struct _MYPERSON
{
    char
*  first; 
    char
*  last; 
} MYPERSON, 
* LP_MYPERSON;
*/

[ StructLayout( LayoutKind.Sequential, CharSet
= CharSet.Ansi )]
public  struct MyPerson 
{
    
public   String  first; 
    
public   String  last;
}

/*
typedef struct _MYPERSON2
{
    MYPERSON
*  person;
    
int  age; 
} MYPERSON2, 
* LP_MYPERSON2;
*/

[ StructLayout( LayoutKind.Sequential )]
public  struct MyPerson2 
{
    
public  IntPtr person;
    
public   int  age;
}

/*
typedef struct _MYPERSON3
{
    MYPERSON person;
    
int  age; 
} MYPERSON3;
*/

[ StructLayout( LayoutKind.Sequential )]
public  struct MyPerson3 
{
    
public  MyPerson person;
    
public   int  age;
}

/*
typedef struct _MYARRAYSTRUCT
{
    bool flag;
    
int  vals[  3  ]; 
} MYARRAYSTRUCT;
*/

[ StructLayout( LayoutKind.Sequential )]
public  struct MyArrayStruct 
{
    
public  bool flag;
    [ MarshalAs( UnmanagedType.ByValArray, SizeConst
= 3  )] 
    
public   int [] vals;
}

public  class LibWrap
{
    
//   int  TestStructInStruct(MYPERSON2 *  pPerson2);
    
    [ DllImport( 
" ..\\LIB\\PinvokeLib.dll "  )]
    
public  static extern  int  TestStructInStruct( ref MyPerson2 person2 );
    
    
//  void TestStructInStruct3(MYPERSON3 person3)
    
    [ DllImport( 
" ..\\LIB\\PinvokeLib.dll "  )]
    
public  static extern  int  TestStructInStruct3( MyPerson3 person3 );    
    
    
//  void TestArrayInStruct( MYARRAYSTRUCT *  pStruct );
    
    [ DllImport( 
" ..\\LIB\\PinvokeLib.dll "  )]
    
public  static extern  int  TestArrayInStruct( ref MyArrayStruct myStruct );    
}

public  class App
{
    
public  static void Main()
    {
        
//   *******************  structure  with  pointer  to  other structure  ************
        MyPerson personName;
        personName.first 
=   " Sue " ;
        personName.last 
=   " Black " ;
        
        MyPerson2 personAll;
        personAll.age 
=   30 ;
        
        IntPtr buffer 
=  Marshal.AllocCoTaskMem( Marshal.SizeOf( personName ));
        Marshal.StructureToPtr( personName, buffer, 
false  );
        
        personAll.person 
=  buffer;
        
        Console.WriteLine( 
" \nPerson before call: "  );
        Console.WriteLine( 
" first = {0}, last = {1}, age = {2} "
            personName.first, personName.last, personAll.age ); 
        
        
int  res  =  LibWrap.TestStructInStruct( ref personAll );
        
        MyPerson personRes 
=  
            (MyPerson)Marshal.PtrToStructure( personAll.person, typeof( MyPerson ));
        
        Marshal.FreeCoTaskMem( buffer );
        
        Console.WriteLine( 
" Person after call: "  );
        Console.WriteLine( 
" first = {0}, last = {1}, age = {2} "
            personRes.first, personRes.last, personAll.age );
        
        
//   *******************  structure  with  embedded structure  ************     
        MyPerson3 person3 
=   new  MyPerson3();
        person3.person.first 
=   " Marie " ;
        person3.person.last 
=   " Claire " ;
        person3.age 
=   27 ;
        
        LibWrap.TestStructInStruct3( person3 );
        
        
//   *******************  structure  with  embedded  array   ************     
        MyArrayStruct myStruct 
=   new  MyArrayStruct();
        
        myStruct.flag 
=   false ;
        myStruct.vals 
=   new   int 3  ];
        myStruct.vals[ 
0  ]  =   1 ;
        myStruct.vals[ 
1  ]  =   4 ;
        myStruct.vals[ 
2  ]  =   9 ;
        
        Console.WriteLine( 
" \nStructure with array before call: "  );
        Console.WriteLine( myStruct.flag );
        Console.WriteLine( 
" {0} {1} {2} " , myStruct.vals[  0  ], 
            myStruct.vals[ 
1  ], myStruct.vals[  2  ] );
        
        LibWrap.TestArrayInStruct( ref myStruct );
        
        Console.WriteLine( 
" \nStructure with array after call: "  );
        Console.WriteLine( myStruct.flag );
        Console.WriteLine( 
" {0} {1} {2} " , myStruct.vals[  0  ], 
            myStruct.vals[ 
1  ], myStruct.vals[  2  ] );        
    }
}

其他的API,string,数组的调用实例:请下载:PlatformInvoke实例。

你可能感兴趣的:(struct在p/invoke中的转化)