private void FreeDependencies() { foreach (var dependency in _peImage.ImportDirectory.GetImportDescriptors()) { // Free the dependency using the Windows loader var dependencyAddress = _processContext.GetModuleAddress(dependency.Name); if (!_processContext.CallRoutine <bool>(_processContext.GetFunctionAddress("kernel32.dll", "FreeLibrary"), dependencyAddress)) { throw new ApplicationException($"Failed to free the dependency {dependency.Name} from the process"); } } _processContext.ClearModuleCache(); }