C#调用C函数,一个固定大小字符数组返回结构 [英] C# calling C function that returns struct with fixed size char array

查看:277
本文介绍了C#调用C函数,一个固定大小字符数组返回结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,出现了这个问题的许多变种,看着那几个后,我仍然无法弄清楚



这是C代码:

  typedef结构
{
unsigned long类型标识符;
CHAR名称[128];
}框架;

帧的getFrame(INT指数);

这是C#代码:

 结构框架
{
公共ULONG标识符;
[的MarshalAs(UnmanagedType.ByValArray,ArraySubType = UnmanagedType.I1,SizeConst = 128)]
公众的char []名称;
}

函数[DllImport(XNETDB.dll,CallingConvention = CallingConvention.Cdecl,字符集= CharSet.Ansi)]
私人静态外部框架的getFrame(INT指数);

这是我在C#中尝试了最后一次尝试,它似乎很合乎逻辑,但我得到的错误方法的签名不兼容的PInvoke。所以,我有点失去了什么尝试下一个。任何帮助表示赞赏。



谢谢,
凯文



更新凯文加入这个作为编辑我的答案



我应该将改变我的C代码:

 无效的getFrame(INT指数,帧* F); 

和使用,而不是为C#:

 结构框架
{
公共UINT标识符;
[MarshalAsAttribute(UnmanagedType.ByValTStr,SizeConst = 128)]
公共字符串名称;
}

函数[DllImport(XNETDB.dll,CallingConvention = CallingConvention.Cdecl,字符集= CharSet.Ansi)]
私人静态外部无效的getFrame(INT指数,参考帧F);


解决方案

的问题是,本机函数返回非blittable类型作为返回值。



http://msdn.microsoft.com/en-us/library/ef4c3t39.aspx



的p / Invoke不能有非-blittable类型作为返回值。



您不能p /调用该方法。 [修改 这实际上是可能的,看的 JaredPar的答案 的]



按值返回132个字节是一个坏理念。如果本地代码是你,我会解决它。您可以通过分配132个字节,并返回一个指针修复它。然后添加一个FreeFrame方法来释放内存。现在它可以是p /调用



或者,也可以改变它接受一个指针帧存储器,这将在填


So, there have been many variants of this question, and after looking at several I still can't figure it out.

This is the C code:

typedef struct
{
unsigned long Identifier;
char Name[128];
} Frame;

Frame GetFrame(int index);

This is the C# code:

struct Frame
{
    public ulong Identifier;
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 128)]
    public char[] Name;
}

[DllImport("XNETDB.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern Frame GetFrame(int index);

This is the last attempt I tried in C#, and it seems pretty logical, but I get the error "Method's signature is not PInvoke compatible." So, I'm kind of lost on what to try next. Any help is appreciated.

Thanks, Kevin

Updated Kevin added this as an edit to my answer

I should instead change my C code:

void GetFrame(int index, Frame * f);

and use instead for C#:

struct Frame
{
    public uint Identifier;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string Name;
}

[DllImport("XNETDB.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern void GetFrame(int index, ref Frame f);

解决方案

The problem is that the native function returns a non-blittable type as a return value.

http://msdn.microsoft.com/en-us/library/ef4c3t39.aspx

P/Invoke cannot have non-blittable types as a return value.

You cannot p/Invoke that method. [EDIT It is actually possible, see JaredPar's answer]

Returning 132 bytes by value is a bad idea. If this native code is yours, I would fix it. You can fix it by allocating the 132 bytes and returning a pointer. Then add a FreeFrame method to release that memory. Now it can be p/Invoked.

Alternately, you could change it to accept a pointer to the Frame memory that it will fill in.

这篇关于C#调用C函数,一个固定大小字符数组返回结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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