获取AccessViolation异常从C ++返回一个布尔值时,C# [英] Getting AccessViolation Exception when returning a bool from C++ to C#

查看:629
本文介绍了获取AccessViolation异常从C ++返回一个布尔值时,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是第三方的,专有的DLL,它的源$ C ​​$ c不是提供给我。出现包装code一直自动生成使用痛饮39年3月1日,但是,提供给我。该包装code由C ++文件编译(使用一些标题,描述了DLL)的C#项目,使的PInvoke调用C ++的包装DLL的DLL和。

检查堆栈跟踪后,我得到了以下信息:

 在org.doubango.tinyWRAP.tinyWRAPPINVOKE.MediaSessionMgr_consumerSetInt64(HandleRef jarg1,的Int32 jarg2,字符串jarg3,Int64的jarg4)
在Deskcon_ABL.NotificationHandler.sipService_onInviteEvent(对象发件人,InviteEventArgs E)
在BogheCore.Events.EventHandlerTrigger.TriggerEvent [T](EventHandler`1处理程序,对象源,T参数)
在BogheCore.Services.Impl.SipService.MySipCallback.OnDialogEvent(DialogEvent E)
在org.doubango.tinyWRAP.SipCallback.SwigDirectorOnDialogEvent(IntPtr的E)
 

因此​​,这里是有问题的C#code:

  //在C#包装

    公共BOOL consumerSetInt64(twrap_media_type_t媒体,串键,长值){
    布尔RET = tinyWRAPPINVOKE.MediaSessionMgr_consumerSetInt64(swigCPtr,(INT)媒体键,值);
    返回RET;
  }

//在tinyWRAPPINVOKE类在C#中封装另一个文件:

  [的DllImport(tinyWRAP,入口点=CSharp_MediaSessionMgr_consumerSetInt64)]
  公共静态的extern BOOL MediaSessionMgr_consumerSetInt64(HandleRef jarg1,INT jarg2,串jarg3,长jarg4);
 

和从C ++封装的C ++ code:

  SWIGEXPORT unsigned int类型SWIGSTDCALL CSharp_MediaSessionMgr_consumerSetInt64(无效* jarg1,INT jarg2,字符* jarg3,长长jarg4){
  无符号整型jresult;
  MediaSessionMgr * ARG1 =(MediaSessionMgr *)0;
  twrap_media_type_t ARG2;
  字符*参数3 =(字符*)0;
  ARG4的int64_t;
  布尔结果;

  ARG1 =(MediaSessionMgr *)jarg1;
  ARG2 =(twrap_media_type_t)jarg2;
  参数3 =(字符*)jarg3;
  ARG4 =(的int64_t)jarg4;
  结果=(布尔)(ARG1) - > consumerSetInt64(ARG2,(字符常量*)ARG3,ARG4);
  jresult =结果;
  返回jresult;
}
 

解决方案

奇怪的是它要么第一(无效*)或第三(字符*)的参数中的DllImport。 你能告诉了code,你正在创造和分配你传递什么在为那些?

您可以尝试改变一个或两个编组,也许是类似以下内容:

  [的DllImport(tinyWRAP,入口点=CSharp_MediaSessionMgr_consumerSetInt64)
公共静态的extern BOOL MediaSessionMgr_consumerSetInt64(IntPtr的jarg1,INT jarg2,StringBuilder的jarg3,长jarg4);
 

但是,如果你有什么每个这些参数用于更多的信息也会有所帮助确定问题所在。

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.

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) 

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);

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;
}

解决方案

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.

这篇关于获取AccessViolation异常从C ++返回一个布尔值时,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆