[英]array data not marshaled correctly for structures from c# to c within Linux embedded mono program
我以Mono Embed示例為基礎,嘗試並調用C#程序集中的方法來更新結構。 該結構具有1個int數組。 這是在Linux系統上。
在c#中訪問int array字段會導致分段錯誤。 僅檢查該字段是否為空就足以導致故障。
當我在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;
}
}
}
糟糕! 我的無知!
看來我對封送的理解不正確。 基於原始數組的數據類型(string,long [])無法直接封送。 c結構必須具有Monoxxx *類型作為成員,以便運行時正確地封送。
使用MonoString * StringValue1代替char StringValue1 [31]和MonoArray * IntArray代替int IntArray [2]可以使封送處理正常工作。
這就是我最終要得到的結果,我真的需要從c中傳遞原始結構,而不必在結構中包含所有“單”行李,我試圖使用現有的c結構而不更改它們。 通過使用“不安全的” c#代碼並將結構本身的地址傳遞到c#方法中,我能夠做到這一點。 這樣就可以在c#中操縱原始內存,並為c#封送處理程序提供了將字節轉換為struct的完全自由權,反之亦然。
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方法執行數據編組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.