vfsfitvnm / frida-il2cpp-bridge

A Frida module to dump, trace or hijack any Il2Cpp application at runtime, without needing the global-metadata.dat file.
https://github.com/vfsfitvnm/frida-il2cpp-bridge/wiki
MIT License
1.05k stars 203 forks source link

Question: How can I create my own instance of an existing struct? #475

Closed matl4c closed 10 months ago

matl4c commented 10 months ago

I understand how to create an instance of an existing class...

const AssemblyCSharp = Il2Cpp.Domain.assembly("Assembly-CSharp").image;
const interestingClass = Il2Cpp.Domain.class("Some.Namespace.interestingClass");

let myInstanceOfInterestingClass = interestingClass.alloc();
...

If the namespace Some.Namespace has a struct of interestingStruct, how would I reference and create my own instance of that?

vfsfitvnm commented 10 months ago

Create the object, then call Il2Cpp.Object::unbox to get a Il2Cpp::ValueType (a struct). PS: Il2Cpp.Class::alloc only allocates the instance: its constructor will not be invoked - you need to call it aftwerwards like any other method:

const myInstanceOfInterestingClass = interestingClass.alloc();
myInstanceOfInterestingClass.method(".ctor").invoke(...);

// shorthand to alloc + ctor (but without arguments)
const myInstanceOfInterestingClass = interestingClass.new();