-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recognize new names for Xbox Console Companion #170
Open
Ignefolio
wants to merge
2
commits into
fqlx:master
Choose a base branch
from
Ignefolio:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace XboxKeyboardMouse | ||
{ | ||
internal struct WINDOWINFO | ||
{ | ||
public uint ownerpid; | ||
public uint childpid; | ||
} | ||
|
||
public class UWPUtils | ||
{ | ||
#region User32 | ||
[DllImport("user32.dll")] | ||
public static extern IntPtr GetForegroundWindow(); | ||
[DllImport("user32.dll", SetLastError = true)] | ||
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); | ||
|
||
/// <summary> | ||
/// Delegate for the EnumChildWindows method | ||
/// </summary> | ||
/// <param name="hWnd">Window handle</param> | ||
/// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param> | ||
/// <returns>True to continue enumerating, false to bail.</returns> | ||
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter); | ||
|
||
[DllImport("user32", SetLastError = true)] | ||
[return: MarshalAs(UnmanagedType.Bool)] | ||
public static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowProc lpEnumFunc, IntPtr lParam); | ||
#endregion | ||
|
||
#region Kernel32 | ||
public const UInt32 PROCESS_QUERY_INFORMATION = 0x400; | ||
public const UInt32 PROCESS_VM_READ = 0x010; | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern bool QueryFullProcessImageName([In]IntPtr hProcess, [In]int dwFlags, [Out]StringBuilder lpExeName, ref int lpdwSize); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
public static extern IntPtr OpenProcess( | ||
UInt32 dwDesiredAccess, | ||
[MarshalAs(UnmanagedType.Bool)] | ||
Boolean bInheritHandle, | ||
Int32 dwProcessId | ||
); | ||
#endregion | ||
|
||
/// <summary> | ||
/// Gets the path of application running in foreground | ||
/// </summary> | ||
/// <param name="hWnd">Window handle</param> | ||
/// <returns>Path of the application</returns> | ||
public static string GetProcessName(IntPtr hWnd) | ||
{ | ||
string processName = null; | ||
|
||
hWnd = GetForegroundWindow(); | ||
|
||
if (hWnd == IntPtr.Zero) | ||
return null; | ||
|
||
uint pID; | ||
GetWindowThreadProcessId(hWnd, out pID); | ||
|
||
IntPtr proc; | ||
if ((proc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, (int)pID)) == IntPtr.Zero) | ||
return null; | ||
|
||
int capacity = 2000; | ||
StringBuilder sb = new StringBuilder(capacity); | ||
QueryFullProcessImageName(proc, 0, sb, ref capacity); | ||
|
||
processName = sb.ToString(0, capacity); | ||
|
||
// UWP apps are wrapped in another app called, if this has focus then try and find the child UWP process | ||
if (Path.GetFileName(processName).Equals("ApplicationFrameHost.exe")) | ||
{ | ||
processName = UWP_AppName(hWnd, pID); | ||
} | ||
|
||
return processName; | ||
} | ||
|
||
#region Get UWP Application Name | ||
|
||
/// <summary> | ||
/// Find child process for uwp apps, edge, mail, etc. | ||
/// </summary> | ||
/// <param name="hWnd">hWnd</param> | ||
/// <param name="pID">pID</param> | ||
/// <returns>The application name of the UWP.</returns> | ||
private static string UWP_AppName(IntPtr hWnd, uint pID) | ||
{ | ||
WINDOWINFO windowinfo = new WINDOWINFO(); | ||
windowinfo.ownerpid = pID; | ||
windowinfo.childpid = windowinfo.ownerpid; | ||
|
||
IntPtr pWindowinfo = Marshal.AllocHGlobal(Marshal.SizeOf(windowinfo)); | ||
|
||
Marshal.StructureToPtr(windowinfo, pWindowinfo, false); | ||
|
||
EnumWindowProc lpEnumFunc = new EnumWindowProc(EnumChildWindowsCallback); | ||
EnumChildWindows(hWnd, lpEnumFunc, pWindowinfo); | ||
|
||
windowinfo = (WINDOWINFO)Marshal.PtrToStructure(pWindowinfo, typeof(WINDOWINFO)); | ||
|
||
IntPtr proc; | ||
if ((proc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, (int)windowinfo.childpid)) == IntPtr.Zero) | ||
return null; | ||
|
||
int capacity = 2000; | ||
StringBuilder sb = new StringBuilder(capacity); | ||
QueryFullProcessImageName(proc, 0, sb, ref capacity); | ||
|
||
Marshal.FreeHGlobal(pWindowinfo); | ||
|
||
return sb.ToString(0, capacity); | ||
} | ||
|
||
/// <summary> | ||
/// Callback for enumerating the child windows. | ||
/// </summary> | ||
/// <param name="hWnd">hWnd</param> | ||
/// <param name="lParam">lParam</param> | ||
/// <returns>always <c>true</c>.</returns> | ||
private static bool EnumChildWindowsCallback(IntPtr hWnd, IntPtr lParam) | ||
{ | ||
WINDOWINFO info = (WINDOWINFO)Marshal.PtrToStructure(lParam, typeof(WINDOWINFO)); | ||
|
||
uint pID; | ||
GetWindowThreadProcessId(hWnd, out pID); | ||
|
||
if (pID != info.ownerpid) | ||
info.childpid = pID; | ||
|
||
Marshal.StructureToPtr(info, lParam, true); | ||
|
||
return true; | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes, while switching between applications/windows, GetForegroundWindow can return a handle of 0. GetProcessName then returns null, and the following
null.EndsWith
crashes the application. We need null safety on name, and should probably skip the GetProcessName work when handle is 0 as well.