对于Linux嵌入式单声道程序中c#到c的结构,数组数据没有正确编组

时间:2013-10-21 20:15:58

标签: c# linux mono embedded

我在Mono Embed示例的基础上尝试调用更新结构的C#程序集中的方法。该结构有1个int数组。这是在Linux系统上。

访问c#中的int数组字段会导致分段错误。只检查字段是否为空就足以导致错误。

当我在C#中进行内部封送模拟时,将结构转换为字节然后再转换回结构,这可以正常工作。

单声道版本:3.2.3

我已经在下面提供了c#和c代码,如果需要,可以根据要求提供更多信息。

这是c代码......

#include <mono/jit/jit.h>
#include <mono/metadata/object.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <string.h>
#include <stdlib.h>

#ifndef FALSE
#define FALSE 0
#endif

struct STRUCT_Test
{
    int IntValue1[2];
};

int 
main (int argc, char* argv[]) {
    MonoDomain *domain;
    MonoAssembly *assembly; 
    MonoClass *klass;
    MonoObject *obj;
    MonoImage *image;

    const char *file;
    int retval;

    if (argc < 2){
        fprintf (stderr, "Please provide an assembly to load\n");
        return 1;
    }
    file = argv [1];

    domain = mono_jit_init (file);

    assembly = mono_domain_assembly_open(domain, file);
    if (!assembly)
        exit(2);

    image = mono_assembly_get_image(assembly);

    klass = mono_class_from_name(image, "StructTestLib", "StructReader");
    if (!klass) {
        fprintf(stderr, "Can't find StructTestLib in assembly %s\n", mono_image_get_filename(image));
        exit(1);
    }

    obj = mono_object_new(domain, klass);
    mono_runtime_object_init(obj);

    {
        struct STRUCT_Test structRecord; memset(&structRecord, 0, sizeof(struct STRUCT_Test));
        void* args[2];
        int val = 277001;

        MonoMethodDesc* mdesc = mono_method_desc_new(":ReadData", FALSE);
        MonoMethod *method = mono_method_desc_search_in_class(mdesc, klass);

        args[0] = &val;
        args[1] = &structRecord;

        structRecord.IntValue1[0] = 1111;
        structRecord.IntValue1[1] = 2222;

        mono_runtime_invoke(method, obj, args, NULL);

        printf("IntValue1: %d, %d\r\n", structRecord.IntValue1[0], structRecord.IntValue1[1]);
    }


    retval = mono_environment_exitcode_get ();

    mono_jit_cleanup (domain);
    return retval;
}

这是c#代码......

 using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace StructTestLib
{
    [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Ansi)]
    public struct STRUCT_Test
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public Int32[] IntValue1;
    }

    public class StructReader
    {
        public void ReadData(int uniqueId, ref STRUCT_Test tripRecord)
        {
            if (tripRecord.IntValue1 != null)
                Console.WriteLine("IntValue1: " + tripRecord.IntValue1[0] + ", " + tripRecord.IntValue1[1]);
            else
                Console.WriteLine("IntValue1 is NULL");

            tripRecord.IntValue1[0] = 3333;
            tripRecord.IntValue1[1] = 4444;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

糟糕!我的无知!

似乎我对编组的理解不正确。基于原始数组的数据类型(字符串,long [])无法直接封送。 c结构必须具有Monoxxx *类型作为运行时的成员才能正确编组。

使用MonoString * StringValue1而不是char StringValue1 [31]和MonoArray * IntArray而不是int IntArray [2]允许编组正常工作。

以下是我最终的结果 我真的需要传递来自c的原始结构而没有结构内的所有“单声道”行李,我正在尝试使用现有的c结构而不改变它们。我能够通过使用“不安全”的c#代码并允许将结构本身的地址传递给c#方法来实现这一点。这允许在c#中操作原始内存,并为c#编组提供完全的自由,将字节转换为struct,反之亦然。

c#c​​ode

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using EmpireCLS.Comm;

namespace StructTestLib
{
    [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Ansi)]
    public struct STRUCT_Test
    {
        public Int32 IntValue1;
        public Int32 IntValue2;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
        public string StringValue1;

    }

    public class StructReader
    {
        unsafe public void ReadDataRaw(int uniqueId, void* tripRecordPtr)
        {
            STRUCT_Test tripRecord = (STRUCT_Test)Marshal.PtrToStructure((IntPtr)tripRecordPtr, typeof(STRUCT_Test));

            tripRecord.IntValue1 = 3333;
            tripRecord.IntValue2 = 4444;

            Console.WriteLine("c# StringValue1: " + tripRecord.StringValue1);
            tripRecord.StringValue1 = "fghij";

            GCHandle pinnedPacket = new GCHandle();
            try
            {
                int structSizeInBytes = Marshal.SizeOf(typeof(STRUCT_Test));

                byte[] bytes = new byte[structSizeInBytes];
                pinnedPacket = GCHandle.Alloc(bytes, GCHandleType.Pinned);

                Marshal.StructureToPtr(tripRecord, pinnedPacket.AddrOfPinnedObject(), true);
                Marshal.Copy(bytes, 0, (IntPtr)tripRecordPtr, bytes.Length);

            }
            finally
            {
                if (pinnedPacket.IsAllocated)
                    pinnedPacket.Free();
            }
        }
    }
}

c code

#include <mono/jit/jit.h>
#include <mono/metadata/object.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <string.h>
#include <stdlib.h>

#ifndef FALSE
#define FALSE 0
#endif

struct STRUCT_Test
{
    int IntValue1;
    int IntValue2;

    char StringValue1[20];
};

int 
main (int argc, char* argv[]) {
    MonoDomain *domain;
    MonoAssembly *assembly; 
    MonoClass *klass;
    MonoObject *obj;
    MonoImage *image;

    const char *file;
    int retval;

    if (argc < 2){
        fprintf (stderr, "Please provide an assembly to load\n");
        return 1;
    }
    file = argv [1];

    domain = mono_jit_init (file);

    assembly = mono_domain_assembly_open(domain, file);
    if (!assembly)
        exit(2);

    image = mono_assembly_get_image(assembly);

    klass = mono_class_from_name(image, "StructTestLib", "StructReader");
    if (!klass) {
        fprintf(stderr, "Can't find StructTestLib in assembly %s\n", mono_image_get_filename(image));
        exit(1);
    }

    obj = mono_object_new(domain, klass);
    mono_runtime_object_init(obj);

    {
        struct STRUCT_Test structRecord; memset(&structRecord, 0, sizeof(struct STRUCT_Test));
        void* args[2];
        int val = 277001;
        char* p = NULL;

        MonoMethodDesc* mdesc = mono_method_desc_new(":ReadDataRaw", FALSE);
        MonoMethod *method = mono_method_desc_search_in_class(mdesc, klass);

        args[0] = &val;
        args[1] = &structRecord;

        structRecord.IntValue1 = 1111;
        structRecord.IntValue2 = 2222;
        strcpy(structRecord.StringValue1, "abcde");

        mono_runtime_invoke(method, obj, args, NULL);

        printf("C IntValue1: %d, %d\r\n", structRecord.IntValue1, structRecord.IntValue2);
        printf("C StringValue: %s\r\n", structRecord.StringValue1);
    }


    retval = mono_environment_exitcode_get ();

    mono_jit_cleanup (domain);
    return retval;
}

答案 1 :(得分:0)

尝试将StringValue1作为一个字符数组传递,因为这是您在C程序中实际定义的内容。

答案 2 :(得分:0)

mono_runtime_invoke()不进行任何类型的编组操作(如果您反过来并使用内部调用,则相同)。

只有P / Invoke方法执行数据编组。