Skip to content

Instantly share code, notes, and snippets.

@Ilchert
Created August 12, 2023 18:14
Show Gist options
  • Save Ilchert/ddb1235f9c830c16a49f38574e239a33 to your computer and use it in GitHub Desktop.
Save Ilchert/ddb1235f9c830c16a49f38574e239a33 to your computer and use it in GitHub Desktop.
Find layout from console
using System.Diagnostics;
using System.Runtime.InteropServices;
while (true)
{
var pbi = new ParentProcessUtilities();
var process = Process.GetCurrentProcess();
int status;
while (true)
{
status = NtQueryInformationProcess(process.Handle, 0, ref pbi, Marshal.SizeOf(pbi), out var returnLength);
if (status != 0 || pbi.InheritedFromUniqueProcessId == 0)
break;
process = Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32());
var isVisible = IsWindowVisible(process.MainWindowHandle);
Console.WriteLine($"{process.ProcessName} {process.Id} {isVisible}");
if (isVisible)
{
var t = GetWindowThreadProcessId(process.MainWindowHandle, IntPtr.Zero);
var layout = GetKeyboardLayout(t);
Console.WriteLine(layout);
break;
}
}
Console.WriteLine("Change language");
Console.ReadLine();
}
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hwnd, IntPtr proccess);
[DllImport("user32.dll")]
static extern IntPtr GetKeyboardLayout(uint thread);
[DllImport("ntdll.dll")]
static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessUtilities processInformation, int processInformationLength, out int returnLength);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
public struct ParentProcessUtilities
{
// These members must match PROCESS_BASIC_INFORMATION
internal IntPtr Reserved1;
internal IntPtr PebBaseAddress;
internal IntPtr Reserved2_0;
internal IntPtr Reserved2_1;
internal IntPtr UniqueProcessId;
internal IntPtr InheritedFromUniqueProcessId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment