Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:56:57+00:00 2026-06-13T22:56:57+00:00

I would like to get a Window Caption as given by spy++ (highlighted in

  • 0

I would like to get a Window Caption as given by spy++ (highlighted in red)

enter image description here

I have code to get the window caption. It does this by enumerating all windows via a callback which checks the window caption by invoking GetWindowText. If the window with caption = "window title | my application" is Open then I expect the window caption to be included in the enumeration and discovered.

If the window count is not 1 then the function releases any window handles and returns null. In the case where null is returned this is considered a failure. In one test case where I ran this code 100 times I had a fail count of 99.

public delegate bool EnumDelegate(IntPtr hWnd, int lParam);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool IsWindowVisible(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

        [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

        static List<NativeWindow> collection = new List<NativeWindow>();

public static NativeWindow GetAppNativeMainWindow()
        {            
            GetNativeWindowHelper.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
            {
                StringBuilder strbTitle = new StringBuilder(255);
                int nLength = GetNativeWindowHelper.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
                string strTitle = strbTitle.ToString();

                if (!string.IsNullOrEmpty(strTitle))
                {
                    if (strTitle.ToLower().StartsWith("window title | my application"))
                    {
                        NativeWindow window = new NativeWindow();
                        window.AssignHandle(hWnd);
                        collection.Add(window);
                        return false;//stop enumerating
                    }
                }
                return true;//continue enumerating
            };

            GetNativeWindowHelper.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);
            if (collection.Count != 1)
            {
                //log error

                ReleaseWindow();
                return null;
            }                    
            else
                return collection[0];
        }

        public static void ReleaseWindow()
        {
            foreach (var item in collection)
            {
                item.ReleaseHandle();
            }
        }

Note that I have streamed all values of "strTitle" into a file. Then performed a text base search for keywords in my caption and was unsuccessful. Why does the enumeration not find the window I’m looking for in some cases yet it does in other cases?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:56:58+00:00Added an answer on June 13, 2026 at 10:56 pm

    How did you run it 100 times?.. in a tight loop, restarted the application, etc?

    According to your code if you run it in a loop without clearing the collection you will error on every single found entry after the first one it finds because of the error condition if (collection.Count != 1).

    Then on every EnumDesktopWindows call you simply add to the collection and then return to caller. The collection never gets cleared or reset and as such after it adds the second item the fail condition is true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code which is something like this: osgViewer::Viewer viewer; viewer.setSceneData(scene); viewer.setCameraManipulator( tracker.get() );
I would like to get the XAML source of a WPF Window (MainWindow). Clicking
I would like to get a help from you all. The below is the
I would like to get the dimensions (coordinates) for all the HTML elements of
I would like to get to the bottom of this because it's confusing me.
I've got a window and I would like to get notified when the position
I would like to get process id and process name via using window title
I would like to get the context of my window into a bitmap. I
I would like to get the executable file´s path of the active foreground window.
I have a VSPackage that I would like to get information similar to that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.