I am trying to change a part in a third party library, it has a bug, however, there are no fixes and source code is not available as well.
At first, I tried to modify IL Opcodes directly by disassembling an assembly, but it has some kind of protection, so after recompiling it I was not able to use it even that I had removed Strong Name verification
from it.
I have tried to change IL instructions
in runtime, however
InjectionHelper.Initialize();
InjectionHelper.WaitForIntializationCompletion();
Type type = obj.GetType();
var methods = type.GetMethods();
// MethodInfo targetMethod = type.GetMethod("MethodName", BindingFlags.NonPublic | BindingFlags.Instance);
var methodInfo = type
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Single(
m =>
m.ReturnType == typeof(string) &&
m.GetParameters().Length == 1);
RuntimeHelpers.PrepareMethod(methodInfo.MethodHandle);
byte[] ilOpCodes = methodInfo.GetMethodBody()?.GetILAsByteArray();
for (int i = 0; i < ilOpCodes.Length; i++)
{
if (ilOpCodes[i] == OpCodes.Ldc_I4_1.Value)
{
ilOpCodes[i] = (byte) OpCodes.Ldc_I4_0.Value;
break;
}
}
InjectionHelper.UpdateILCodes(methodInfo,ilOpCodes);
But unfortunately got an exception.
Unhandled Exception: System.Exception: UpdateILCodes() failed, please check the initialization is failed or uncompleted.
Is there any mistake I made? I used this article as a reference https://www.codeproject.com/Articles/463508/NET-CLR-Injection-Modify-IL-Code-during-Run-time So what is the best way to make it usable?