Why does GetWindowText hang with a “closed” handle but not with a random one

前端 未结 2 782
旧巷少年郎
旧巷少年郎 2020-12-21 01:12

Using the following code

    [DllImport(\"user32.dll\", EntryPoint = \"GetWindowText\", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
         


        
相关标签:
2条回答
  • 2020-12-21 01:27

    It's impossible to answer this question in any meaningful way. The Win32 interface makes no guarantees about what happens when you pass invalid window handles to routines. It is an error to do so. Please refrain.

    Having said all that, passing title.Capacity + 1 to GetWindowText is an error even with a valid window handle.

    0 讨论(0)
  • 2020-12-21 01:42

    Read here a description of GetWindowText undercovers: The secret life of GetWindowText.

    I don't think you'll ever get a better one :-) If you really want to be 100% sure you won't hang calling it, you need to do it on another thread that you can manage yourself (ie: kill if you need to)

    0 讨论(0)
提交回复
热议问题