【发布时间】:2011-07-19 23:53:25
【问题描述】:
我正在编写一个 c#/c++ 应用程序,当我尝试传递一个仅包含两个浮点数的结构时遇到了问题。例如:
[DllImport("Resources\\CppInterface", EntryPoint = "?ReadDllTest@ScriptParserInterface@@YA?AVDllTest@@PAVScriptParser@@PB_W@Z", CharSet = CharSet.Unicode)]
private static extern DllTest ReadDllTestS(IntPtr scriptParser, string name);
当 DLLTest 包含 3 或 4 个浮点数时工作得很好。但是,当它包含 2 时,intptr 和传递的字符串指针在 C++ 端会出现 1 个字节未对齐。
知道是什么原因造成的吗?
示例结构布局:
[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++ 方面:
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 at *((&scriptParser)-1) and *((&name)-1)
}
任何建议将不胜感激。谢谢。
【问题讨论】:
标签: c# c++ string marshalling