C#调用C导出的带定长变量结构体函数

C定义

typedef struct CASH
{
    int CashNumber;
    CURRENCYTYPE Types[24];
} CASHTYPE; 

typedef struct CURRENCY
{
    char Name[2];
    char NoteType[6];
    int NoteNumber;
} CURRENCYTYPE;

typedef CASHTYPE* CASHTYPEPOINTER;

int APIENTRY GetData (CASHTYPEPOINTER cashData);

C#定义

using System;
using System.Runtime.InteropServices;

public static class MyCApi
{
    [StructLayout(LayoutKind.Sequential)]
    public struct CASHTYPE
    {
        public int CashNumber;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
        public CURRENCYTYPE[] Types;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct CURRENCYTYPE
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
        public string Name;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
        public string NoteType;
        public int NoteNumber;
    }

    [DllImport("MyCApi.dll")]
    public static extern int GetData(ref CASHTYPE cashData);
}

引用

https://stackoverflow.com/questions/1502526/using-p-invoke-to-call-a-function-from-a-c-api

你可能感兴趣的:(C#)