将布尔值从 C++ 返回到 C# 时出现 AccessViolation 异常 [英] Getting AccessViolation Exception when returning a bool from C++ to C#
问题描述
我正在使用第三方专有 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 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!