-
-
Save paulhayes/06435c009d8ae2d305cc7d9e2f6cc1b7 to your computer and use it in GitHub Desktop.
using System; | |
using UnityEngine; | |
public class DllLoadingAtRuntimeExample : MonoBehaviour | |
{ | |
static IntPtr nativeLibraryPtr; | |
delegate int MultiplyFloat(float number, float multiplyBy); | |
delegate void DoSomething(string words); | |
void Awake() | |
{ | |
if (nativeLibraryPtr != IntPtr.Zero) return; | |
nativeLibraryPtr = Native.LoadLibrary("MyNativeLibraryName"); | |
if (nativeLibraryPtr == IntPtr.Zero) | |
{ | |
Debug.LogError("Failed to load native library"); | |
} | |
} | |
void Update() | |
{ | |
Native.Invoke<DoSomething>(nativeLibraryPtr, "Hello, World!"); | |
int result = Native.Invoke<int, MultiplyFloat>(nativeLibraryPtr, 10, 5); // Should return the number 50. | |
} | |
void OnApplicationQuit() | |
{ | |
if (nativeLibraryPtr == IntPtr.Zero) return; | |
Debug.Log(Native.FreeLibrary(nativeLibraryPtr) | |
? "Native library successfully unloaded." | |
: "Native library could not be unloaded."); | |
} | |
} |
/* | |
* Native dll invocation helper by Francis R. Griffiths-Keam | |
* www.runningdimensions.com/blog | |
*/ | |
using System; | |
using System.Runtime.InteropServices; | |
using UnityEngine; | |
public static class Native | |
{ | |
public static T Invoke<T, T2>(IntPtr library, params object[] pars) | |
{ | |
IntPtr funcPtr = GetProcAddress(library, typeof(T2).Name); | |
if (funcPtr == IntPtr.Zero) | |
{ | |
Debug.LogWarning("Could not gain reference to method address."); | |
return default(T); | |
} | |
var func = Marshal.GetDelegateForFunctionPointer(GetProcAddress(library, typeof(T2).Name), typeof(T2)); | |
return (T)func.DynamicInvoke(pars); | |
} | |
public static void Invoke<T>(IntPtr library, params object[] pars) | |
{ | |
IntPtr funcPtr = GetProcAddress(library, typeof(T).Name); | |
if (funcPtr == IntPtr.Zero) | |
{ | |
Debug.LogWarning("Could not gain reference to method address."); | |
return; | |
} | |
var func = Marshal.GetDelegateForFunctionPointer(funcPtr, typeof(T)); | |
func.DynamicInvoke(pars); | |
} | |
[DllImport("kernel32", SetLastError = true)] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool FreeLibrary(IntPtr hModule); | |
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] | |
public static extern IntPtr LoadLibrary(string lpFileName); | |
[DllImport("kernel32")] | |
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); | |
} |
Any ideas how I could make this work on Android, I am currently patching a file to create the library at runtime and then loading it and would like to do the same on android.
That sounds impossible on Android. Can you let me know more about what the code of your compiling at runtime? What does it do, and why does it need to be changed?
Project is here, it also uses this , I create a modified version of the original EXE in DLL format and then create a bps patch file based on the original EXE, this patch file is then used to patch the original EXE into a DLL at launch which the game then loads at runtime. This way, all the original EXE tables, text, etc. are preserved and my new 64-bit DLL code is swapped in to replace the old 32-bit EXE code. I can then run the original game engine in Unity without having to include any intellectual property from the original EXE. Currently this is working for Windows but if I want to get it working on the Oculus Quest I need to do the same on Android.
🤯 Wow, that is incredible work. If I have any ideas I'll suggest them.
Thanks.
That's really useful info.