private void FreeDependencies() { foreach (var dependency in _peImage.ImportDirectory.GetImportDescriptors()) { // Free the dependency using the Windows loader var routineAddress = _processContext.GetFunctionAddress("kernel32.dll", "FreeLibrary"); if (routineAddress == IntPtr.Zero) { throw new ApplicationException("Failed to resolve the address of a function in a module"); } var dependencyAddress = _processContext.GetModuleAddress(dependency.Name); if (dependencyAddress == IntPtr.Zero) { throw new ApplicationException("Failed to resolve the address of a module in the process"); } if (!_processContext.CallRoutine <bool>(routineAddress, dependencyAddress)) { throw new ApplicationException("Failed to free a dependency from the process"); } } _processContext.Refresh(); }