63954

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

Question:

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

Answer1:

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.

Recommend

  • 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a nul
  • Example of using EM_STREAMOUT with c# and RichEditBox
  • Monitor turn on or wake up command in windows 7 SP1 x64
  • Select columns in pandas dataframe by value in rows
  • Pandas remap to range in column
  • BigQuery not dealing with timestamp in millisecond with partition column
  • Azure file Storage SMB slow to list files in directory
  • Multiplying column elements of sparse Matrix
  • How to access profile picture with Facebook API in Swift 3?
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • Filter strings with regex before casting to numeric
  • SAXReader not re-ecape characters
  • Ember.js model to be organised as a tree structure
  • Reading a file into a multidimensional array
  • Read text file that is not in the main package in a runnable jar
  • IE7 and TinyMCE with Plone
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Conversion from string “a” to type 'Boolean' is not valid
  • how to upload multiple files in c# windows application
  • Display java JPanel in a JFrame
  • Appending Character to Character Array In C
  • Unable to decode certificate at client new X509Certificate2()
  • OOP Javascript - Is “get property” method necessary?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Passing 1 of 3 values to a Sub
  • How to test if a URL from an Eclipse bundle is a directory?
  • ActiveRecord query for a count of new users by day
  • Set the selected item in dropdownlist in MVC3
  • How to create a file in java without a extension
  • Spring Data JPA custom method causing PropertyReferenceException
  • Splitting given String into two variables - php
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++