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

**URL:** https://discourse.itk.org/t/challenges-with-itk-simple-simpleitkpinvoke-swigexceptionhelper-in-unity-il2cpp-final-build/6566
**Category:** Engineering
**Tags:** build, cmake, simpleitk
**Created:** [April 12, 2024, 2:54pm UTC](https://discourse.itk.org/t/challenges-with-itk-simple-simpleitkpinvoke-swigexceptionhelper-in-unity-il2cpp-final-build/6566 "2024-04-12T14:54:47Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Anton\_Smirnov](https://discourse.itk.org/user_avatar/discourse.itk.org/anton_smirnov/32/3997_2.png) [@Anton\_Smirnov](https://discourse.itk.org/u/Anton_Smirnov)
#### Post date: [April 12, 2024, 2:54pm UTC](https://discourse.itk.org/t/challenges-with-itk-simple-simpleitkpinvoke-swigexceptionhelper-in-unity-il2cpp-final-build/6566/1 "2024-04-12T14:54:47Z")

</div>

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!

```auto
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 

```

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [April 15, 2024, 1:38pm UTC](https://discourse.itk.org/t/challenges-with-itk-simple-simpleitkpinvoke-swigexceptionhelper-in-unity-il2cpp-final-build/6566/2 "2024-04-15T13:38:39Z")

</div>

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](https://discussions.unity.com/).
