I need to enumerate all open windows and get their title, but the problem is that some windows belong to the same process but to a different thread, which is blocked (waiting for a mutex). Therefore I cannot use GetWindowText for windows that belong in my own process as this will result to a SendMessage call which will block my code's execution (as it will be waiting a relpy for the blocked thread).
Btw here is an interesting article on how the GetWindowText works internally: http://blogs.msdn.com/b/oldnewthing/archive/2003/08/21/54675.aspx
As a solution decided to use SendMessageTimeout to the window in order to retrieve its title but I can't make it work. What I am doing is:
[DllImport("User32.dll")]
public static extern int SendMessageTimeout(
IntPtr hWnd,
int uMsg,
int wParam,
int lParam,
int fuFlags,
int uTimeout,
out StringBuilder lpdwResult);
...
StringBuilder sb = new StringBuilder(256);
int result = Win32API.SendMessageTimeout(
hWnd,
0x0D /*WM_GETTEXT*/,
256,
0,
10 /*SMTO_ABORTIFHUNG | SMTO_NOTIMEOUTIFNOTHUNG*/,
500,
out sb);
but I always get 0 meaning that the function failed, and sb is always null. Any ideas? Thanks a lot.