public static void Initialize() { string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); currentDir = Regex.Replace(currentDir, @"^(file\:\\)", string.Empty); // Environment.Is64BitProcess string path = Path.Combine(currentDir, (IntPtr.Size == 8) ? "Injection64.dll" : "Injection32.dll"); #if DEBUG MessageBox.Show("The injection.dll is only designed for .Net release mode process. it is not supposed to be used for debug mode."); #endif _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) throw new FileNotFoundException( string.Format( "Failed to load [{0}]", path) ); IntPtr ptr = GetProcAddress(_moduleHandle, "UpdateILCodes"); if (ptr == IntPtr.Zero) throw new MethodAccessException("Failed to locate UpdateILCodes function!"); _updateILCodesMethod = (UpdateILCodesDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(UpdateILCodesDelegate)); ptr = GetProcAddress(_moduleHandle, "GetStatus"); if (ptr == IntPtr.Zero) throw new MethodAccessException("Failed to locate GetStatus function!"); _getStatusDelegate = (GetStatusDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(GetStatusDelegate)); ptr = GetProcAddress(_moduleHandle, "WaitForIntializationCompletion"); if (ptr == IntPtr.Zero) throw new MethodAccessException("Failed to locate WaitForIntializationCompletion function!"); _waitForIntializationCompletionDelegate = (WaitForIntializationCompletionDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(WaitForIntializationCompletionDelegate)); }
public static void Initialize(bool showLoggingUI = false) { //if (showLoggingUI) //{ // _loggingForm = new LoggingForm(); // _loggingForm.Show(); //} string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); currentDir = Regex.Replace(currentDir, @"^(file\:\\)", string.Empty); // Environment.Is64BitProcess string path = Path.Combine(currentDir, (IntPtr.Size == 8) ? "Injection64.dll" : "Injection32.dll"); #if DEBUG Log("The injection.dll is only designed for .Net release mode process. it is not supposed to be used for debug mode."); #endif _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) { path = path.Replace("\\bin", "\\bin\\x86"); // try x86 _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) { throw new FileNotFoundException(string.Format("Failed to load [{0}]", path)); } } IntPtr ptr = GetProcAddress(_moduleHandle, "UpdateILCodes"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate UpdateILCodes function!"); } _updateILCodesMethod = (UpdateILCodesDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(UpdateILCodesDelegate)); ptr = GetProcAddress(_moduleHandle, "GetStatus"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate GetStatus function!"); } _getStatusDelegate = (GetStatusDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(GetStatusDelegate)); ptr = GetProcAddress(_moduleHandle, "WaitForIntializationCompletion"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate WaitForIntializationCompletion function!"); } _waitForIntializationCompletionDelegate = (WaitForIntializationCompletionDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(WaitForIntializationCompletionDelegate)); }
public static void Main(string[] args) { string currentFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); currentFile = Path.Combine(currentFile.Substring(6), (IntPtr.Size == 8) ? "Injection64.dll" : "Injection32.dll"); IntPtr _moduleHandle = LoadLibraryW(currentFile); if (_moduleHandle == IntPtr.Zero) { throw new FileNotFoundException(string.Format("Failed to load [{0}]", currentFile)); } IntPtr ptr = GetProcAddress(_moduleHandle, "UpdateILCodes"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate UpdateILCodes function!"); } _updateILCodesMethod = (UpdateILCodesDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(UpdateILCodesDelegate)); Type type = typeof(MainClass); MethodInfo methodInfo = type.GetMethod("CompareOneAndTwo", BindingFlags.NonPublic | BindingFlags.Static); RuntimeHelpers.PrepareMethod(methodInfo.MethodHandle); byte[] ilCodes = methodInfo.GetMethodBody().GetILAsByteArray(); //for (int i = 0; i < ilCodes.Length; i++) Console.WriteLine(ilCodes [i].ToString()); for (int i = 0; i < ilCodes.Length; i++) { if ((ilCodes [i] == OpCodes.Bge_S.Value)) { Console.WriteLine(i.ToString()); ilCodes [i] = (byte)OpCodes.Blt_S.Value; } if ((ilCodes [i] == OpCodes.Bge.Value)) { Console.WriteLine(i.ToString()); ilCodes [i] = (byte)OpCodes.Blt.Value; } } Console.WriteLine(ilCodes.Length.ToString()); Console.ReadKey(); UpdateILCodes(methodInfo, ilCodes); Console.WriteLine(string.Format("The result is: \"{0}\"", CompareOneAndTwo())); Console.ReadKey(); //if (_moduleHandle != IntPtr.Zero) FreeLibrary(_moduleHandle); }
private static void InternalInitialize() { string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); currentDir = Regex.Replace(currentDir, @"^(file\:\\)", string.Empty); // Environment.Is64BitProcess string path = Path.Combine(currentDir, (IntPtr.Size == 8) ? "Injection64.dll" : "Injection32.dll"); #if DEBUG Debug.WriteLine("The injection.dll is only designed for .Net release mode process. it is not supposed to be used for debug mode."); #endif _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) { var err = GetLastError(); Console.WriteLine(err); throw new FileNotFoundException(string.Format("Failed to load [{0}]", path)); } IntPtr ptr = GetProcAddress(_moduleHandle, "UpdateILCodes"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate UpdateILCodes function!"); } _updateILCodesMethod = (UpdateILCodesDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(UpdateILCodesDelegate)); ptr = GetProcAddress(_moduleHandle, "GetStatus"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate GetStatus function!"); } _getStatusDelegate = (GetStatusDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(GetStatusDelegate)); ptr = GetProcAddress(_moduleHandle, "WaitForIntializationCompletion"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate WaitForIntializationCompletion function!"); } _waitForIntializationCompletionDelegate = (WaitForIntializationCompletionDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(WaitForIntializationCompletionDelegate)); }
public static void Initialize() { string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); currentDir = Regex.Replace(currentDir, @"^(file\:\\)", string.Empty); // Environment.Is64BitProcess string path = Path.Combine(currentDir, (IntPtr.Size == 8) ? "Injection64.dll" : "Injection32.dll"); #if DEBUG MessageBox.Show("The injection.dll can only be used for .Net release mode process. it is not supposed to be used for debug mode."); #endif _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) { throw new FileNotFoundException(string.Format("Failed to load [{0}]", path)); } IntPtr ptr = GetProcAddress(_moduleHandle, "UpdateILCodes"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate UpdateILCodes function!"); } _updateILCodesMethod = (UpdateILCodesDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(UpdateILCodesDelegate)); ptr = GetProcAddress(_moduleHandle, "GetErrorMessage"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate GetErrorMessage function!"); } _getErrorMessage = (GetErrorMessageDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(GetErrorMessageDelegate)); ptr = GetProcAddress(_moduleHandle, "IsInitialized"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate IsInitialized function!"); } _isInitializedDelegate = (IsInitializedDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(IsInitializedDelegate)); ptr = GetProcAddress(_moduleHandle, "IsInjectionSucceeded"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate IsInjectionSucceeded function!"); } _isInjectionSucceededDelegate = (IsInjectionSucceededDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(IsInjectionSucceededDelegate)); ptr = GetProcAddress(_moduleHandle, "WaitForIntializationCompletion"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate WaitForIntializationCompletion function!"); } _waitForIntializationCompletionDelegate = (WaitForIntializationCompletionDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(WaitForIntializationCompletionDelegate)); ptr = GetProcAddress(_moduleHandle, "__CollectAddress"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate __CollectAddress function!"); } _collectVersionDelegate = (CollectVersionDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(CollectVersionDelegate)); }