-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cs
More file actions
46 lines (38 loc) · 1.39 KB
/
program.cs
File metadata and controls
46 lines (38 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Text;
using System.Runtime.InteropServices;
public class Programa
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
public static string ObtenerTituloVentanaActiva()
{
try
{
const int nChars = 256;
StringBuilder buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, buff, nChars) > 0)
{
uint processId;
GetWindowThreadProcessId(handle, out processId);
Process process = Process.GetProcessById((int)processId);
return $"{buff} - {process.ProcessName}.exe";
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
return "";
}
public static void Main()
{
string tituloVentanaActiva = ObtenerTituloVentanaActiva();
Console.WriteLine("Título de la ventana activa: " + tituloVentanaActiva);
}
}