gpt4 book ai didi

c# - PInvoke 致命执行引擎错误

转载 作者:行者123 更新时间:2023-11-27 23:19:26 28 4
gpt4 key购买 nike

<分区>

我使用 PInvoke 从 C# 代码访问 C++ 库。但是我在使用以下功能时遇到问题:

UINT32 myfunc(IN char * var1, IN UINT32 var2, INOUT UINT16 * var3, OUT UINT8 * var4)

文档说:

return parameter is error code

var1 is a string

var2 is an integer

var3 is a pointer to an integer (the value will be the size of var4, the value will be changed by the function)

var4 is a pointer to a buffer that will be filled by the function

当我使用

[DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int myfunc(string var1, int var2, out int var3,
out byte[] var4);

该函数给出以下异常:

Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'C:\Users...\Documents\Projects...\bin\Debug...vshost.exe'.

Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x74292e44, on thread 0x2edc. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

If there is a handler for this exception, the program may be safely continued.

我尝试使用参数来查看是否可以得到任何东西,然后我按如下方式更改了字符集:

[DllImport("mydll.dll, CallingConvention = CallingConvention.Cdecl", CharSet = CharSet.Unicode)]
public static extern int myfunc(string var1, int var2, out int var3, out byte[] var4);

在这种情况下,它不会给出 FatalExecutionEngineError,而是函数无错误地返回。但是返回值表明函数内部发生了错误。 (我不知道为什么字符集更改会阻止 FatalExecutionEngineError。)

我不确定我做错了什么,请帮助我。

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