0

Mono Embed サンプルに基づいて構築し、構造を更新する C# アセンブリ内のメソッドを呼び出してみました。構造体には 1 つの int 配列があります。これは Linux システム上にあります。

C# で int 配列フィールドにアクセスすると、セグメンテーション違反が発生します。フィールドが null かどうかを確認するだけで、エラーが発生します。

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

3 に答える 3

1

おっとっと!私の無知!

マーシャリングに関する私の理解が間違っていたようです。生の配列ベースのデータ型 (string、long[]) は直接マーシャリングできません。c 構造体は、ランタイムが正しくマーシャリングするためのメンバーとして Monoxxx* 型を持っている必要があります。

char StringValue1[31] の代わりに MonoString* StringValue1 を使用し、int IntArray[2] の代わりに MonoArray* IntArray を使用すると、マーシャリングが正しく機能します。

構造内のすべての「モノ」バゲージなしで、c から生の構造を渡す必要があった最終的な結果は 次のとおりです。既存の c 構造を変更せずに使用しようとしています。これは、「安全でない」C# コードを使用し、構造自体のアドレスを C# メソッドに渡すことで実現できました。これにより、生のメモリを c# 内で操作できるようになり、c# マーシャラーがバイトを構造体に、またはその逆に変換する完全な自由が与えられます。

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;
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コード

#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;
}
于 2013-10-22T12:34:38.337 に答える
0

mono_runtime_invoke() は型のマーシャリングを行いません (逆に内部呼び出しを使用する場合も同様です)。

P/Invoke メソッドのみがデータ マーシャリングを実行します。

于 2013-10-22T12:46:16.137 に答える