How to get window title using SendMessageTimeout in C #

I need to list all open windows and get their name, but the problem is that some windows belong to the same process, but to another thread that is blocked (waiting for the mutex). Therefore, I cannot use GetWindowText for windows that belong to my own process, as this will result in a SendMessage call that will block the execution of my code (since it will wait for reinstallation for a blocked thread).

Btw here's an interesting article on how GetWindowText works internally: http://blogs.msdn.com/b/oldnewthing/archive/2003/08/21/54675.aspx

As a solution, I decided to use SendMessageTimeout in the window to get its title, but I cannot get it to work. I'm doing it:

[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);

0, , , sb null. ? .

+3
2

bufer lParam, lpdwResult. lpdwResult NULL Int32, . , SendMessageTimeout , .

: , PInvoke SendMessageTimeout , . , , PInvoke, API.

+1

@Alex . , P/Invoke, .

[DllImport("User32.dll", SetLastError=true)] 
public static extern int SendMessageTimeout( 
  IntPtr hWnd,  
  uint uMsg,  
  uint wParam,
  StringBuilder lParam,  
  uint fuFlags,  
  uint uTimeout,  
  IntPtr lpdwResult);

StringBuilder lParam, WM_GETTEXT , lParam, IntPtr.Zero lpdwResult.

+5

Source: https://habr.com/ru/post/1763610/


All Articles