Getting AccessViolation Exception when returning a bool from C++ to C#

て烟熏妆下的殇ゞ 提交于 2019-12-25 18:34:38

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/11030423/getting-accessviolation-exception-when-returning-a-bool-from-c-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!