private void errorFn(IntPtr colVal) { if (OnErrorFunction == null) { return; } List <DisposableNamedOnnxValue> rgVal = new List <DisposableNamedOnnxValue>(); List <string> rgNames = new List <string>(); OrtValueCollection col = new OrtValueCollection(colVal); int nCount = col.Count; for (int i = 0; i < nCount; i++) { string strName; OrtValue val = col.GetAt(i, out strName); rgVal.Add(DisposableNamedOnnxValue.CreateTensorFromOnnxValue(strName, val)); } OnErrorFunction(this, new ErrorFunctionArgs(rgVal)); // Clean-up the data used during this batch. foreach (IDisposable iDispose in m_rgCleanUpList) { iDispose.Dispose(); } m_rgCleanUpList.Clear(); }
/// <summary> /// Fetch the spotify audio session. /// </summary> protected AudioSession? FetchAudioSession() { // Fetch sessions using var device = AudioDevice.GetDefaultAudioDevice(EDataFlow.eRender, ERole.eMultimedia); using var sessionManager = device.GetSessionManager(); using var sessions = sessionManager.GetSessionCollection(); // Check main process var sessionCount = sessions.Count; using var sessionCache = new DisposableList<AudioSession>(sessionCount); for (var i = 0; i < sessions.Count; i++) { var session = sessions[i]; if (session.ProcessID == MainWindowProcess?.Id) { Logger.LogInfo("SpotifyHook: Successfully fetched audio session using main window process."); _audioSession = session; return _audioSession; } else { // Store non-spotify sessions in disposable list to make sure that they the underlying COM objects are disposed. sessionCache.Add(session); } } Logger.LogWarning("SpotifyHook: Failed to fetch audio session using main window process."); // Try fetch through other "spotify" processes. var processes = FetchSpotifyProcesses(); // Transfer the found sessions into a dictionary to speed up the search by process id. // (we do this here to avoid the overhead as most of the time we will find the session in the code above.) using var sessionMap = new ValueDisposableDictionary<uint, AudioSession>(); foreach (var session in sessionCache) sessionMap.Add(session.ProcessID, session); sessionCache.Clear(); foreach (var process in processes) { var processId = (uint)process.Id; // skip main process as we already checked it if (MainWindowProcess?.Id == processId) continue; if (sessionMap.TryGetValue(processId, out AudioSession session)) { _audioSession = session; Logger.LogInfo("SpotifyHook: Successfully fetched audio session using secondary spotify processes."); // remove from map to avoid disposal sessionMap.Remove(processId); return _audioSession; } } Logger.LogError("SpotifyHook: Failed to fetch audio session."); return null; }
private IEnumerable <NtObject> CreateDirectoriesAndObject() { if (Close) { throw new ArgumentException("Can't use CreateDirectories and Close at the same time"); } DisposableList <NtObject> objects = new DisposableList <NtObject>(); string[] path_parts = ResolvePath().Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); StringBuilder builder = new StringBuilder(); bool finished = false; if (Root == null) { builder.Append(@"\"); } try { for (int i = 0; i < path_parts.Length - 1; ++i) { builder.Append(path_parts[i]); NtDirectory dir = null; try { dir = NtDirectory.Create(builder.ToString(), Root, DirectoryAccessRights.MaximumAllowed); } catch (NtException) { } if (dir != null) { objects.Add(dir); } builder.Append(@"\"); } objects.Add((NtObject)CreateObject(ResolvePath(), AttributeFlags, Root, SecurityQualityOfService, SecurityDescriptor)); finished = true; } finally { if (!finished) { objects.Dispose(); objects.Clear(); } } return(objects.ToArray()); }
/// <summary> /// Create an instance of the Win32 debug console. /// </summary> /// <param name="session_id">The session ID for the console. Set to 0 to capture global output.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The Win32 debug console.</returns> public static NtResult <Win32DebugConsole> Create(int session_id, bool throw_on_error) { using (var list = new DisposableList()) { using (var mutant = CreateMutant(session_id)) { // Wait 2 seconds for mutex, if it's not released just try and do it anyway. if (mutant.IsSuccess) { mutant.Result.Wait(NtWaitTimeout.FromSeconds(2)); } try { var buffer_ready = list.AddResource(CreateEvent(session_id, BUFFER_EVENT_NAME, throw_on_error)); if (!buffer_ready.IsSuccess) { return(buffer_ready.Cast <Win32DebugConsole>()); } var data_ready = list.AddResource(CreateEvent(session_id, DATA_EVENT_NAME, throw_on_error)); if (!data_ready.IsSuccess) { return(data_ready.Cast <Win32DebugConsole>()); } var buffer = list.AddResource(CreateSection(session_id, throw_on_error)); if (!buffer.IsSuccess) { return(buffer.Cast <Win32DebugConsole>()); } var mapped_buffer = list.AddResource(buffer.Result.MapRead(throw_on_error)); if (!mapped_buffer.IsSuccess) { return(mapped_buffer.Cast <Win32DebugConsole>()); } var ret = new Win32DebugConsole(session_id, buffer_ready.Result, data_ready.Result, buffer.Result, mapped_buffer.Result); list.Clear(); buffer_ready.Result.Set(); return(ret.CreateResult()); } finally { if (mutant.IsSuccess) { mutant.Result.Release(false); } } } } }