Nested struct unmarshalling

。_饼干妹妹 提交于 2019-12-11 20:21:23

问题


There are two unmanaged structs

typedef struct multipolynomial
{
    int N;
    int max_power;
    double* X;
    double** Y;
} multipolynomial;

typedef struct output
{
    double d;
    multipolynomial mp;
} output;

and corresponding managed analogs

[StructLayoutAttribute(LayoutKind.Sequential)]
public unsafe class Multipolynomial
{
    public int n;
    public int max_power;
    public double* X;
    public double** Y;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public unsafe struct Output
{
    public double d;
    public Multipolynomial mp;
}

And there is native function

__declspec(dllexport) output __cdecl foo()
{
    output out;
    out.t = 1;
    return out;
}

with managed signature

[DllImport("kernel.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern Output foo();

that crashes here

Output output = MathKernel.foo();

with explanation "Method's type signature is not PInvoke compatible."

Please point out whats going wrong?

PS: please note that managed analogue for Multipolynomial struct is class


回答1:


MSDN: P/Invoke cannot have non-blittable types as a return value. That's why you are getting the error you are getting. Also, your managed definitions don't match your unmanaged definitions. The unmanaged output contains multipolynomial by value, but your managed equivalent contains it by reference (besides, object references are not blittable). The managed Multipolynomial must be a struct, and you must specify [MarshalAs(UnmanagedType.Struct)] on the mp field — see MarshalAs nested structure. In addition, I am unsure whether unsafe pointers are blittable. Replace them with IntPtrs while you are testing this out, then put the pointers back in.



来源:https://stackoverflow.com/questions/19200615/nested-struct-unmarshalling

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