gpt4 book ai didi

c# - 将 byte[] 从 c# 传递到纯 c dll 时发生访问冲突

转载 作者:行者123 更新时间:2023-11-30 15:30:40 28 4
gpt4 key购买 nike

我正在尝试将 SOIL 库用于我的 Unity3d 项目。我稍微修改了代码,将其编译为 DLL。我有一个带签名的 C 函数:

__declspec(dllexport)
unsigned int SOIL_load_OGL_texture_from_memory
(
const unsigned char *const buffer,
int buffer_length,
int force_channels,
unsigned int reuse_texture_ID,
unsigned int flags
);

我在我的 C# 脚本中声明了它:

  [DllImport("SOIL", CallingConvention = CallingConvention.Cdecl)]
private static extern uint SOIL_load_OGL_texture_from_memory(
System.IntPtr buffer,
int buffer_length,
int force_channels,
uint reuse_texture_ID,
uint flags);

尝试调用:

GCHandle pinnedArray = GCHandle.Alloc(bytes, GCHandleType.Pinned);
System.IntPtr pointer = pinnedArray.AddrOfPinnedObject();
uint id = SOIL_load_OGL_texture_from_memory(pointer, bytes.Length, 3, 0, 0);
pinnedArray.Free();

并出现访问冲突。所以我尝试将 IntPtr 作为 const unsigned char * 传递。也许我需要使用与 GCHandle 不同的东西?

编辑:它在播放模式下使整个 Unity 3D 崩溃:MSVCR120.dll 处的访问冲突。

最佳答案

尝试Marshal.Copy(Byte[], Int32, IntPtr, Int32)并将指针传递给您的 C 调用

关于c# - 将 byte[] 从 c# 传递到纯 c dll 时发生访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25485496/

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