gpt4 book ai didi

C# - 方法的类型签名与 PInvoke 不兼容。使用 MarshalDirectiveException

转载 作者:行者123 更新时间:2023-11-30 03:36:35 25 4
gpt4 key购买 nike

当我运行 GetBoard 方法时出现以下异常(但方法 Initialize 工作正常):

System.Runtime.InteropServices.MarshalDirectiveException was unhandled
HResult=-2146233035
Message=Method's type signature is not PInvoke compatible.
Source=MatinChess.Net
StackTrace:
at MatinChess.Net.ExternMethods.GetBoard()
at MatinChess.Net.MatinChess.GetBoard() in C:\Users\Matin\Documents\GitHub\MatinChessDLL\dotnet\MatinChess.Net\MatinChess.cs:line 12
at MatinChess.Net.Demo.Program.PrintBoard(MatinChess chess) in C:\Users\Matin\Documents\GitHub\MatinChessDLL\dotnet\MatinChess.Net.Demo\Program.cs:line 53
at MatinChess.Net.Demo.Program.Main(String[] args) in C:\Users\Matin\Documents\GitHub\MatinChessDLL\dotnet\MatinChess.Net.Demo\Program.cs:line 14
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

我编写了以下基于 x86 设置的 C# 结构:

[DllImport("MatinChess.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public extern static void Initialize();

[DllImport("MatinChess.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public extern static ChessBoard GetBoard();

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ChessBoard
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
byte[] board;

public char this[int x, int y]
{
get
{
return (char)board[y * 8 + x];
}
}
}

这是我的 C++ 结构,由 MSVC2015 32 位编译:

struct ChessBoard
{
char board[8][8];
};

最佳答案

我必须自己编写代码并进行测试,这样我才能确定。

好的。我编写了代码并对其进行了测试。

因为它是 C# 代码中的一个结构,您无法从 native C 代码中获取它,所以您必须通过 GC 分配它,然后通过 API 填充它,或者您只是在 native 代码中获取一个指向结构的指针并将其编码到你在 C# 中的结构

第一个:

C 代码:

    __declspec(dllexport) void __cdecl GetBoard(ChessBoard& chess);

C#代码:

[DllImport("testDll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void GetBoard2(ref ChessBoard ptr);

public static ChessBoard GetChessBoard()
{
ChessBoard chess = new ChessBoard();
GetBoard2(ref chess);
return chess;
}

第二种方法:

C 代码:

__declspec(dllexport) ChessBoard* __cdecl GetBoard();

__declspec(dllexport) void __cdecl FreeMemory(void *);

C#代码:

[DllImport("testDll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr GetBoard();

[DllImport("testDll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void FreeMemory(IntPtr ptr);


public static ChessBoard GetChessBoard()
{
var boradPtr = GetBoard();
var chessBoard = (ChessBoard)Marshal.PtrToStructure(boradPtr, typeof(ChessBoard));

FreeMemory(boradPtr);
return chessBoard;
}

关于C# - 方法的类型签名与 PInvoke 不兼容。使用 MarshalDirectiveException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40595298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com