LibWrap 类包含一组由 App 类调用的方法。有关传递数组的特定详细信息,请参见以下示例中的注释。默认情况下,作为引用类型的数组被作为 In 参数进行传递。为使调用方接收结果,必须将 InAttribute 和 OutAttribute 显式应用于包含该数组的参数。
声明原型
[C#]
// Declares a managed structure for each unmanaged structure.
[ StructLayout( LayoutKind.Sequential )]
public struct MyPoint
{
public int x;
public int y;
public MyPoint( int x, int y )
{
this.x = x;
this.y = y;
}
}
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
public struct MyPerson
{
public String first;
public String last;
public MyPerson( String first, String last )
{
this.first = first;
this.last = last;
}
}
public class LibWrap
{
// Declares a managed prototype for an array of integers by value.
// The array size cannot be changed, but the array is copied back.
[ DllImport( "..\\LIB\\PinvokeLib.dll" )]
public static extern int TestArrayOfInts([In, Out] int[] array, int size );
// Declares a managed prototype for an array of integers by reference.
// The array size can change, but the array is not copied back
// automatically because the marshaler does not know the resulting size.
// The copy must be performed manually.
[ DllImport( "..\\LIB\\PinvokeLib.dll" )]
public static extern int TestRefArrayOfInts( ref IntPtr array,
ref int size );
// Declares a managed prototype for a matrix of integers by value.
[ DllImport( "..\\LIB\\PinvokeLib.dll" )]
public static extern int TestMatrixOfInts([In, Out] int[,] pMatrix,
int row );
// Declares a managed prototype for an array of strings by value.
[ DllImport( "..\\LIB\\PinvokeLib.dll" )]
public static extern int TestArrayOfStrings( [In, Out]
String[] stringArray, int size );
// Declares a managed prototype for an array of structures with integers.
[ DllImport( "..\\LIB\\PinvokeLib.dll" )]
public static extern int TestArrayOfStructs([In, Out] MyPoint[]
pointArray, int size );
// Declares a managed prototype for an array of structures with strings.
[ DllImport( "..\\LIB\\PinvokeLib.dll" )]
public static extern int TestArrayOfStructs2( [In, Out]
MyPerson[] personArray, int size );
}
MI_CARD_API long WINAPI IssueUserCard(p_CARD user_card,unsigned long * CardSN,p_CARD user_card1)
{
memcpy(user_card1,user_card,9);
}
-------------------------------------------------------------------
vb中的代码
Public Declare Auto Function IssueUser Lib "test" (ByRef UCardStructure As CARD,ByRef aa As CARD) As Integer '发行用户
Public Structure CARD
Public BlockID As Byte
Public SecID As Byte
Public key() As Byte
Public CardID As Byte
End Structure
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
dim a,b as card
dim i as integer
redim a.key(6)
redim b.key(6)
a.BlockID =1
a.SecID =2
for i=0 to 5
a.key(i)=i+3
next i
a.CardID=9
i=IssueUserCard(a,b)
End Sub