Skip to content

Commit db2ad0a

Browse files
authored
View Controller: account for no title condition (#743)
If the hWnd does not have a window title, assume that there is no title and return `nil`. Account for the trailing nul-terminator on the title when copying the text.
1 parent 51c3139 commit db2ad0a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Sources/SwiftWin32/View Controllers/ViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@ public class ViewController: Responder {
177177
public var title: String? {
178178
get {
179179
let szLength: Int32 = GetWindowTextLengthW(view.hWnd)
180+
guard szLength > 0 else { return nil }
181+
180182
let buffer: [WCHAR] = Array<WCHAR>(unsafeUninitializedCapacity: Int(szLength) + 1) {
181-
$1 = Int(GetWindowTextW(view.hWnd, $0.baseAddress!, CInt($0.count)))
183+
$1 = Int(GetWindowTextW(view.hWnd, $0.baseAddress!, CInt($0.count))) + 1
182184
}
183185
return String(decodingCString: buffer, as: UTF16.self)
184186
}

0 commit comments

Comments
 (0)