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)); }
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)); }