internal static System.Collections.Generic.List <string> ReadStandaloneWindowTitles() { var parameter = new EnumWindowsParameter { Titles = new System.Collections.Generic.List <string>(), ProcessId = WindowsApi.GetCurrentProcessId(), }; var parameterGCHandle = GCHandle.Alloc(parameter); try { WindowsApi.EnumWindows(EnumWindowsCallback, GCHandle.ToIntPtr(parameterGCHandle)); } finally { parameterGCHandle.Free(); } if (parameter.LastWin32Error != 0) { throw new Exception("Unknown error: " + parameter.LastWin32Error); } if (!parameter.Found) { throw new Exception("No window found"); } return(parameter.Titles); }
internal static void Change(string title) { // The article https://qiita.com/kirurobo/items/82dd484ad6374e725a43#%E5%95%8F%E9%A1%8C%E7%82%B9-3 // says that System.Diagnostics.Process.GetCurrentProcess().Id fails in some environment. We use // GetCurrentProcessId() instead believing it will never fail. var processId = WindowsApi.GetCurrentProcessId(); var parameter = new EnumWindowsParameter { Title = title, ProcessId = processId, }; var parameterGCHandle = GCHandle.Alloc(parameter); bool enumWindowsResult; int enumWindowsError; try { enumWindowsResult = WindowsApi.EnumWindows(EnumWindowsCallback, GCHandle.ToIntPtr(parameterGCHandle)); enumWindowsError = Marshal.GetLastWin32Error(); } finally { parameterGCHandle.Free(); } if (!enumWindowsResult) { throw new StandaloneWindowTitleChangeException(StandaloneWindowTitleChangeException.Error.Unknown, "Failed to enumerate windows", new Win32Exception(enumWindowsError)); } if (parameter.InnerException != null) { throw new StandaloneWindowTitleChangeException(StandaloneWindowTitleChangeException.Error.Unknown, parameter.InnerExceptionMessage, parameter.InnerException); } if (!parameter.Found) { throw new StandaloneWindowTitleChangeException(StandaloneWindowTitleChangeException.Error.NoWindow, "No window found"); } }