Skip to content

Use UTF8 in Il2Cpp PInvoke calls#220

Merged
ds5678 merged 3 commits intoBepInEx:masterfrom
HookedBehemoth:fix/non-ansi-nested-resolve
Jul 8, 2025
Merged

Use UTF8 in Il2Cpp PInvoke calls#220
ds5678 merged 3 commits intoBepInEx:masterfrom
HookedBehemoth:fix/non-ansi-nested-resolve

Conversation

@HookedBehemoth
Copy link
Contributor

In a previous pull request I had already worked around this by loading all FFI strings as System.String.
I've run into this issue again and reimplemented this part from the PR: 6a7afa1

This seems to be the only critical part it seems. If this resolve fails, the NativeClassPtr is null and other parts of the code will dereference it without checking.


while ((nestedTypePtr = il2cpp_class_get_nested_types(enclosingType, ref iter)) != IntPtr.Zero)
if (Marshal.PtrToStringAnsi(il2cpp_class_get_name(nestedTypePtr)) == nestedTypeName)
if (Marshal.PtrToStringUTF8(il2cpp_class_get_name(nestedTypePtr)) == nestedTypeName)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it difficult to believe this is the only place. Strings should be either all Ansi or all UTF8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My old PR loaded System.String everywhere.
abdebd5

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That commit is more attractive to me. A couple small tweaks:

  • All DllImport in IL2CPP.cs should be changed to LibraryImport.
  • Delete Il2CppApi.cs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LibraryImport is only supported on .NET 7+
Are there plans for moving the entire project to 8 or 9? 10 is still 4 months away but would be LTS.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about that limitation. DllImport is fine for now. That'll let modding communities make immediate use of your work.

v2 is coming soon. It will probably be .NET 9 for a beta period and switch to .NET 10 in November.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks
I've now had time to test the patch and I've not found any issue so far.

@HookedBehemoth HookedBehemoth force-pushed the fix/non-ansi-nested-resolve branch from 04a4c61 to 38e9b33 Compare July 7, 2025 12:10
@ds5678 ds5678 changed the title fix crash when resolving types with non-ansi names Use UTF8 in Il2Cpp PInvoke calls Jul 8, 2025
@ds5678 ds5678 merged commit f749ff6 into BepInEx:master Jul 8, 2025
2 checks passed
@slxdy
Copy link
Contributor

slxdy commented Aug 22, 2025

Has this even been tested? Like at all? It literally causes heap corruption no matter the game or platform

slxdy added a commit to slxdy/Il2CppInterop that referenced this pull request Aug 23, 2025
Fixes BepInEx#220 which completely broke most of the Il2Cpp imports.
Reverts BepInEx#227. The fallback method for GetIl2CppNestedType is no longer needed as the root cause of the issue has been solved.
@slxdy slxdy mentioned this pull request Aug 23, 2025
@HookedBehemoth
Copy link
Contributor Author

I've been running it for a bit with unity explorer on some unity 2021 game. Didn't use it heavily.
On vacation right now so can't investigate further or submit a new pull request.

ds5678 pushed a commit that referenced this pull request Aug 23, 2025
* Fix UTF8 marshalling for IL2CPP imports

Fixes #220 which completely broke most of the Il2Cpp imports.
Reverts #227. The fallback method for GetIl2CppNestedType is no longer needed as the root cause of the issue has been solved.

* Add UTF-8 support to the Class Injector
@ds5678 ds5678 added this to the 1.5.1 milestone Sep 2, 2025
XtraCube added a commit to XtraCube/Il2CppInterop that referenced this pull request Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants