【问题标题】:converting c++ API struct to c#将 c++ API 结构转换为 c#
【发布时间】:2020-04-16 07:45:54
【问题描述】:

我正在尝试将 c++ api 示例转换为 c#。但是由于struct具有字符串类型,我无法在struct中找到struct指针的方式。这里是 c++ 结构和函数

struct AuthParam { char server_ip[32];  char username[50];  char password[50]; };
struct CameraInfo { int index;  char devicename[100];  char smallrtsp[1000];  char bigrtsp[1000]; };
struct SingleDevice { char deviceid[50];  char devicename[100];  int  flag_onuse;  int  cameralist_size;  CameraInfo* cameralist; };
struct DeviceList { int listsize;  SingleDevice* singledevicelist; };

typedef int (WINAPI capi_init)(void);
typedef int (WINAPI capi_disabled)(void);
typedef int (WINAPI capi_GetServerTimeCode)(char* server_ip, unsigned int* timecode);
typedef int (WINAPI GetDevicelist)(AuthParam auth_para, DeviceList* devicelist);

这是我的 c# 代码,但我找不到在我得到的结构中定义结构指针的方法“ 错误 CS0208 无法获取托管类型的地址、大小或声明指向托管类型 ('Form1.CameraInfo') 的指针”错误。

public struct AuthParam
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string server_ip;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string username;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string password;
        };

        [StructLayout(LayoutKind.Sequential)]
        public struct CameraInfo
        {
            public int index;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
            public string devicename;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1000)]
            public string smallrtsp;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1000)]
            public string bigrtsp;
        };

        [StructLayout(LayoutKind.Sequential)]
        public struct SingleDevice
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
            public string deviceid;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
            public string devicename;
            public int flag_onuse;
            public int cameralist_size;

            public CameraInfo *cameralist;
        };

        [StructLayout(LayoutKind.Sequential)]
        public unsafe struct DeviceList
        {
            public int listsize;
            public SingleDevice *singledevicelist;
        };

[DllImport("c:\\lib\\api_client.dll")] public static extern int capi_init();
        [DllImport("c:\\lib\\api_client.dll")] public static extern int capi_disabled();
        [DllImport("c:\\lib\\api_client.dll")] public static extern int capi_GetServerTimeCode(string ip, ref uint timecode);
        [DllImport("c:\\lib\\api_client.dll")] public static extern int GetDevicelist (AuthParam auth_para,ref  DeviceList devicelist);

有没有办法实现这种转换?

【问题讨论】:

  • 也许那行应该是public ref SingleDevice singledevice?或者您可以使用IntPtr 作为指针。

标签: c# c++ pointers struct


【解决方案1】:

我认为这个错误是因为 c++(非托管代码)和 c#(托管代码)的不同性质,所以可能使用 System.IntPtr 作为指向相机结构的指针。

请考虑这个问题here,您可以找到关于P/Invoke Interop Assistant 的信息,它是一个开源工具,可以将您的代码从非托管代码转换为托管C# 代码。有一篇关于C++/C# interoperability的小博客文章

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多