Simple Interop Test; Stack Unbalanced by a simple call

后端 未结 1 1886
花落未央
花落未央 2021-01-26 19:14

I am currently looking into an interop related issue and have written a small test program in order to help me figure out what is going on (the issue in question involves a call

1条回答
  •  囚心锁ツ
    2021-01-26 19:46

    You need to specify a correct calling convention. Default calling convention for C/C++ programs is cdecl, but default calling convention when importing through the PInvoke is StdCall. So you either need to specify the Cdecl convention when importing or __stdcall when exporting. For example:

    [DllImport("Interop.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void Foo( int i  );
    
    private void Test()
    {
        Foo( 1 );
    }
    

    0 讨论(0)
提交回复
热议问题