0

EDIT3: I need a list from all windows a thread has. Not FindWindowsEnum() because it only returns Top level Windows.... I need the window handle befor it's visible END EDIT3

EDIT2: Short desciption: I need a method, where i can get a "window handle" from a thread.

END EDIT2:

EDIT: First of all i can't use FindWindowsEnum()!!! Because it only returns the top windows. I have to kill the window befor it is visible. So i have to get The Handle of the window by a Thread ID. EDIT END:

I got a problem by a window which i have to close which is not focused or something like that. I have to close the window as it pops up. I only use code.

What i got:

This is the extern class where I import one dll and some functions which use the functions from the dll.

    class FindWindowApi
{
    //[DllImport("user32.dll", SetLastError = true)]
    //public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    public IntPtr[] GetWindowHandlesForThread(int threadHandle)
    {
        _results.Clear();
        EnumWindows(WindowEnum, threadHandle);
        return _results.ToArray();
    }

    public  delegate int EnumWindowsProc(IntPtr hwnd, int lParam);

    [DllImport("user32.Dll")]
    public static extern int EnumWindows(EnumWindowsProc x, int y);

    public List<IntPtr> _results = new List<IntPtr>();

    public int WindowEnum(IntPtr hWnd, int lParam)
    {

        _results.Add(hWnd);
        return 1;
    }


    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int GetWindowText(
        IntPtr handle,
        [MarshalAs(UnmanagedType.LPWStr)] StringBuilder caption,
        int count);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int GetWindowTextLength(IntPtr handle);

    public const UInt32 WM_CLOSE = 0x0010;
}

at this class i use the functions from the extern class.

I start a Thread which runs every 10 milli secounds and kill a window in another method.

    public Disk_UC()
    {
        InitializeComponent();
        Thread killit = new Thread(this.killIt);
        killit.Start();
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
        Thread nt = new Thread(this.fillData);
        nt.Start();

    }

    //Thread Method
    private void killIt()
    {
        bool blac = false;
        Int32 oldThreadID = threadID;
        FindWindowApi api = new FindWindowApi();
        while ((!blac) 
            && (!MainDiag.isGoingDown))
        {
            if (oldThreadID != threadID)
            {

                IntPtr[] windows = api.GetWindowHandlesForThread(threadID);
                if (windows != null && windows.Length > 0)
                    foreach (IntPtr hWnd in windows)
                    {
                        killWindow(hWnd, threadID);
                    }

            }
            Thread.Sleep(10);
        }
    }   

    //Method which calls all the extern dll stuff
    private bool killWindow(IntPtr handle, int param)
    {
        var length = FindWindowApi.GetWindowTextLength(handle);
        var caption = new StringBuilder(length + 1);
        FindWindowApi.GetWindowText(handle, caption, caption.Capacity);

        IntPtr windowPtr = FindWindowApi.FindWindowByCaption(IntPtr.Zero, caption.ToString());
        if (windowPtr == IntPtr.Zero)
        {
            return false;
        }
        FindWindowApi.SendMessage(windowPtr, FindWindowApi.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        return true;
    }

My Problem is now how can i get this specific window handle. when the Thread "fillData" pops up the window. So i save the thread ID from this Thread fillData.

Now i call the other Methods with this thread id. I got the Thread ID and i get so many windows from the Process. But i need this specific window from that thread.

Zaza
  • 9
  • 4
  • possible duplicate of [How can I detect if a thread has windows handles?](http://stackoverflow.com/questions/1922982/how-can-i-detect-if-a-thread-has-windows-handles) – James Apr 21 '15 at 14:04
  • with EnumWindowsProc i dont get the Window i need.... – Zaza Apr 21 '15 at 15:02
  • This is very confused, and almost impossible to make sense of. Please step back and explain more clearly what you are trying to achieve, and how your current attempts fail. – David Heffernan Apr 21 '15 at 22:34
  • The thread "fill data" use another .dll in this dll is an error. When this error occured, the Thread will be paused and an error messagebox appears. I can't edit the code from this dll. – Zaza Apr 22 '15 at 05:42
  • Now i implemented a new thread which runs parallel to kill the error messagebox. But i dont get the Handle of this Messagebox. I only got a threadID and a processid. I have to kill this Messagebox befor it appears. The EnumWindowsProc method only returns a window which is already visible. What i need is a handle on this MessageBox only by using a ThreadID and/or a ProcessID. By the way in this programm are 10 own implemented threads. Please help me .dll import is alowed too. – Zaza Apr 22 '15 at 05:46
  • "Kill it before it appears" sounds like a race condition that is hard to get right. On Windows this is nearly impossible if the messagebox creating code does not provide some kind of hook. – Emond Apr 22 '15 at 05:55
  • The code in the Thread "filData" will be paused when the messageBox appears. The MessageBox will be build. Befor its visible i can close it. But i first of all i need the Handle of this MessageBox. – Zaza Apr 22 '15 at 06:01
  • You cannot turn a thread id into a window handle directly because a single thread can service multiple windows. So you need to enumerate them. See http://stackoverflow.com/questions/1922982/how-can-i-detect-if-a-thread-has-windows-handles I am not sure it will list the messagebox but it is worth a try. – Emond Apr 22 '15 at 19:53
  • I tried to enumerate them ... and in the description i tried to say that this dont get me the window i want -.- – Zaza Apr 23 '15 at 05:18
  • aaaaand i can say that this thread only have one Window – Zaza Apr 23 '15 at 05:18

0 Answers0