2

platform / invokeを使用していて、floatLPSTRとintをc++関数にマーシャリングしようとすると、次のエラーが発生します。PInvoke関数'Game!Game.Graphics::CreateModel'を呼び出すとスタックのバランスが崩れます。これは、マネージドPInvokeシグニチャがアンマネージドターゲットシグニチャと一致しないことが原因である可能性があります。PInvokeシグニチャの呼び出し規約とパラメータがターゲットのアンマネージドシグニチャと一致することを確認してください。これが私のc#コードです:

public struct Graphics
        {
        [DllImport(@"Graphics.dll", EntryPoint = "StartGL")]
        public static extern void StartGL();
        [DllImport(@"Graphics.dll", EntryPoint = "CreateModel")]
        public static extern void CreateModel([MarshalAs(UnmanagedType.LPStr)]string ModelPath, [MarshalAs(UnmanagedType.LPStr)]string TexturePath,float xposh, float yposh, float zposh, float rotAngleh, float xroth, float yroth, float zroth);
        [DllImport(@"Graphics.dll", EntryPoint = "rotateModel")]
        public static extern void rotateModel(int id,float rotAngle,float x, float y, float z);
        }
    class Program
    {
        static void Main(string[] args) 
        {
            OpenGL();
        }
        static void OpenGL()
        {
            Graphics.CreateModel("box.obj","asd.png",0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
            Graphics.rotateModel(0,1.5707963267948966192313216916398f, 1.0f, 0.0f, 0.0f);
            Graphics.StartGL();
            //end of program
        }

およびc++関数の宣言:

extern "C"  __declspec(dllexport)void StartGL();
extern "C"  __declspec(dllexport)void CreateModel(LPSTR ModelPath,LPSTR TexturePath,float xposh,float yposh,float zposh,float rotAngleh,float xroth,float yroth,float zroth)
{
    m3Dobject mod = m3Dobject(ModelPath,TexturePath,xposh,yposh,zposh,rotAngleh,xroth,yroth,zroth);
    Models.push_back(mod);
}
    extern "C"  __declspec(dllexport)void moveModel(int id,float x,float y,float z)
{
    Models[id].xpos = x;
    Models[id].ypos = y;
    Models[id].zpos = z;
}
extern "C"  __declspec(dllexport)void rotateModel(int id,float rotAnglef,float x,float y,float z)
{
    Models[id].rotAngle = rotAnglef;
    Models[id].xrot = x;
    Models[id].yrot = y;
    Models[id].zrot = z;
}

よろしくお願いします。

4

1 に答える 1

6

デフォルトでは、C ++__cdeclは呼び出し規約に使用されますが、C#のデフォルトは__stdcall。です。そのため、PInvoke宣言でこれを指定する必要があります。つまり、次のようになります。

[DllImport(@"Graphics.dll", EntryPoint = "StartGL", CallingConvention=CallingConvention.Cdecl)]
于 2012-10-02T17:33:27.287 に答える