将布尔值从 C++ 返回到 C# 时出现 AccessViolation 异常

编程入门 行业动态 更新时间:2024-10-26 21:22:57
本文介绍了将布尔值从 C++ 返回到 C# 时出现 AccessViolation 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用第三方专有 DLL,我无法使用该 DLL 的源代码.然而,似乎是使用 SWIG 1.3.39 自动生成的包装器代码对我来说是可用的.包装器代码由一个 C++ 文件组成,该文件将(使用一些描述 DLL 的头文件)编译为一个 DLL,以及一个 C# 项目,该项目对 C++ 包装器 DLL 进行 PInvoke 调用.

I am using a third-party, proprietary DLL for which the source code is not available to me. Wrapper code that appears to have been auto-generated using SWIG 1.3.39 is, however, available to me. The wrapper code consists of a C++ file that compiles (using some headers that describe the DLL) to a DLL and of a C# project that makes PInvoke calls to the C++ wrapper DLL.

在检查 StackTrace 后,我得到了以下信息:

After inspecting the StackTrace I got the following information:

at org.doubango.tinyWRAP.tinyWRAPPINVOKE.MediaSessionMgr_consumerSetInt64(HandleRef jarg1, Int32 jarg2, String jarg3, Int64 jarg4) at Deskcon_ABL.NotificationHandler.sipService_onInviteEvent(Object sender, InviteEventArgs e) at BogheCore.Events.EventHandlerTrigger.TriggerEvent[T](EventHandler`1 handler, Object source, T args) at BogheCore.Services.Impl.SipService.MySipCallback.OnDialogEvent(DialogEvent e) at org.doubango.tinyWRAP.SipCallback.SwigDirectorOnDialogEvent(IntPtr e)

这里是有问题的 C# 代码:

So here is the offending C# code:

//in the C# Wrapper public bool consumerSetInt64(twrap_media_type_t media, string key, long value) { bool ret = tinyWRAPPINVOKE.MediaSessionMgr_consumerSetInt64(swigCPtr, (int)media, key, value); return ret; } //In tinyWRAPPINVOKE Class in another file in the C# wrapper: [DllImport("tinyWRAP", EntryPoint="CSharp_MediaSessionMgr_consumerSetInt64")] public static extern bool MediaSessionMgr_consumerSetInt64(HandleRef jarg1, int jarg2, string jarg3, long jarg4);

以及来自 C++ 包装器的 C++ 代码:

And the C++ code from the C++ wrapper :

SWIGEXPORT unsigned int SWIGSTDCALL CSharp_MediaSessionMgr_consumerSetInt64(void * jarg1, int jarg2, char * jarg3, long long jarg4) { unsigned int jresult ; MediaSessionMgr *arg1 = (MediaSessionMgr *) 0 ; twrap_media_type_t arg2 ; char *arg3 = (char *) 0 ; int64_t arg4 ; bool result; arg1 = (MediaSessionMgr *)jarg1; arg2 = (twrap_media_type_t)jarg2; arg3 = (char *)jarg3; arg4 = (int64_t)jarg4; result = (bool)(arg1)->consumerSetInt64(arg2,(char const *)arg3,arg4); jresult = result; return jresult; }

推荐答案

它可能是 DllImport 中的第一个 (void *) 或第三个 (char *) 参数.你能展示你正在创建的代码,并为这两者分配你传入的内容吗?

Odds are it's either the first (void *) or third (char *) parameters in the DllImport. Could you show the code where you're creating and assigning what you're passing in for both of those?

您可以尝试将一个或两个的编组更改为如下所示:

You could try changing the marshalling of one or both, perhaps to something like the following:

[DllImport("tinyWRAP", EntryPoint="CSharp_MediaSessionMgr_consumerSetInt64")] public static extern bool MediaSessionMgr_consumerSetInt64(IntPtr jarg1, int jarg2, StringBuilder jarg3, long jarg4);

但是,如果您对每个参数的用途有更多的了解,也可能有助于识别问题.

But if you had more information on what each of those parameters is used for that might help identify the problem too.

更多推荐

将布尔值从 C++ 返回到 C# 时出现 AccessViolation 异常

本文发布于:2023-11-11 16:54:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579024.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   布尔值   AccessViolation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!