📦 about 🧻 posts
Unity 2018 supports IL2CPP on windows standalone. But when you run Facepunch.Steamworks with it, you get errors, like.
NotSupportedException: IL2CPP does not support marshaling 
delegates that point to instance methods to native code. 
So yesterday I set out to fix that. I’m guessing a lot of people are going to be doing the same, so here’s some info.

The Problem

Pretty clear, you can’t turn a instance method (a class function) into a function pointer and pass it to c++ with il2cpp. But you can pass static functions. So the solution is to convert all of the functions you pass to c++ into static functions and use those instead. How hard that is depends on your implementation.

The Next Problem

Your next problem is going to be
NotSupportedException: To marshal a manged method, please add 
an attribute named 'MonoPInvokeCallback' to the method definition
This seems to be a little bit of il2cpp black magic. I don’t know where the MonoPInvokeCallbackAttribute is defined, but we don’t want to be including unity or mono dlls in Facepunch.Steamworks. Thankfully, where it’s defined doesn’t matter. You can define your own version. in FPSW I did it like this.
internal class MonoPInvokeCallbackAttribute : Attribute { public MonoPInvokeCallbackAttribute() { } }
Then add that to any function you’re gonna be marshalling as a function pointer.
[MonoPInvokeCallback] internal static void OnResult( IntPtr param ) { OnResultWithInfo( param, false, 0 ); }
I’ve seen implementations of MonoPInvokeCallback taking a System.Type in the constructor, with people passing in a delegate type. That doesn’t seem to be needed in my experience (but fair warning, I’ve only tested it on 64bit windows builds).

Add a Comment

An error has occurred. This application may no longer respond until reloaded. Reload 🗙