用于 C 结构和函数的 C# 编码

标签 c# marshalling

我有以下 C 头文件/代码示例:


头文件


struct category_info {
 int id;
 const char *name;
 const char *description;
};


DLLEXPORT
void* xyz_categories_info(struct category_info **info, size_t *info_count);

示例 C 代码段


struct category_info *catinfo;

size_t catcount;
size_t i;
int max_name_len = 0;
void *catmem = xyz_categories_info(&catinfo, &catcount)


我想将其转换为 C#...

我的第一个猜测(也是一个猜测)是:

[StructLayout(LayoutKind.Sequential)]
    public struct category_info
    {
        int id;
        [MarshalAs(UnmanagedType.LPStr)]
        StringBuilder name;
        [MarshalAs(UnmanagedType.LPStr)]
        StringBuilder description;
    };

[DllImport("mydll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr xyz_categories_info([Out]category_info cat, [Out]int catSize);

但它看起来不对..

任何建议.. 一旦在 C# 中正确声明了上述内容.. 应该如何在 C# 中访问它

category_info catinfo;

catmem = xyz_categories_info(out catinfo, out catcount);

??????

非常感谢任何帮助。

谢谢

============================================= =================================

更新2

xyz_categories_info 中分配的内存使用此 C 调用释放:

void xyz_categories_info_free(void *p);

下面是一个在 C 中使用它的例子....希望这能解释得更多一点..

    category_buffer = xyz_categories_info(&category_info, &category_count);

if( !category_buffer ) 
    {
    // Failed Log a message and exit.
    exit(1);
}

for(j=0; j<category_count; j++) 
    {
    if( category_info[j].id == 0 )
        continue;

    printf("id: %d name: '%s' description: '%s'\n",
        category_info[j].id,
        category_info[j].name,
        category_info[j].description
    );
}

xyz_categories_info_free(category_buffer);

最佳答案

此代码已编译,但未经过测试。如果您了解 C,就会明白这里发生了什么,这只是将相同的 C 代码转换为 C#。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public struct category_info 
    { 
        public int id;
        public IntPtr name;
        public IntPtr description;
    }; 

    class Program
    {
        [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr xyz_categories_info(ref IntPtr cat, ref int catSize);

        [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void xyz_categories_info_free(IntPtr cat); 

        static void Main(string[] args)
        {
            IntPtr categories = IntPtr.Zero;
            IntPtr category_buffer = IntPtr.Zero;
            int category_count = 0;
            category_info info = new category_info();
            IntPtr current;

            try
            {
                category_buffer = xyz_categories_info(ref categories, ref category_count);

                if (category_buffer == IntPtr.Zero)
                {
                    return;
                }

                if (category_count == 0)
                {
                    return;
                }

                for (int j = 0; j < category_count; j++)
                {
                    if (IntPtr.Size == 4)
                    {
                        current = new IntPtr(categories.ToInt32() + j * Marshal.SizeOf(info));
                    }
                    else
                    {
                        current = new IntPtr(categories.ToInt64() + j * Marshal.SizeOf(info));
                    }

                    info = (category_info)Marshal.PtrToStructure(current, typeof(category_info));

                    if (info.id == 0)
                    {
                        continue;
                    }

                    Console.WriteLine(info.id);
                    Console.WriteLine(Marshal.PtrToStringAnsi(info.name));
                    Console.WriteLine(Marshal.PtrToStringAnsi(info.description));
                }
            }
            finally
            {
                if (category_buffer != IntPtr.Zero)
                {
                    xyz_categories_info_free(category_buffer);
                }
            }
        }
    }
}

关于用于 C 结构和函数的 C# 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4204019/

相关文章:

visual-studio-2008 - marshal_as、字符串和字段与属性

c# - 如何将 byte[] 转换为 struct(包含一个 byte[] 成员和 length 成员)

java - 解码parcelables的问题

c# - 根据 ViewModel 类型为 ItemsControl 选择 DataTemplate

c# - 绑定(bind)到只读 ViewModel 属性并在更改时更新 UI

c# - 如何在 c# 中的打包记录中读取固定大小字符串的 Delphi 数组

java - JAXB 编码器和输出 XML 格式

c# - 如何在(字符串、标志列表、带标志的枚举)之间转换枚举?

c# - 插入表时出错

c# - 使用 houghline 变换的直线斜率 c# Aforge.Net