Challenges with itk.simple.SimpleITKPINVOKE+SWIGExceptionHelper in Unity il2cpp Final Build

Good day, esteemed forum members!

I am developing an application on Unity il2cpp and am using the SimpleITK library, compiled through SuperBuild for C#. Everything works perfectly in the Unity editor, but during the final build of the project, I encounter an exception itk.simple.SimpleITKPINVOKE+SWIGExceptionHelper when calling functions from itk.simple. I am unsure how to wrap all library methods in MonoPInvokeCallback, and I do not know how to modify SWIG and CMake to account for il2cpp during the SimpleITK compilation.

Has anyone faced a similar issue or knows of a workaround? Currently, I am only using a few SimpleITK functions, but I plan to expand this set, which might necessitate building a custom DLL and defining a list of used functions to implement overload. However, this could compromise the universality of the solution.

Any advice or guidance would be greatly appreciated. Thank you for your help!

NotSupportedException: To marshal a managed method, please add an attribute named 'MonoPInvokeCallback' to the method definition. The method we're attempting to marshal is: itk.simple.SimpleITKPINVOKE+SWIGExceptionHelper::SetPendingApplicationException
  at itk.simple.SimpleITKPINVOKE+SWIGExceptionHelper..cctor () [0x00000] in <00000000000000000000000000000000>:0 
  at itk.simple.SimpleITKPINVOKE..cctor () [0x00000] in <00000000000000000000000000000000>:0 
  at itk.simple.VectorUInt32..ctor () [0x00000] in <00000000000000000000000000000000>:0 
  at itk.simple.VectorUInt32..ctor (System.Collections.Generic.IEnumerable`1[T] c) [0x00000] in <00000000000000000000000000000000>:0 
  at DicomDataHandler.Start () [0x00000] in <00000000000000000000000000000000>:0 
Rethrow as TypeInitializationException: The type initializer for 'itk.simple.SimpleITKPINVOKE.SWIGExceptionHelper' threw an exception.
  at itk.simple.SimpleITKPINVOKE..cctor () [0x00000] in <00000000000000000000000000000000>:0 
  at itk.simple.VectorUInt32..ctor () [0x00000] in <00000000000000000000000000000000>:0 
  at itk.simple.VectorUInt32..ctor (System.Collections.Generic.IEnumerable`1[T] c) [0x00000] in <00000000000000000000000000000000>:0 
  at DicomDataHandler.Start () [0x00000] in <00000000000000000000000000000000>:0 
Rethrow as TypeInitializationException: The type initializer for 'itk.simple.SimpleITKPINVOKE' threw an exception.
  at itk.simple.VectorUInt32..ctor () [0x00000] in <00000000000000000000000000000000>:0 
  at itk.simple.VectorUInt32..ctor (System.Collections.Generic.IEnumerable`1[T] c) [0x00000] in <00000000000000000000000000000000>:0 
  at DicomDataHandler.Start () [0x00000] in <00000000000000000000000000000000>:0 

Hello @Anton_Smirnov,

This seems to be a particular issue with the SWIG and Unity combination. Unfortunately, we don’t have any idea what could be causing it. Possibly ask the question on the unity discussions forum.

1 Like