Skip to content

Commit

Permalink
Use cswin32 to pinvoke methods
Browse files Browse the repository at this point in the history
  • Loading branch information
taooceros committed Sep 9, 2023
1 parent b7fbd54 commit 9dfb6df
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 163 deletions.
12 changes: 8 additions & 4 deletions Components/LivePreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// Code forked from Betsegaw Tadele's https://github.com/betsegaw/windowwalker/

using System;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Dwm;
using Windows.Win32.UI.Shell;

namespace Flow.Plugin.WindowWalker.Components
{
Expand All @@ -17,14 +21,14 @@ internal class LivePreview
/// Makes sure that a window is excluded from the live preview
/// </summary>
/// <param name="hwnd">handle to the window to exclude</param>
public static void SetWindowExclusionFromLivePreview(IntPtr hwnd)
public static unsafe void SetWindowExclusionFromLivePreview(HWND hwnd)
{
var renderPolicy = (int)DwmNCRenderingPolicies.Enabled;

_ = NativeMethods.DwmSetWindowAttribute(
_ = PInvoke.DwmSetWindowAttribute(
hwnd,
12,
ref renderPolicy,
DWMWINDOWATTRIBUTE.DWMWA_EXCLUDED_FROM_PEEK,
&renderPolicy,
sizeof(int));
}

Expand Down
43 changes: 0 additions & 43 deletions Components/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,10 @@ public static extern bool QueryFullProcessImageName(
ref int lpdwSize);

// get process id to window handle
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int EnumWindows(EnumWindowsProc callPtr, int lPar);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, GetWindowCmd uCmd);

[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int EnumChildWindows(IntPtr hWnd, EnumWindowsProc callPtr, int lPar);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool IsWindow(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommand nCmdShow);

[DllImport("user32.dll")]
public static extern bool FlashWindow(IntPtr hwnd, bool bInvert);

[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

[DllImport("psapi.dll", BestFitMapping = false, CharSet = CharSet.Unicode)]
public static extern uint GetProcessImageFileName(IntPtr hProcess, [Out] StringBuilder lpImageFileName, [In][MarshalAs(UnmanagedType.U4)] int nSize);

[DllImport("user32.dll", SetLastError = true, BestFitMapping = false, CharSet = CharSet.Unicode)]
public static extern IntPtr GetProp(IntPtr hWnd, string lpString);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);

Expand Down
22 changes: 11 additions & 11 deletions Components/OpenWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Win32;
using Windows.Win32.Foundation;

namespace Flow.Plugin.WindowWalker.Components
{
Expand Down Expand Up @@ -57,8 +59,7 @@ private OpenWindows()
public void UpdateFlowWindow()
{
FlowWindow = null;
EnumWindowsProc callbackptr = FlowWindowCallback;
_ = NativeMethods.EnumWindows(callbackptr, 0);
_ = PInvoke.EnumWindows(FlowWindowCallback, 0);
}

/// <summary>
Expand All @@ -68,11 +69,10 @@ public void UpdateOpenWindowsList()
{
FlowWindow = null;
windows.Clear();
EnumWindowsProc callbackptr = WindowEnumerationCallBack;
_ = NativeMethods.EnumWindows(callbackptr, 0);
_ = PInvoke.EnumWindows(WindowEnumerationCallBack, 0);
}

private static string flowLauncherExe = "Flow.Launcher.exe";
private const string FlowLauncherExe = "Flow.Launcher.exe";

/// <summary>
/// Call back method for window enumeration, to find the Flow Window
Expand All @@ -81,11 +81,11 @@ public void UpdateOpenWindowsList()
/// <param name="lParam">Value being passed from the caller (we don't use this but might come in handy
/// in the future</param>
/// <returns>true to make sure to continue enumeration</returns>
public bool FlowWindowCallback(IntPtr hwnd, IntPtr lParam)
private BOOL FlowWindowCallback(HWND hwnd, LPARAM lParam)
{
Window window = new Window(hwnd);

if (window.Process.Name == flowLauncherExe && window.IsWindow)
if (window.Process.Name == FlowLauncherExe && window.IsWindow)
{
window.IsFlowWindow = true;
FlowWindow = window;
Expand All @@ -102,11 +102,11 @@ public bool FlowWindowCallback(IntPtr hwnd, IntPtr lParam)
/// <param name="lParam">Value being passed from the caller (we don't use this but might come in handy
/// in the future</param>
/// <returns>true to make sure to continue enumeration</returns>
public bool WindowEnumerationCallBack(IntPtr hwnd, IntPtr lParam)
private BOOL WindowEnumerationCallBack(HWND hwnd, LPARAM lParam)
{
Window window = new Window(hwnd);
var window = new Window(hwnd);

if (window.Process.Name == flowLauncherExe && window.IsWindow)
if (window.Process.Name == FlowLauncherExe && window.IsWindow)
{
window.IsFlowWindow = true;
FlowWindow = window;
Expand All @@ -117,7 +117,7 @@ public bool WindowEnumerationCallBack(IntPtr hwnd, IntPtr lParam)
&& (!window.IsToolWindow || window.IsAppWindow) && !window.TaskListDeleted
&& (SearchWindowsAcrossAllVDesktop || (window.Desktop.IsVisible || Main.VirtualDesktopHelperInstance.GetDesktopCount() < 2))
&& window.ClassName != "Windows.UI.Core.CoreWindow"
&& window.Process.Name != flowLauncherExe
&& window.Process.Name != FlowLauncherExe
&& (!window.IsCloaked ||
window.GetWindowCloakState() ==
Window.WindowCloakState.OtherDesktop)
Expand Down
1 change: 1 addition & 0 deletions Components/SearchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Windows.Win32;

namespace Flow.Plugin.WindowWalker.Components
{
Expand Down
18 changes: 10 additions & 8 deletions Components/VirtualDesktopHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Flow.Plugin.WindowWalker.Components.COM;
using IVirtualDesktopManager = Windows.Win32.UI.Shell.IVirtualDesktopManager;
using System.Windows.Controls;
using Windows.Win32.Graphics.Dwm;

namespace Flow.Plugin.WindowWalker.Components
{
Expand Down Expand Up @@ -327,7 +328,6 @@ public VDesktop GetWindowDesktop(IntPtr hWindow)
Log.Error("VirtualDesktopHelper.GetWindowDesktop() failed: The instance of <IVirtualDesktopHelper> isn't available.", typeof(VirtualDesktopHelper));
return CreateVDesktopInstance(Guid.Empty);
}

int hr = _virtualDesktopManager.GetWindowDesktopId((Windows.Win32.Foundation.HWND)hWindow, out Guid desktopId);
return hr != (int)HRESULT.S_OK || desktopId == Guid.Empty ? VDesktop.Empty : CreateVDesktopInstance(desktopId, hWindow);
}
Expand Down Expand Up @@ -389,10 +389,11 @@ public bool IsWindowOnVisibleDesktop(IntPtr hWindow, Guid? desktop = null)
/// <param name="hWindow">Handle of the window.</param>
/// <param name="desktop">Optional the desktop id if known</param>
/// <returns>A value indicating if the window is cloaked by Virtual Desktop Manager, because it is moved to an other desktop.</returns>
public bool IsWindowCloakedByVirtualDesktopManager(IntPtr hWindow, Guid? desktop = null)
internal unsafe bool IsWindowCloakedByVirtualDesktopManager(HWND hWindow, Guid? desktop = null)
{
var dwmCloakedState = 0;
// If a window is hidden because it is moved to an other desktop, then DWM returns type "CloakedShell". If DWM returns an other type the window is not cloaked by shell or VirtualDesktopManager.
_ = NativeMethods.DwmGetWindowAttribute(hWindow, (int)DwmWindowAttributes.Cloaked, out var dwmCloakedState, sizeof(uint));
_ = PInvoke.DwmGetWindowAttribute(hWindow, DWMWINDOWATTRIBUTE.DWMWA_CLOAKED, &dwmCloakedState, sizeof(uint));
return GetWindowDesktopAssignmentType(hWindow, desktop) == VirtualDesktopAssignmentType.OtherDesktop && dwmCloakedState == (int)DwmWindowCloakStates.CloakedShell;
}

Expand All @@ -409,11 +410,12 @@ public static bool IsWindowPinned(IntPtr hWnd)
/// <param name="hWindow">Handle of the top level window.</param>
/// <param name="desktopId">Guid of the target desktop.</param>
/// <returns><see langword="True"/> on success and <see langword="false"/> on failure.</returns>
public static bool MoveWindowToDesktop(IntPtr hWnd, IVirtualDesktop comVirtualDesktop)
internal static unsafe bool MoveWindowToDesktop(HWND hWnd, IVirtualDesktop comVirtualDesktop)
{
// move window to this desktop
if (hWnd == IntPtr.Zero) throw new ArgumentNullException();
_ = NativeMethods.GetWindowThreadProcessId(hWnd, out var processId);
if (hWnd == nint.Zero) throw new ArgumentNullException();
uint processId = 0;
_ = PInvoke.GetWindowThreadProcessId(hWnd, &processId);

if (Process.GetCurrentProcess().Id == processId)
{
Expand Down Expand Up @@ -454,7 +456,7 @@ public static bool MoveWindowToDesktop(IntPtr hWnd, IVirtualDesktop comVirtualDe
/// </summary>
/// <param name="hWindow">Handle of the top level window.</param>
/// <returns><see langword="True"/> on success and <see langword="false"/> on failure.</returns>
public bool MoveWindowOneDesktopLeft(IntPtr hWindow)
internal bool MoveWindowOneDesktopLeft(HWND hWindow)
{
var hr = GetWindowDesktopId(hWindow, out var windowDesktop);
if (hr != (int)HRESULT.S_OK)
Expand Down Expand Up @@ -486,7 +488,7 @@ public bool MoveWindowOneDesktopLeft(IntPtr hWindow)
/// </summary>
/// <param name="hWindow">Handle of the top level window.</param>
/// <returns><see langword="True"/> on success and <see langword="false"/> on failure.</returns>
public bool MoveWindowOneDesktopRight(IntPtr hWindow)
internal bool MoveWindowOneDesktopRight(HWND hWindow)
{
var hr = GetWindowDesktopId(hWindow, out var windowDesktop);
if (hr != (int)HRESULT.S_OK)
Expand Down
Loading

0 comments on commit 9dfb6df

Please sign in to comment.