使用 p/invoke 从 C api 调用函数

发布于 2024-08-06 12:18:50 字数 426 浏览 3 评论 0原文

我有一个 C api,并且正在使用 p/invoke 从我的 C# 应用程序中的 api 调用函数。函数签名为:

int APIENTRY GetData (CASHTYPEPOINTER cashData);

类型定义:

typedef CASHTYPE* CASHTYPEPOINTER;

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

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

我的 C# 方法签名和数据类型如何?

谢谢。

I have a C api and I am using p/invoke to call a function from the api in my C# application. The function signature is:

int APIENTRY GetData (CASHTYPEPOINTER cashData);

Type definitions:

typedef CASHTYPE* CASHTYPEPOINTER;

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

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

How would be my C# method signature and data types?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2024-08-13 12:18:50

您需要使用 SizeConst< 指定数组大小/a>:

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);
}

You need to specify the array sizes using SizeConst:

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);
}
老娘不死你永远是小三 2024-08-13 12:18:50

我想它可能看起来像这样

      using System.Runtime.InteropServices;

        [StructLayout(LayoutKind.Sequential)]
        public struct CASH{
            public int CashNumber; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
            public CURRENCY Types[24];
        }   

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

        class Wrapper {
            [DllImport("my.dll")]
            public static extern int GetData(ref CASH cashData}
        }

I think it may look like this

      using System.Runtime.InteropServices;

        [StructLayout(LayoutKind.Sequential)]
        public struct CASH{
            public int CashNumber; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
            public CURRENCY Types[24];
        }   

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

        class Wrapper {
            [DllImport("my.dll")]
            public static extern int GetData(ref CASH cashData}
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文