Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

.net 4.0 - Getting AccessViolation Exception when returning a bool from C++ to C#

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...