方法的类型签名与PInvoke不兼容 [英] Method's type signature is not PInvoke compatible

查看:460
本文介绍了方法的类型签名与PInvoke不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C#应用程序中使用c dll中的函数,每次尝试运行应用程序并调用有问题的函数时,都会遇到此错误.
起初我以为这可能是因为我使用了错误的签名,但我试图使其尽可能简单但没有运气. 简而言之:
这是我对c dll的实际源代码:

I am trying to use a function from a c dll, in a c# application, I am facing this error each time i try to run the application and call the function in question.
At first i thought maybe this is because of the wrong signature i am using, but i tried to make it as simple as possible yet no luck.!
To cut a long story short:
This is my actual source code for c dll :

#include <stdio.h>
#include <string.h>
extern "C"
{
    struct teststruct
    {
        char acharacter;
        int  anumber;
        char* astring;
        char  anarray[10];
        const char* conststring;
    };

    __declspec(dllexport) teststruct  TestDLL()
    {
        teststruct stc;
        stc.acharacter = 'A';
        stc.anumber = 10;
        strcpy(stc.anarray, "Test");
        stc.astring = "astring!";
        stc.conststring = "Crash?";

        return stc;
    }
}

这是c#计数器部分:

    [StructLayout(LayoutKind.Sequential)]
public struct teststruct
{
    public char acharacter;
    public  int anumber;
    [MarshalAs(UnmanagedType.LPStr)]
    public string astring;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public char[] anarray;
    [MarshalAs(UnmanagedType.LPStr)]
    public string conststring;
}

namespace Tcp_connections_list
{

    public partial class Form1 : Form
    {

        [DllImport("simple c dll.dll",CallingConvention= CallingConvention.Cdecl)]
        public static extern teststruct TestDLL();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnTestDll_Click(object sender, EventArgs e)
        {
            teststruct test = TestDLL(); //Crash at the very begining!
            textBox1.Text = test.acharacter.ToString();
            textBox1.Text = test.anarray.ToString();
            textBox1.Text = test.anumber.ToString();
            textBox1.Text = test.astring.ToString();
            textBox1.Text = test.conststring.ToString(); 


        }

    }
}

以下代码段给了我相同的确切错误,我将结构更改为

The following code snippet gives me the same exact error, I changed the structure to

struct teststruct
{
    char acharacter;
    int  anumber;
};

及其等同于C#的C#

and its C# equivalent to

[StructLayout(LayoutKind.Sequential)]
public struct teststruct
{
    public char acharacter;
    public  int anumber;
}

使它尽可能简单,但再次出现相同的确切错误!
我在这里想念什么?

to make it as simple as possible, Yet again i get the same exact error!
What am i missing here?

推荐答案

问题是以null结尾的C字符串的编组.您不能期望p/invoke marshaller处理函数返回值中的那些.该结构需要这样声明:

The problem is the marshalling of the null-terminated C strings. You cannot expect the p/invoke marshaller to deal with those in a function return value. The struct needs to be declared like this:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct teststruct
{
    public byte acharacter; // don't use C# char which is 2 bytes wide
    public int anumber; 
    public IntPtr astring;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public char[] anarray;
    public IntPtr conststring;
}

您需要使用Marshal.PtrToStringAnsi提取C字符串的内容.

You'll need to use Marshal.PtrToStringAnsi to extract the contents of the C strings.

作为更一般的建议,将大型结构作为函数返回值传递是一个坏主意.没有这样做的公认的ABI.不同的编译器以不同的方式处理此问题.更好的方法是在调用代码中分配该结构,然后传递其地址.像这样:

As a more general piece of advice, passing large structs as function return values is a bad idea. There is no single generally accepted ABI for doing that. Different compilers handle this in different ways. Better is to allocate the struct in the calling code, and pass its address. Like this:

__declspec(dllexport) void TestDLL(teststruct *ts)
{
    ts->... = ...;
    ...
}

在C#端:

[DllImport(...)]
public static extern void TestDLL(out teststruct ts);

实际上,如果您尝试使用的结构不能整理为返回值,这也不会令我感到惊讶.您确实应该将其作为out参数传递.

In fact, it would not surprise me if the struct that you are trying to work with cannot be marshalled as a return value. You really should pass it as an out parameter.

这篇关于方法的类型签名与PInvoke不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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