File tree 2 files changed +14
-4
lines changed
2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,9 @@ static class User32
33
33
[ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
34
34
public static extern int GetWindowText ( IntPtr handle , StringBuilder title , int size ) ;
35
35
36
+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
37
+ public static extern int GetWindowTextLength ( IntPtr handle ) ;
38
+
36
39
[ DllImport ( "user32.dll" , CharSet = CharSet . Auto ) ]
37
40
public static extern int GetClassName ( IntPtr handle , StringBuilder className , int size ) ;
38
41
Original file line number Diff line number Diff line change @@ -324,10 +324,17 @@ public static void SetOpacity(IntPtr hWnd, byte opacity)
324
324
325
325
public static string GetWindowText ( IntPtr hWnd )
326
326
{
327
- var builder = new StringBuilder ( 1024 ) ;
328
- User32 . GetWindowText ( hWnd , builder , builder . Capacity ) ;
329
- var windowText = builder . ToString ( ) ;
330
- return windowText ;
327
+ var length = GetWindowTextLength ( hWnd ) ;
328
+ if ( length > 0 )
329
+ {
330
+ var builder = new StringBuilder ( length + 1 ) ;
331
+ User32 . GetWindowText ( hWnd , builder , builder . Capacity ) ;
332
+ return builder . ToString ( ) ;
333
+ }
334
+ else
335
+ {
336
+ return string . Empty ;
337
+ }
331
338
}
332
339
333
340
public static string GetClassName ( IntPtr hWnd )
You can’t perform that action at this time.
0 commit comments