在 Linux 嵌入式单声道程序中,对于从 C# 到 C 的结构,数组数据未正确封送
本文关键字:结构 数组 数据 单声道 嵌入式 Linux 声道 程序 | 更新日期: 2023-09-27 18:36:28
我基于单声道嵌入示例进行构建,以尝试在更新结构的 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;
}
}
}
哎呀! 我的无知!
看来我对编组的理解是不正确的。 基于原始数组的数据类型(字符串、long[])不能直接封送。 c 结构必须将 Monoxxx* 类型作为成员,运行时才能正确封送。
使用 MonoString* StringValue1而不是 char StringValue1[31] 和 MonoArray* IntArray 而不是 int IntArray[2] 可以正确封送处理。
这是我最终得到的我真的需要从 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;
}
尝试将StringValue1
作为字符数组传递,因为这是您在 C 程序中实际定义的内容。
mono_runtime_invoke() 不执行任何类型编组(如果您反过来使用内部调用,则相同)。
只有 P/Invoke 方法执行数据封送。