Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kirsbo/c8ed6c13a014f05689ab to your computer and use it in GitHub Desktop.
Save kirsbo/c8ed6c13a014f05689ab to your computer and use it in GitHub Desktop.
C# - Get name of currently active application

Authored in 2011

The following code can be copy pasted directly into a C# class. Calls to getCurrentAppName() will return a string with the name of the current application.

        #region "WinAPI"

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);

        #endregion


       private string getCurrentAppName()
        {
            IntPtr activeAppHandle = GetForegroundWindow();

            IntPtr activeAppProcessId;
            GetWindowThreadProcessId(activeAppHandle, out activeAppProcessId);

            Process currentAppProcess = Process.GetProcessById((int)activeAppProcessId);
            string currentAppName = FileVersionInfo.GetVersionInfo(currentAppProcess.MainModule.FileName).FileDescription;

            return currentAppName;
        }
@ledlamp
Copy link

ledlamp commented Oct 1, 2022

image

@kirsbo
Copy link
Author

kirsbo commented Oct 1, 2022

Compiling your app as 64 bit instead of 32 bit, will resolve this error.

@ledlamp
Copy link

ledlamp commented Oct 2, 2022

but then get access denied error so i did this

		[Flags]
		public enum ProcessAccessFlags : uint {
			PROCESS_ALL_ACCESS = 0x001F0FFF,
			PROCESS_CREATE_PROCESS = 0x00000080,
			PROCESS_CREATE_THREAD = 0x00000002,
			PROCESS_DUP_HANDLE = 0x00000040,
			PROCESS_QUERY_INFORMATION = 0x00000400,
			PROCESS_QUERY_LIMITED_INFORMATION = 0x00001000,
			PROCESS_SET_INFORMATION = 0x00000200,
			PROCESS_SET_QUOTA = 0x00000100,
			PROCESS_SUSPEND_RESUME = 0x00000800,
			PROCESS_TERMINATE = 0x00000001,
			PROCESS_VM_OPERATION = 0x00000008,
			PROCESS_VM_READ = 0x00000010,
			PROCESS_VM_WRITE = 0x00000020
		}

		[DllImport("user32.dll")]
		private static extern IntPtr GetForegroundWindow();

		[DllImport("user32.dll")]
		private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);

		[DllImport("kernel32.dll")]
		private static extern bool QueryFullProcessImageName([In] IntPtr hProcess, [In] uint dwFlags, [Out] StringBuilder lpExeName, ref uint lpdwSize);

		[DllImport("kernel32.dll")]
		private static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, bool bInheritHandle, int dwProcessId);

		private static string GetActiveAppName() {
			IntPtr foregroundWindowId = GetForegroundWindow();
			GetWindowThreadProcessId(foregroundWindowId, out IntPtr activeAppProcessId);
			IntPtr hprocess = OpenProcess(ProcessAccessFlags.PROCESS_QUERY_LIMITED_INFORMATION, false, (int)activeAppProcessId);
			uint lpdwSize = 1024;
			StringBuilder lpExeName = new StringBuilder((int)lpdwSize);
			QueryFullProcessImageName(hprocess, 0, lpExeName, ref lpdwSize);
			var exePath = lpExeName.ToString();
			FileVersionInfo appInfo = FileVersionInfo.GetVersionInfo(exePath);
			return String.IsNullOrEmpty(appInfo.FileDescription) ? Path.GetFileName(exePath) : appInfo.FileDescription;
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment