Error in listing open windows
-
Hello, I am trying to get the list of open windows using C#, here is my code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace howto_list_desktop_windows
{
static class DesktopWindowsStuff
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd,
StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop,
EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
// Define the callback delegate's type.
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
// Save window titles and handles in these lists.
private static List WindowHandles;
private static List WindowTitles;
// Return a list of the desktop windows' handles and titles.
public static void GetDesktopWindowHandlesAndTitles(
out List handles, out List titles)
{
WindowHandles = new List();
WindowTitles = new List();
if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero))
{
handles = null;
titles = null;
}
else
{
// handles = null;
handles = WindowHandles;
titles = WindowTitles;
}
}
// We use this function to filter windows.
// This version selects visible windows that have titles.
private static bool FilterCallback(IntPtr hWnd, int lParam)
{
// Get the window's title.
StringBuilder sb_title = new StringBuilder(1024);
int length = GetWindowText(hWnd, sb_title, sb_title.Capacity);
string title = sb_title.ToString();
// If the wind -
Hello, I am trying to get the list of open windows using C#, here is my code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace howto_list_desktop_windows
{
static class DesktopWindowsStuff
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd,
StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop,
EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
// Define the callback delegate's type.
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
// Save window titles and handles in these lists.
private static List WindowHandles;
private static List WindowTitles;
// Return a list of the desktop windows' handles and titles.
public static void GetDesktopWindowHandlesAndTitles(
out List handles, out List titles)
{
WindowHandles = new List();
WindowTitles = new List();
if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero))
{
handles = null;
titles = null;
}
else
{
// handles = null;
handles = WindowHandles;
titles = WindowTitles;
}
}
// We use this function to filter windows.
// This version selects visible windows that have titles.
private static bool FilterCallback(IntPtr hWnd, int lParam)
{
// Get the window's title.
StringBuilder sb_title = new StringBuilder(1024);
int length = GetWindowText(hWnd, sb_title, sb_title.Capacity);
string title = sb_title.ToString();
// If the wind -
Try with EnumWindows function (Windows)[^] instead.
can you please edit my code..Sorry I am new to .net and I am not getting how to use EnumWindows
-
can you please edit my code..Sorry I am new to .net and I am not getting how to use EnumWindows
-
Hello, I am trying to get the list of open windows using C#, here is my code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace howto_list_desktop_windows
{
static class DesktopWindowsStuff
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd,
StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop,
EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
// Define the callback delegate's type.
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
// Save window titles and handles in these lists.
private static List WindowHandles;
private static List WindowTitles;
// Return a list of the desktop windows' handles and titles.
public static void GetDesktopWindowHandlesAndTitles(
out List handles, out List titles)
{
WindowHandles = new List();
WindowTitles = new List();
if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero))
{
handles = null;
titles = null;
}
else
{
// handles = null;
handles = WindowHandles;
titles = WindowTitles;
}
}
// We use this function to filter windows.
// This version selects visible windows that have titles.
private static bool FilterCallback(IntPtr hWnd, int lParam)
{
// Get the window's title.
StringBuilder sb_title = new StringBuilder(1024);
int length = GetWindowText(hWnd, sb_title, sb_title.Capacity);
string title = sb_title.ToString();
// If the windHave you found solution for this issue ? I am facing similar issue.