Last active
August 12, 2023 18:36
-
-
Save Ilchert/145931f70577a6c43d5bebf02f245f00 to your computer and use it in GitHub Desktop.
Conhost console search
This file contains hidden or 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
// See https://aka.ms/new-console-template for more information | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
while (true) | |
{ | |
var process = Process.GetCurrentProcess(); | |
ulong cohost = 0; | |
int status; | |
status = NtQueryInformationProcess(process.Handle, 0x31, ref cohost, Marshal.SizeOf(typeof(ulong)), out _); // cohost | |
if (status != 0 || cohost == 0) | |
break; | |
process = Process.GetProcessById((int)cohost); | |
while (true) | |
{ | |
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; | |
} | |
var pbi = new ParentProcessUtilities(); | |
status = NtQueryInformationProcessParent(process.Handle, 0, ref pbi, Marshal.SizeOf<ParentProcessUtilities>(), out _); | |
if (status != 0 || pbi.InheritedFromUniqueProcessId == 0) | |
break; | |
process = Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32()); | |
} | |
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 ulong processInformation, int processInformationLength, out int returnLength); | |
[DllImport("ntdll.dll", EntryPoint = "NtQueryInformationProcess")] | |
static extern int NtQueryInformationProcessParent(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