Method's type signature is not PInvoke compatible

若如初见. 提交于 2019-12-23 03:17:16

问题


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;
    }
}

And this is the c# counter part:

    [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;
};

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?


回答1:


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;
}

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

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->... = ...;
    ...
}

And on the C# side:

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

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.



来源:https://stackoverflow.com/questions/22803083/methods-type-signature-is-not-pinvoke-compatible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!