调用从C#到C函数接受来电者分配一个结构数组 [英] Calling from C# to C function which accept a struct array allocated by caller

查看:141
本文介绍了调用从C#到C函数接受来电者分配一个结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的C结构



 结构XYZ 
{
void *的一个;
焦炭FN [MAX_FN]
无符号长升;
无符号长O;
};

和我想打电话从C#以下功能:

 的externCINT FUNC(INT处理,为int * numEntries,XYZ * xyzTbl); 



在哪里xyzTbl是由主叫方分配的大小numEntires的XYZ数组



我已经定义了下面的C#结构:

  [StructLayoutAttribute(顺序,字符集=字符集.Ansi)] 
公共结构XYZ
{
公共System.IntPtr RVA;
[MarshalAsAttribute(UnmanagedType.ByValTStr,SizeConst = 128)]
公共字符串FN;
公共UINT升;
公共UINT O;
}

和方法:

 函数[DllImport(@xyzdll.dll,CallingConvention = CallingConvention.Cdecl)] 
公共静态外部的Int32 FUNC(的Int32手柄,裁判的Int32 numntries,
[的MarshalAs(UnmanagedType.LPArray)XYZ [] ARR);



然后我尝试调用该函数:

  XYZ XYZ =新的XYZ [numEntries] 
为(...)XYZ [I] =新XYZ();
FUNC(手柄,numEntries,XYZ);



当然,这是行不通的。有人可以揭示什么我做错了?


解决方案

  [StructLayoutAttribute(顺序,字符集= CharSet.Ansi)] 
公共结构XYZ
{
公共System.IntPtr RVA;
[MarshalAsAttribute(UnmanagedType.ByValTStr,SizeConst = 128)]
公共字符串FN;
公共UINT升;
公共UINT O;
}



应该不是那些 UINT ULONG ? ?此外,MAX_FN为128权

  XYZ XYZ =新的XYZ [numEntries] 
为(...)XYZ [I] =新XYZ();



XYZ是值类型(结构),所以在这里第二行是多余的(结构总是被初始化)

 函数[DllImport(@xyzdll.dll,CallingConvention = CallingConvention.Cdecl)] 
公共静态外部的Int32 FUNC(的Int32手柄,裁判的Int32 numntries,
[的MarshalAs(UnmanagedType.LPArray)XYZ [] ARR);



[的MarshalAs(UnmanagedType.LPArray)] 是多余的,编译器会看到它是一个结构数组。


I have the following C struct

struct XYZ
{
void            *a;
char            fn[MAX_FN];     
unsigned long   l;          
unsigned long   o;  
};

And I want to call the following function from C#:

extern "C"  int     func(int handle, int *numEntries, XYZ *xyzTbl);

Where xyzTbl is an array of XYZ of size numEntires which is allocated by the caller

I have defined the following C# struct:

[StructLayoutAttribute(Sequential, CharSet = CharSet.Ansi)]
public struct XYZ
{
   public System.IntPtr rva;
   [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
   public string fn;
   public uint l;
   public uint o;
}

and a method:

 [DllImport(@"xyzdll.dll", CallingConvention = CallingConvention.Cdecl)]
 public static extern Int32 func(Int32 handle, ref Int32 numntries,
     [MarshalAs(UnmanagedType.LPArray)] XYZ[] arr);

Then I try to call the function :

XYZ xyz = new XYZ[numEntries];
for (...) xyz[i] = new XYZ();
func(handle,numEntries,xyz);

Of course it does not work. Can someone shed light on what I am doing wrong ?

解决方案

[StructLayoutAttribute(Sequential, CharSet = CharSet.Ansi)]
public struct XYZ
{
   public System.IntPtr rva;
   [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
   public string fn;
   public uint l;
   public uint o;
}

Shouldn't those uint be ulong ? Also, MAX_FN is 128 right ?

XYZ xyz = new XYZ[numEntries];
for (...) xyz[i] = new XYZ(); 

XYZ is a value type (struct), so the second line here is redundant (structs are always initialized)

 [DllImport(@"xyzdll.dll", CallingConvention = CallingConvention.Cdecl)]
 public static extern Int32 func(Int32 handle, ref Int32 numntries,
 [MarshalAs(UnmanagedType.LPArray)] XYZ[] arr);

[MarshalAs(UnmanagedType.LPArray)] is redundant, the compiler will see it's a struct array.

这篇关于调用从C#到C函数接受来电者分配一个结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆