我想在C#項目中使用C DLL。PInvoke具有不平衡的堆棧
我在C函數:
extern __declspec(dllexport) void InitBoard(sPiece board[8][8]);
的sPiece結構:
typedef struct Piece
{
ePieceType PieceType; //enum
ePlayer Player; //enum
int IsFirstMove;
} sPiece;
我的PInvoke在C#:
[DllImport("chess_api.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
static extern void InitBoard([MarshalAs(UnmanagedType.LPArray, SizeConst = 64)]ref sPiece[] board);
在C#中的sPiece結構:
[StructLayout(LayoutKind.Sequential)]
public struct sPiece
{
public ePieceType PieceType;
public ePlayer Player;
public int IsFirstMove;
}
當我運行的PInvoke我得到以下錯誤:
A call to PInvoke function 'Chess!Chess.Main::InitBoard' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
我嘗試調用約定更改爲Cdecl
,但是當我運行它時,VS卡住。
我該怎麼辦?
@Mark。我看到了這個答案,並沒有幫助,因爲我試圖改變PInvoke就像在那裏說的,它卡住了我的VS. – 2014-09-20 18:01:55
InitBoard需要一個指針,但是你給它一個指針指針,因爲具有引用類型的ref是雙重間接。這是你會發現的下一個錯誤。 – usr 2014-09-20 21:11:51