开发者

When calling DLLs from C#, why would small structs misalign function parameters?

I'm writing a c# / c++ application I've ran into problems when I tried passing a structure containing only two floats across. For example:

[DllImport("Resources\\CppInterface", EntryPoint = "?ReadDllTest@ScriptParserInterface@@YA?AVDllTest@@PAVScriptParser@@PB_W@Z", CharSet = CharSet.Unicode)]
private static extern DllTest ReadDllTestS(IntPtr scriptParser, string name);

Works perfectly fine when DLLTest contains 3 or 4 floats. However, when it contains 2, the intptr and the string pointer passed through ends up 1 byte misaligned on the C++ side.

Any idea what could cause this?

Example struct layout:

[StructLayout( LayoutKind.Sequential )]
public struct DllTest
{
    public float a, b;/*, c, d; (works if c or/d are in)*/

    DllTest( float i, float j )
    {
        a = i;
        b = j;
    }
}

C++ side:

DllTest ScriptParserInterface::ReadDllTest( ScriptParser* scriptParser, const wchar_t* name )
{
     return DllTest(); /* If only using two variables in DLLTest. scriptParser and name no longer work, but are located开发者_JAVA技巧 at *((&scriptParser)-1) and *((&name)-1)
}

Any suggestions would be very much appreciated. Thanks.


ScriptParserInterface must be a namespace name, you'd never make it work if it is a class name. The function is __cdecl according to the mangled name, you forgot to use the CallingConvention property in your [DllImport] declaration. You should have gotten a PInvokeStackImbalance MDA warning. Since you didn't, I have to assume you run this as 64-bit code.

Forgetting CallingConvention in itself could be enough to throw off the stack. Start there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜